1、授权获取openId和手机号
<button class="sub_btn " v-if="!phone&&!openId" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权并获取手机号</button>
getPhoneNumber (e) {
console.log(e.detail,'获取手机号') // 动态令牌
// console.log(e.detail.code) // 动态令牌
// console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)
// console.log(e.detail.errno) // 错误码(失败时返回)
this.phoneCode=e.detail.code
this.phoneEncryptedData=e.detail.encryptedData
this.phoneIv=e.detail.iv
//
this.login()
},
login(){
uni.login({
provider: 'weixin',
success: async (loginRes)=> {
// 打印临时凭证
console.log(loginRes.code,'code');
this.getSessionKey(loginRes.code)
// let res = await getOpenId({code:loginRes.code})
// console.log(JSON.parse(res).openId,'获取opneid')
// uni.setStorageSync('openId',JSON.parse(res).openId);
// // 获取用户信息
// uni.getUserInfo({
// provider: 'weixin',
// success: function (infoRes) {
// console.log('用户昵称为:' + infoRes.userInfo.nickName);
// console.log(infoRes);
// }
// });
}
});
},
async getSessionKey(code){
console.log('开始获取openId')
let res = await getOpenId({code})
console.log(JSON.parse(res),'获取opneid')
this.openId = JSON.parse(res).openId;
this.sessionKey = JSON.parse(res).session_key;
uni.setStorageSync('openId',JSON.parse(res).openId);
this.decrypt()
// uni.request({
// url: 'https://api.weixin.qq.com/sns/jscode2session',// 请求微信服务器
// method:'GET',
// data: {
// appid: this.appid, //你的小程序的APPID
// secret: this.secret, //你的小程序秘钥secret,
// js_code: code, //wx.login 登录成功后的code
// grant_type:'authorization_code' //此处为固定值
// },
// success: (res) => {
// console.log('获取信息',res);
// this.openId = res.data.openid;
// uni.setStorageSync('openId',res.data.openid)
// this.sessionKey = res.data.session_key;
// this.decrypt()
// }
// });
},
decrypt(){
console.log(this.sessionKey,this.phoneEncryptedData,this.phoneIv,'解密参数')
let pc = new WXBizDataCrypt(appId,this.sessionKey);
let data = pc.decryptData(this.phoneEncryptedData , this.phoneIv);
console.log(data,'解密结果') //data就是最终解密的用户信息
this.phone = data.phoneNumber // 手机号
uni.setStorageSync('phone',data.phoneNumber)
this.savePhone()
},
async savePhone(){
let res = await saveGndhSign({openId:this.openId,phone:this.phone})
console.log(res,'保存手机号')
uni.showModal({
title: '提示',
content: '是否允许订阅消息',
success: (res) =>{
if (res.confirm) {
uni.requestSubscribeMessage({
tmplIds,
success: res => {
console.log(res,'订阅消息成功')
//if(res.tmplId=='acc')
},
fail: res=>{
console.log(res,'取消订阅消息')
}
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
WXBizDataCrypt.js
var crypto = require('crypto')
function WXBizDataCrypt(appId, sessionKey) {
this.appId = appId
this.sessionKey = sessionKey
}
WXBizDataCrypt.prototype.decryptData = function (encryptedData, iv) {
// base64 decode
var sessionKey = new Buffer(this.sessionKey, 'base64')
encryptedData = new Buffer(encryptedData, 'base64')
iv = new Buffer(iv, 'base64')
try {
// 解密
var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)
// 设置自动 padding 为 true,删除填充补位
decipher.setAutoPadding(true)
var decoded = decipher.update(encryptedData, 'binary', 'utf8')
decoded += decipher.final('utf8')
decoded = JSON.parse(decoded)
} catch (err) {
throw new Error('Illegal Buffer')
}
if (decoded.watermark.appid !== this.appId) {
throw new Error('Illegal Buffer')
}
return decoded
}
module.exports = WXBizDataCrypt