小程序模板網(wǎng)

微信小程序AES解密失敗

發(fā)布時(shí)間:2018-05-30 11:38 所屬欄目:小程序開(kāi)發(fā)教程

微信小程序分享群獲取群id時(shí)后端接口返回“微信AES解密失敗”,后來(lái)定位到原因是服務(wù)端用于解密的session_key失效。用戶(hù)獲取到openID存在緩存后,就不會(huì)每次login獲取登錄態(tài)了,這樣會(huì)導(dǎo)致登錄態(tài)失效,即后端維護(hù)的session_key失效。分享群后獲取的加密信息是老的session_key+openId構(gòu)成,服務(wù)端解密時(shí)的session_key要和分享前一致。

在需要獲取openGid的頁(yè)面:

app.getOpenId(this.route, this.data.pageOptions).then((res) => {
wx.login({
success(res) {
// 刷新服務(wù)端session_key
api.ajax('GET', api.config_url.refreshWxUserSessionKey, {
appId: api.appId,
code: res.code,
}).then(res => {
const { status, message } = res.data;
if (status) {
console.log('登錄態(tài)刷新成功');
}
}, res => { });
}
})
// 其他業(yè)務(wù)邏輯

app.js

//獲取openGid
getOpenGid(shareTicket, status, callback) {
var self = this;
wx.getShareInfo({
shareTicket: shareTicket,
complete(res) {
var param = {
"iv": res.iv,
"encryptedData": res.encryptedData,
"appId": self.appId,
"openId": wx.getStorageSync("openId")
};
self.getAesDecryptData(callback, param);
}
});
},
getAesDecryptData(callback, param) {
const self = this;
this.ajax('POST', this.config_url.aesDecryptData, param).then(res => {
console.log(res);
if (res.data.entry && res.data.entry.openGId) {
wx.setStorageSync("openGId", res.data.entry.openGId);
callback() && callback();
} else {
console.log('獲取群id失敗');
wx.login({
success(res) {
self.ajax('GET', self.config_url.refreshWxUserSessionKey, {
appId: self.appId,
code: res.code,
}).then(res => {
const { status, message } = res.data;
if (status) {
console.log('登錄態(tài)刷新成功');
}
}, res => { });
}
});
wx.removeStorageSync('openGId');
}
});
},
 


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://22321a.com/wxmini/doc/course/24490.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢(xún):800182392 點(diǎn)擊咨詢(xún)
QQ在線咨詢(xún)