UNPKG

d-utils

Version:

d-utils

104 lines (103 loc) 8.05 kB
/** * d-utils version: 4.0.3 * by ifmiss */ "use strict";require("../checkType.js"),require("../_tslib-282d34cf.js"),require("../isObject.js");var e=require("../log/index.js"),n=require("../isAndroid.js"),t=require("../isIOS.js"),i=require("../parseUrl.js"),r=require("weixin-js-sdk"),o=require("sha1"),a=/** @class */function(){function a(){} /** * ios 安卓需要验签的地址 * @returns { string } 浏览器url */return a.sdkUrlIosOrAndorid=function(){return(t.isIOS()||n()&&!a.isUpThanWxVersion("6.3.31"))&&window.__D_UTILS_WX_FIRST_URL_HOOK__?window.__D_UTILS_WX_FIRST_URL_HOOK__:window.location.href.split("#")[0]}, /** * @description IOS 或者 Android 微信版本小于6.3.31 需要种植首次进入页面的URL,用于解决微信签名错误 */ a.plantSdkUrlIosOrAndorid=function(){window.__D_UTILS_WX_FIRST_URL_HOOK__||(window.__D_UTILS_WX_FIRST_URL_HOOK__=window.location.href.split("#")[0])}, /** * @description wxSign 微信验签的动作 * @param { String } jsapi_ticket 公众号用于调用微信JS接口的临时票据 * @return { IWxSign } 返回 timestamp, nonceStr, signature */ a.wxSign=function(e){var n=a.randomWord(16),t=(Date.now()+"").substr(0,10),i=a.sdkUrlIosOrAndorid();return{timestamp:t,nonceStr:n,signature:o("jsapi_ticket="+e+"&noncestr="+n+"&timestamp="+t+"&url="+i)}}, /** * 跳转微信oauth2授权登录 非静默授权 * @param { String } appId */ a.routerAuthorized=function(e){var n=window.location.href;n=encodeURIComponent(n),window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+e+"&redirect_uri="+n+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"}, /** * randomWord 产生任意长度随机字母数字组合 * min-任意长度最小位[固定位数] * max-任意长度最大位 */ a.randomWord=function(e,n){var t="",i=e,r=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; // 随机产生 if(n&&n>e)i=Math.round(Math.random()*(n-e))+e;else for(var o=0;o<i;o++){t+=r[Math.round(Math.random()*(r.length-1))]}return t}, /** * @description 是否高于微信某一个版本 * @param { String } version * @returns { Boolean } 返回是否满足条件 */ a.isUpThanWxVersion=function(e){void 0===e&&(e="6.3.31");var n=window.navigator.userAgent,t=e.split(".").map((function(e){return parseInt(e,10)})),i=/MicroMessenger\/([\d|\.]+)/;if(null===i.exec(n))return!1;var r=(i.exec(n)||[])[1].split(".");return r.length>=4&&(r=r.slice(0,3)),(r=r.map((function(e){return parseInt(e,10)})))[0]>t[0]||(r[0]===t[0]&&r[1]>t[1]||r[0]===t[0]&&r[1]===t[1]&&r[2]>=t[2])}, /** * @description 初始化微信配置签名 * @param { Object } data 微信的签名配置 * @props { Boolean } data.debug 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 * @props { String } data.appId 必填,公众号的唯一标识 * @props { Number } data.timestamp 必填,生成签名的时间戳 * @props { String } data.nonceStr 必填,生成签名的随机串 * @props { String } data.signature 必填,签名 * @props { Array } data.jsApiList 必填,需要使用的JS接口列表 * @link 接口列表地址 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 */ a.initWxConfig=function(n){r.config(Object.assign({},{debug:!1},n)),r.error((function(n){e.error(n,"[d-utils] wx.config error => ")}))}, /** * 分享给朋友 * @param {Object} sharInfo * @props { String } sharInfo.title 分享的title * @props { String } sharInfo.desc 分享描述 * @props { String } sharInfo.link 分享链接 * @props { String } sharInfo.imgUrl 分享图标 * @props { Function } sharInfo.success 成功的回调 * @props { Function } sharInfo.cancel 取消的回调 * @props { Function } sharInfo.complete 完成的回调 * @return { Promise<IWxCallBackType> } 返回一个promise */ a.wxShareToFriend=function(e){var n=Object.assign({},this.defaultShareInfo,e);return new Promise((function(e,t){try{r.ready((function(){r.onMenuShareAppMessage({title:n.title,desc:n.desc,link:n.link,imgUrl:n.imgUrl,success:function(t){var i={type:"onMenuShareAppMessage",data:t};n.success(i),e(i)},cancel:function(t){var i={type:"onMenuShareAppMessage",data:t};n.cancel(i),e(i)},complete:function(t){var i={type:"onMenuShareAppMessage",data:t};n.complete(i),e(i)}})}))}catch(e){t({type:"onMenuShareAppMessage",data:e})}}))}, /** * 分享到朋友圈 * @param {Object} sharInfo * @props { String } sharInfo.title 分享的title * @props { String } sharInfo.link 分享链接 * @props { String } sharInfo.imgUrl 分享图标 * @props { Function } sharInfo.success 成功的回调 * @props { Function } sharInfo.cancel 取消的回调 * @props { Function } sharInfo.complete 完成的回调 * @return { Promise<IWxCallBackType> } 返回一个promise */ a.wxShareToFriendCircle=function(e){var n=Object.assign({},this.defaultShareInfo,e);return new Promise((function(e,t){try{r.ready((function(){r.onMenuShareTimeline({title:n.title,link:n.link,imgUrl:n.imgUrl,success:function(t){var i={type:"onMenuShareTimeline",data:t};n.success(i),e(i)},cancel:function(t){var i={type:"onMenuShareTimeline",data:t};n.cancel(i),e(i)},complete:function(t){var i={type:"onMenuShareTimeline",data:t};n.complete(i),e(i)}})}))}catch(e){t({type:"onMenuShareTimeline",data:e})}}))}, /** * 隐藏所有非基础按钮接口 * @return { Promise<IWxCallBackType> } 返回一个promise */ a.hideAllNonBaseMenuItem=function(){return new Promise((function(e,n){r.ready((function(){try{r.hideAllNonBaseMenuItem(),e({type:"hideAllNonBaseMenuItem",data:"成功"})}catch(e){n({type:"hideAllNonBaseMenuItem",data:e})}}))}))}, /** * 批量隐藏功能按钮接口 * @param { array } arr // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3 * @return { Promise<IWxCallBackType> } 返回一个promise */ a.hideMenuItems=function(e){return void 0===e&&(e=[]),new Promise((function(n,t){r.ready((function(){try{r.hideMenuItems({menuList:e}),n({type:"hideMenuItems",data:"成功, 隐藏的数组名称: "+e})}catch(e){t({type:"hideMenuItems",data:e})}}))}))},a.wx=r, /** * @description 初始化微信请求 js-sdk 的url地址 需要区分两种情况 * IOS 或者 Android 微信版本小于6.3.31, Android 微信版本大于6.3.31 * 当前这种只支持与VUE单页面模式 * @returns 返回获取jssdk的url参数值 */ a.defaultShareInfo={title:"这是一个微信分享的title",desc:"这是一个微信分享的desc",link:"这是一个微信分享的link",imgUrl:"这是一个微信分享的imgUrl",success:function(){},cancel:function(){},complete:function(){}}, /** * ios 手机在code过期之后会重新静默授权,会导致分享失败,通过url中是否存在code,针对ios用户执行reload的操作 * @since 3.0.1 */ a.plantIosReloadShim=function(){var e=i();Object.keys(e).includes("code")&&t.isIOS()&&localStorage.setItem("weixin-utils-reload","true")}, /** * 在其他页面都需要添加改方法,用户在页面加载之后重新reload,已保证微信分享正常 * @since 3.0.1 */ a.reloadIosWhenCode=function(){var e=window.location.href.split("?")[0],n=localStorage.getItem("weixin-utils-reload"),t=new URLSearchParams(window.location.search);t.delete("code");var i=t.toString()?e+"?"+t.toString():e;"true"===n&&(localStorage.removeItem("weixin-utils-reload"),setTimeout((function(){location.replace(i)}),88))},a}();module.exports=a;