官網地址:https://work.weixin.qq.com/api/doc#90000/90136/90514
1. 調用后台接口,獲取config接口所需要的信息。(簽名、時間戳。。。。)
2. wx.config注入權限驗證配置(注入的是企業的身份與權限)
3.通過ready接口處理成功驗證
4.在creatd()或mounted()里面放代碼
getWxJsJdk(){ this.$axios.get('端口地址url', { params:{ url: location.href.split('#')[0] } }) .then(res => { if(res.data.errorCode === '10001') { this.signature = res.data.resultData.signature; this.appId = res.data.resultData.appId; this.noncestr = res.data.resultData.noncestr; this.timestamp= res.data.resultData.timestamp; wx.config({ beta: true,// 必須這么寫,否則wx.invoke調用形式的jsapi會有問題 debug: false, // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。 appId: this.appId, // 必填,企業微信的corpID timestamp: this.timestamp, // 必填,生成簽名的時間戳 nonceStr: this.noncestr, // 必填,生成簽名的隨機串 signature: this.signature,// 必填,簽名,見附錄1 jsApiList: ['hideOptionMenu'] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2 }); wx.ready(function () { wx.hideOptionMenu(); }); wx.error(function(res){//通過error接口處理失敗驗證 // config信息驗證失敗會執行error console.log('執行失敗'); }); } else { console.log(res.data.errorCode) } }).catch(err => { console.log(err); }) },
注意:
這里要注意的就是url的問題,如果url沒有正確傳遞,后端也會返回信息,但是簽名信息會是錯誤的。
上面提到的完整url指的是:'http(s)://'部分,以及'?'后面的GET參數部分,但不包括'#'hash后面的部分。可以通過 location.href 來獲取。
如果你的vue項目,路由沒有開啟history 模式,也就是你的url上面包含“#”,這個時候要從后端正確獲取簽名,就需要 去掉url上#后面的字符 。(url去掉'#'hash部分,可用 location.href.split('#')[0] )
