微信小程序获取手机号

微信小程序获取手机号

2023年7月2日发(作者:)

微信⼩程序获取⼿机号

wxml 代码:您的⼿机号:{{phone}}

wxjs:代码// 授权登录 getUserProfile(e) { // 获取⽤户信息 rProfile({ desc: '⽤于完善会员资料', // 声明获取⽤户个⼈信息后的⽤途,后续会展⽰在弹窗中,请谨慎填写 success: (res) => { // 头像 let avatarUrl=Url; // 昵称 let nickName=me; ({ success:res=>{ // 获取code let code=; t({ url: '/api/xcx/getLogin', data: { code:code, avatarUrl:avatarUrl, nickName:nickName, }, method:"POST", header: { 'content-type': 'application/json' // 默认值 }, // 数据返回json格式 success (res) { (res); // 存储openid ⽤户获取⼿机号 rageSync('openid', ); // 存储session_key⽤于获取⼿机号 rageSync('sessionKey', nKey); // 存储⽤户id rageSync('userid',); } }) } }) } }) },将⽤户信息发送后台/*获取⽤户信息的路由*/Route::post('xcx/getLogin','xcxLoginController@getLogin');控制器代码: public function getLogin(Request $request){// 接受code $code=$request->post('code');// 接受头像 $avatarUrl=$request->post('avatarUrl');// 接受昵称 $nickName=$request->post('nickName');// 获取appid $appid = "wx64832aa6eaea82b0"; // 从微信公众平台获得secret $secret = "95e2acaf355dbcb443f5cd4748a152ed"; // 发送请求换取openid和sessionkey $url = "/sns/jscode2session?appid=$appid&secret=$secret&js_code=" . $code. "&grant_type=authorization_code"; // 暂使⽤file_get_contents()发送请求,你可以使⽤CURL扩展的形式实现,获取opid和session_key $res = json_decode(file_get_contents($url), true);// 给$params追加openid $params['openid'] = $res['openid']; // 给$params追加session_key $params['sessionKey'] = $res['session_key'];// 查看数据库⾥是否有openid,有就修改,没有就添加 $user = Wxuser::where('openid', $params['openid'])->first(); $params['uid']=$user['id'];// 有就修改⽤户的额openID if ($user) {// 将sessionKey 发送⾄⼩程序缓冲⽤于获取⼿机号 Wxuser::where('openid', $params['openid'])->update(['sessionKey'=>$params['sessionKey']]); return ['code' => 201, 'meg' => '修改成功', 'data' => $params]; } else {// 没有就添加新⽤户 $add = Wxuser::create($params); return ['code' => 200, 'meg' => '添加成功', 'data' => $res]; } }⼩程序点击获取⼿机号按钮

代码需要将 button 组件 open-type 的值设置为 getPhoneNumber,当⽤户点击并同意之后,可以通过 bindgetphonenumber 事件回调获取到微信服务器返回的加密数据, 然后在第三⽅服务端结合 session_key 以及 app_id 进⾏解密获取⼿机号。 // 获取⼿机号 getphone(e){ // open-type="getPhoneNumber" bindgetphonenumber="getphone" 获取encryptedData var encryptedData =tedData; var iv =; // 取出授权登录的openid let openid= rageSync('openid') // 取出授权登录的sessionKey let sessionKey= rageSync('sessionKey') // 取出⽤户id var userid= rageSync('userid') t({ url: '/api/xcx/send', data: { encryptedData:encryptedData, iv:iv, openid:openid, sessionKey:sessionKey, userid:userid }, method:"POST", header: { 'content-type': 'application/json' // 默认值 }, // 数据返回json格式 success :res=> { (res); if (res) { a({ phone: }) } }

}) }

laravel 路由/*获取⼿机号*/Route::post('xcx/send','xcxLoginController@send');laravel控制器//获取⼿机号public function send(Request $request){ $encryptedData = $request->post('encryptedData'); $iv = $request->post('iv'); $sessionKey = $request->post('sessionKey'); $id=$request->post('userid'); $appid = 'wx64832aa6eaea82b0'; $pc = new WXBizDataCrypt($appid, $sessionKey); $errCode = $pc->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { $phone = json_decode($data, true)['phoneNumber']; //根据⽤户id 将⼿机号添加到数据表⾥⾯ Wxuser::where('id','=',$id)->update(['phone' => $phone]); return ['code' => 200, 'meg' => 'success', 'data' => $phone]; } else { return ['code' => 500, 'meg' => 'error', 'data' => $errCode]; }

模型代码

下载php

将⽂件放⼊开发的项⽬中,并修改⽂件名称和修改命名空间

decryptData($encryptedData, $iv, $data );if ($errCode == 0) { print($data . "n");} else { print($errCode . "n");} *

  • -41001: encodingAesKey ⾮法
  • *
  • -41003: aes 解密失败
  • *
  • -41004: 解密后得到的buffer⾮法
  • *
  • -41005: base64加密失败
  • *
  • -41016: base64解密失败
  • * */class ErrorCode{ public static $OK = 0; public static $IllegalAesKey = -41001; public static $IllegalIv = -41002; public static $IllegalBuffer = -41003; public static $DecodeBase64Error = -41004;}>sessionKey = $sessionKey; $this->appid = $appid; } /** * 检验数据的真实性,并且获取解密后的明⽂. * @param $encryptedData string 加密的⽤户数据 * @param $iv string 与⽤户数据⼀同返回的初始向量 * @param $data string 解密后的原⽂ * * @return int 成功0,失败返回对应的错误码 */ public function decryptData( $encryptedData, $iv, &$data ) { if (strlen($this->sessionKey) != 24) { return ErrorCode::$IllegalAesKey; } $aesKey=base64_decode($this->sessionKey); if (strlen($iv) != 24) { return ErrorCode::$IllegalIv; } $aesIV=base64_decode($iv); $aesCipher=base64_decode($encryptedData); $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); $dataObj=json_decode( $result ); if( $dataObj == NULL ) { return ErrorCode::$IllegalBuffer; } if( $dataObj->watermark->appid != $this->appid ) { return ErrorCode::$IllegalBuffer; } $data = $result; return ErrorCode::$OK; }}tp框架获取⼿机号的代码,参看以下链接/guanj0623/article/details/121139157?spm=1001.2014.3001.5501

    发布者:admin,转转请注明出处:http://www.yc00.com/news/1688279566a112560.html

    相关推荐

    发表回复

    评论列表(0条)

    • 暂无评论

    联系我们

    400-800-8888

    在线咨询: QQ交谈

    邮件:admin@example.com

    工作时间:周一至周五,9:30-18:30,节假日休息

    关注微信