uniapp抖音小程序一鍵獲取用戶手機號的示例代碼
更新時間:2024年12月26日 11:54:37 作者:還這么多錯誤?!
文章介紹了如何在uniapp抖音小程序中通過點擊按鈕一鍵獲取用戶手機號,encryptedData和iv通過點擊按鈕回傳,后端部分通過解密獲取手機號,感興趣的朋友一起看看吧
前端部分
點擊按鈕,獲取手機號
<button class="button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一鍵獲取</button>
傳入sessionKey和encryptedData、iv
其中sessionKey是通過登錄時,調(diào)用抖音接口https://developer.toutiao.com/api/apps/v2/jscode2session獲得
encryptedData、iv則通過點擊按鈕回傳的事件參數(shù)
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;
}到此這篇關(guān)于uniapp抖音小程序一鍵獲取用戶手機號的示例代碼的文章就介紹到這了,更多相關(guān)uniapp抖音小程序獲取用戶手機號內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
JS實現(xiàn)的數(shù)組去除重復(fù)數(shù)據(jù)算法小結(jié)
這篇文章主要介紹了JS實現(xiàn)的數(shù)組去除重復(fù)數(shù)據(jù)算法,總結(jié)分析了4種比較常見的數(shù)組去重復(fù)算法及相關(guān)使用技巧,需要的朋友可以參考下2017-11-11
微信小程序?qū)崿F(xiàn)的3d輪播圖效果示例【基于swiper組件】
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)的3d輪播圖效果,結(jié)合實例形式分析了微信小程序基于swiper組件相關(guān)屬性設(shè)置、事件響應(yīng)操作技巧,需要的朋友可以參考下2018-12-12

