uniapp抖音小程序,如何一键获取用户手机号
前端部分
点击按钮,获取手机号
<button class="button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一键获取</button>
传入sessionKey和encryptedData、iv
其中sessionKey是通过登录时,调用抖音接口https://developer.toutiao.com/api/apps/v2/jscode2session获得
encryptedData、iv则通过点击按钮回传的事件参数
getPhoneNumber(e) {
if (e.detail) {
if (e.detail.errMsg == "getPhoneNumber:fail auth den") {
uni.showToast({
title: '小程序通过试运营期,才能一键获取手机号',
icon: 'none'
});
}
this.getUserPhone(e.detail)
} else {
this.hasPhoneValue = false
}
},
async getUserPhone(query) {
// douyin-ad-1ddba+123456 /admin/dyinAd/answerAd
let Authorization = this.$user.token || "none Authorization";
let http_url = this.$config.base_url + '/app/user/login/getDyPhone'
let http_data = {
sessionKey: this.$user.sessionKey,
encryptedData: query.encryptedData,
iv: query.iv,
"admin": this.$config.admin,
}
let http_header = {
Authorization
}
let result = await this.$http.post(http_url, http_data, http_header, 'json')
.then(async (res) => {
if (res && res.data) {
let phone = {}
Object.assign(phone, JSON.parse(res.data))
this.formPhone = phone.phoneNumber
}
})
.catch((err) => {
});
},
后端部分
解密手机号
async getDyPhone(query){
const decipher = crypto.createDecipheriv(
"aes-128-cbc",
Buffer.from(query.sessionKey, "base64"),
Buffer.from(query.iv, "base64")
);
let ret = decipher.update(query.encryptedData, "base64", 'utf8');
ret += decipher.final('utf8');
return ret;
}