jssdkAccess.js
import apiSign from ‘@/common/apiSign‘ import Vue from ‘vue‘ function jssdkAccess(appId){ return Vue.prototype.baseAxios.get(‘/getSignInfo‘).then(data=>{ let wxConfig = data.data.retData[0]; let nonceStr = wxConfig.nonce_str; let timestamp = parseInt(wxConfig.timestamp/1000); let signature = wxConfig.jsapi_ticket; let sign = apiSign.signFactory.getSignSha1String({noncestr:nonceStr,jsapi_ticket:signature,timestamp,url:location.href.match(/^((https|http|ftp|rtsp|mms)?:\/\/)[^\s#]+/)[0]}) wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: appId, // 必填,公众号的唯一标识 timestamp: timestamp , // 必填,生成签名的时间戳 nonceStr: nonceStr, // 必填,生成签名的随机串 signature: sign,// 必填,签名,见附录1 jsApiList: [‘onMenuShareTimeline‘,‘showMenuItems‘,‘hideMenuItems‘,‘onMenuShareAppMessage‘,‘onMenuShareQQ‘,‘onMenuShareWeibo‘,‘onMenuShareQZone‘,‘chooseImage‘,‘uploadImage‘] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); }); } export default jssdkAccess
apiSign.js
import forge from ‘node-forge‘ import qs from ‘qs‘ let signFactory = { getSignSha1String(obj){ function alphabeticalSort(a, b) { if(a>b) return 1; else if(a==b) return 0; else return -1; } let s = qs.stringify(obj,{ sort: alphabeticalSort }); s = decodeURIComponent(s); console.log(s) return this.sha1(s); }, sha1(str){ var md = forge.md.sha1.create(); md.update(str); return md.digest().toHex(); } } export default { signFactory }
使用的时候传入appId即可