欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

抖音小程序一鍵獲取手機(jī)號(hào)的實(shí)現(xiàn)思路

 更新時(shí)間:2025年01月23日 11:33:03   作者:Icebreaking丶  
前端通過code獲取sessionkey,再用sessionkey解密手機(jī)號(hào)加密信息,PHP后端實(shí)現(xiàn)這一過程,本文通過實(shí)例代碼給大家介紹抖音小程序一鍵獲取手機(jī)號(hào)功能,感興趣的朋友一起看看吧

前端代碼組件

<button 
		v-if="!isFromOrderList"
		class="get-phone-btn" 
		open-type="getPhoneNumber"
		@getphonenumber="onGetPhoneNumber"
	>
		一鍵獲取
	</button>
// 獲取手機(jī)號(hào)回調(diào)
		onGetPhoneNumber(e) {
			var that = this	
			tt.login({
				force: true,
				success(res) {
					console.log('獲取手機(jī)號(hào)回調(diào)', e)
					if (e.detail.errMsg === 'getPhoneNumber:ok') {
						// 獲取成功,調(diào)用后端接口解密手機(jī)號(hào)
						that.decryptPhoneNumber(res.code,e.detail.iv,e.detail.encryptedData)
					} else {
						uni.showToast({
							title: '獲取手機(jī)號(hào)失敗',
							icon: 'none'
						})
					}
				},
				fail(res) {
					console.log(`login 調(diào)用失敗`);
				},
			});
		},
		// 解密手機(jī)號(hào) 后端PHP進(jìn)行解密
		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 || '獲取手機(jī)號(hào)失敗')
				}
			} catch (error) {
				console.error('解密手機(jī)號(hào)失敗:', error)
				uni.showToast({
					title: error.message || '獲取手機(jī)號(hào)失敗',
					icon: 'none'
				})
			}
		}

后端使用的PHP去實(shí)現(xiàn) 思路首先通過前端的code換取sessionkey 然后通過 sessionkey解密前端手機(jī)號(hào)加密信息

    /**
     * 獲取抖音小程序手機(jī)號(hào)
     * @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']];
    }

到此這篇關(guān)于抖音小程序一鍵獲取手機(jī)號(hào)的實(shí)現(xiàn)思路的文章就介紹到這了,更多相關(guān)抖音小程序一鍵獲取手機(jī)號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論