客户端:
getPhoneNumber: function (e) {
var that = this;
if (that.data.phoneNumber) {
wx.showModal({
content: '我们会在5分钟左右联系您',
showCancel: false
})
return
}
if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
wx.showModal({
content: '无法获取手机号码',
showCancel: false
})
return;
}
wx.showLoading({
title: '获取中...',
})
wx.cloud.callFunction({
name: 'token',
data: {
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
sessionCode: app.globalData.sessionCode
},
success: res => {
that.data.phoneNumber = res.result
wx.hideLoading()
wx.showModal({
content: '我们会在5分钟左右联系您',
showCancel: false
})
},
fail: err => {
console.error(err);
wx.showToast({
title: '获取失败',
icon: 'none'
})
}
})
}
服务端:
let key = ''
options = {
method: 'GET',
uri: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + sessionCode + '&grant_type=authorization_code',
json: true
}
await rp(options).then(res => {
console.log(res)
key = res.session_key
})
var decipher = crypto.createDecipheriv('aes-128-cbc', new Buffer(key, 'base64'), new Buffer(iv, 'base64'));
var decoded = decipher.update(new Buffer(encryptedData, 'base64'), 'binary', 'utf8');
decoded += decipher.final('utf8');
await db.collection(dataBaseType)
.where({ _openid: openId })
.update({
data:
{
phoneNumber: JSON.parse(decoded).phoneNumber
},
})
return JSON.parse(decoded).phoneNumber