抖音小程序一鍵獲取手機號的實現(xiàn)思路
更新時間:2025年01月23日 11:33:03 作者:Icebreaking丶
前端通過code獲取sessionkey,再用sessionkey解密手機號加密信息,PHP后端實現(xiàn)這一過程,本文通過實例代碼給大家介紹抖音小程序一鍵獲取手機號功能,感興趣的朋友一起看看吧
前端代碼組件
<button v-if="!isFromOrderList" class="get-phone-btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" > 一鍵獲取 </button>
// 獲取手機號回調 onGetPhoneNumber(e) { var that = this tt.login({ force: true, success(res) { console.log('獲取手機號回調', e) if (e.detail.errMsg === 'getPhoneNumber:ok') { // 獲取成功,調用后端接口解密手機號 that.decryptPhoneNumber(res.code,e.detail.iv,e.detail.encryptedData) } else { uni.showToast({ title: '獲取手機號失敗', icon: 'none' }) } }, fail(res) { console.log(`login 調用失敗`); }, }); }, // 解密手機號 后端PHP進行解密 async decryptPhoneNumber(code,iv,encryptedData) { try { const res = await orderApi.decryptPhone({ code: code, iv: iv, encryptedData: encryptedData }) if (res.code === 1 && res.data && res.data.phone) { this.phone = res.data.phone } else { throw new Error(res.msg || '獲取手機號失敗') } } catch (error) { console.error('解密手機號失敗:', error) uni.showToast({ title: error.message || '獲取手機號失敗', icon: 'none' }) } }
后端使用的PHP去實現(xiàn) 思路首先通過前端的code換取sessionkey 然后通過 sessionkey解密前端手機號加密信息
/** * 獲取抖音小程序手機號 * @param $code * @param $iv * @param $encryptedData * @return \think\response\Json * @throws \GuzzleHttp\Exception\GuzzleException */ public function get_mobile($code, $iv, $encryptedData) { $result = $this->code2Session($code); //解密 $phone = openssl_decrypt(base64_decode($encryptedData, true), 'AES-128-CBC', base64_decode($result['session_key']), OPENSSL_RAW_DATA, base64_decode($iv)); $phone = json_decode($phone, 1); if (isset($phone['phoneNumber']) && $phone['phoneNumber']) { return json([ 'code' => 1, 'msg' => '獲取成功', 'data' => [ 'phone' => $phone['phoneNumber'] ], ]); } else { return json([ 'code' => 0, 'msg' => '獲取失敗', 'data' => [ ], ]); } } /** * 通過code換取 session_key * @param $code * @return array * @throws \GuzzleHttp\Exception\GuzzleException */ public function code2Session($code) { $uri = 'https://developer.toutiao.com/api/apps/v2/jscode2session'; $options = [ 'body' => json_encode([ 'appid' => config('xinghuo_mp.appid'), 'secret' => config('xinghuo_mp.appsecret'), 'code' => $code, 'anonymous_code' => '' ]), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $response = (new \GuzzleHttp\Client)->post($uri, $options); $stringBody = (string)$response->getBody(); $result = json_decode($stringBody, true); return ['openid' => $result['data']['openid'], 'session_key' => $result['data']['session_key']]; }
到此這篇關于抖音小程序一鍵獲取手機號的實現(xiàn)思路的文章就介紹到這了,更多相關抖音小程序一鍵獲取手機號內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于代數(shù)方程庫Algebra.js解二元一次方程功能示例
這篇文章主要介紹了基于代數(shù)方程庫Algebra.js解二元一次方程功能,結合具體實例形式分析了方程庫Algebra.js計算方程的具體使用技巧,需要的朋友可以參考下2017-06-06javascript模擬鼠標點擊事件原理和實現(xiàn)方法
本文詳細介紹了JS模擬鼠標點擊事件的原理以及應用場景,并提供了模擬鼠標左鍵點擊事件、右鍵點擊事件、滾輪事件和移動事件的代碼實現(xiàn),了解JS模擬鼠標點擊事件的原理和實現(xiàn)方法對于開發(fā)人員非常重要,這對于許多面向用戶的web應用程序的開發(fā)和測試都具有很重要的意義2023-09-09JavaScript基礎篇之變量作用域、傳值、傳址的簡單介紹與實例
這篇文章介紹了變量的作用域,傳值,傳址的一些簡單使用,有需要的朋友可以參考一下2013-06-06