欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > 抖音小程序一键获取手机号

抖音小程序一键获取手机号

2025/1/21 14:23:42 来源:https://blog.csdn.net/qq_41526316/article/details/145259423  浏览:    关键词:抖音小程序一键获取手机号

前端代码组件
在这里插入图片描述

	<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去实现 思路首先通过前端的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']];}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com