E企盈营销工具技术服务商 热线:4006-838-530

微信获取用户信息,微信小程序获取用户信息及手机号后端TP5.0

E企盈直播平台营销卖货系统

wxml页面viewwx:if=”{{config.tipsshow1}}”class=’dialog-container’viewclass=’dialog-mask’/viewviewclass=’dialog-info’viewclass=’dialog-title’loginprompt/viewviewclass=’dialog-content’Toprovidebetterservice,click”allow”inthepromptboxlater!/viewviewclass=’dialog-footer’buttonclass=’dialog-btn’open-type=”getUserInfo”bindgetuserinfo=”getUserInfo”Isee./button/view/view/viewviewwx:if=”{{config.tipsshow2}}”class=’dialog-container’viewclass=’dialog-mask’/viewviewclass=’dialog-info’viewclass=’dialog-title’loginprompt/viewviewclass=’dialog-content’Toprovidebetterservice,click”allow”inthepromptboxlater!/viewviewclass=’dialog-footer’buttonclass=’dialog-btn’open-type=”getPhoneNumber”bindgetphonenumber=”getPhoneNumber”Toauthorize./button/view/view/viewwxss页面.dialog-mask{position:fixed;z-index:1000;top:0;right:0;left:0;bottom:0;background:rgba(0,0,0,0.3);}.dialog-info{position:fixed;z-index:5000;width:80%;max-width:600rpx;top:50%;left:50%;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);background-color:#FFFFFF;text-align:center;border-radius:3px;overflow:hidden;}.dialog-title{font-size:36rpx;padding:30rpx30rpx10rpx;}.dialog-content{padding:10rpx30rpx20rpx;min-height:80rpx;font-size:32rpx;line-height:1.3;word-wrap:break-word;word-break:break-all;color:#999999;}.dialog-footer{display:flex;align-items:center;position:relative;line-height:90rpx;font-size:34rpx;}.dialog-btn{display:block;-webkit-flex:1;flex:1;position:relative;color:#3CC51F;}js页面data:{userName:”,pwd:”,getUserInfoFail:”,userInfo:[],hasUserInfo:”,phone:”,config:{tipsshow1:true,tipsshow2:false}},/生命周期函数–监听页面加载/onLoad:function(options){varthat=this;//用户是否授权过手机号wx.getStorage({key:’phone’,success:function(res){that.setData({config:{tipsshow1:false,tipsshow2:false},})}})//是否授权过用户信息wx.getSetting({success:function(res){if(res.authSetting[‘scope.userInfo’]){//已经授权,可以直接调用getUserInfo获取头像昵称wx.getUserInfo({success:function(res){that.setData({userInfo:res.userInfo,config:{tipsshow1:false,},})}})}}})},getPhoneNumber:function(e){if(e.detail.errMsg==”getPhoneNumber:failuserdeny”)return;//用户允许授权wx.showLoading()varself=this//1.调用登录接口获取临时登录codewx.login({success:res={console.log(res,555)if(res.code){//2.访问登录凭证校验接口获取session_key、openidwx.request({url:”xxxxxxx/index/author/login”,data:{‘js_code’:res.code,},method:’GET’,//OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECTheader:{‘content-type’:’application/json’},//设置请求的headersuccess:function(data){console.log(data,data)if(data.statusCode==200){//3.解密wx.request({url:’xxxxxx/index/author/number’,data:{‘appid’:data.data.appid,’sessionKey’:data.data.session_key,’encryptedData’:e.detail.encryptedData,’iv’:e.detail.iv,},method:’GET’,//OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECTheader:{‘content-type’:’application/json’},//设置请求的headersuccess:function(data2){wx.hideLoading()console.log(data2.data.phoneNumber)if(data2.statusCode==200&&data2.data.phoneNumber){self.setData({phone:data2.data.phoneNumber,config:{tipsshow1:false,tipsshow2:false,},})wx.setStorageSync(‘phone’,data2.data.phoneNumber);if(self.data.userInfo!=”){wx.request({url:’xxxx/index/author/regist’,data:{username:self.data.userInfo.nickName,sex:self.data.userInfo.gender,phone:self.data.phone,pwd:123456,avatarimg:self.data.userInfo.avatarUrl},success:function(data){console.log(data.data,56565)if(data.data!=null){wx.showToast({title:’登录中…’,icon:’loading’,duration:2000})wx.navigateTo({url:’../managementList/managementList’//管理页面})}}});}console.log(self.data,526336)}},fail:function(err){console.log(err);}})}},fail:function(err){console.log(err);}})}}})},getUserInfo:function(e){varthat=this;console.log(e.detail.userInfo,”getuserinfo”)if(e.detail.userInfo){that.setData({userInfo:e.detail.userInfo,config:{tipsshow1:false,tipsshow2:true,},})console.log(that.data.userInfo);}else{console.log(“获取信息失败”)}},PHP后端?phpnamespaceappindexcontroller;usethinkController;useappadminmodelUserRecharge;usethinkDb;classAuthorextendsController{/发送HTTP请求方法@paramstring$url请求URL@paramarray$params请求参数@paramstring$method请求方法GET/POST@returnarray$data响应数据/functionhttpCurl($url,$params,$method=’POST’,$header=array(),$multi=false){date_default_timezone_set(‘PRC’);$opts=array(CURLOPT_TIMEOUT=30,CURLOPT_RETURNTRANSFER=1,CURLOPT_SSL_VERIFYPEER=false,CURLOPT_SSL_VERIFYHOST=false,CURLOPT_HTTPHEADER=$header,CURLOPT_COOKIESESSION=true,CURLOPT_FOLLOWLOCATION=1,CURLOPT_COOKIE=session_name().’=’.session_id(),);/根据请求类型设置特定参数/switch(strtoupper($method)){case’GET’://$opts[CURLOPT_URL]=$url.’?’.http_build_query($params);//链接后拼接参数&非?$opts[CURLOPT_URL]=$url.’?’.http_build_query($params);break;case’POST’://判断是否传输文件$params=$multi?$params:http_build_query($params);$opts[CURLOPT_URL]=$url;$opts[CURLOPT_POST]=1;$opts[CURLOPT_POSTFIELDS]=$params;break;default:thrownewException(‘不支持的请求方式!’);}/初始化并执行curl请求/$ch=curl_init();curl_setopt_array($ch,$opts);$data=curl_exec($ch);$error=curl_error($ch);curl_close($ch);if($error)thrownewException(‘请求发生错误:’.$error);return$data;}/微信信息解密@paramstring$appid小程序id@paramstring$sessionKey小程序密钥@paramstring$encryptedData在小程序中获取的encryptedData@paramstring$iv在小程序中获取的iv@returnarray解密后的数组/functiondecryptData($appid,$sessionKey,$encryptedData,$iv){$OK=0;$IllegalAesKey=-41001;$IllegalIv=-41002;$IllegalBuffer=-41003;$DecodeBase64Error=-41004;if(strlen($sessionKey)!=24){return$IllegalAesKey;}$aesKey=base64_decode($sessionKey);if(strlen($iv)!=24){return$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$IllegalBuffer;}if($dataObj-watermark-appid!=$appid){return$DecodeBase64Error;}$data=json_decode($result,true);return$result;}/请求过程中因为编码原因+号变成了空格需要用下面的方法转换回来/functiondefine_str_replace($data){returnstr_replace(”,’+’,$data);}//获取手机号publicfunctionnumber($appid,$sessionKey,$encryptedData,$iv){include_once(ROOT_PATH.”./public/author/wxBizDataCrypt.php”);//引入wxBizDataCrypt.php文件$appid=$appid;$sessionKey=$sessionKey;$encryptedData=$encryptedData;$iv=$iv;$data=”;$pc=newWXBizDataCrypt($appid,$sessionKey);//注意使用进行转义$errCode=$pc-decryptData($encryptedData,$iv,$data);if($errCode==0){print($data.””);}else{print($errCode.””);}}//微信登录publicfunctionlogin(){$get=input(‘get.’);$param[‘appid’]=’xxxxxxxxxx’;//小程序id$param[‘secret’]=’xxxxxxxxxx’;//小程序密钥$param[‘js_code’]=$this-define_str_replace($get[‘js_code’]);$param[‘grant_type’]=’authorization_code’;$http_key=$this-httpCurl(‘https://api.weixin.qq.com/sns/jscode2session’,$param,’GET’);$session_key=json_decode($http_key,true);//获取openid和session_key//print_r(http_build_query($param));if(!empty($session_key[‘session_key’])){$data[‘appid’]=$param[‘appid’];$data[‘session_key’]=$session_key[‘session_key’];returnjson($data);}else{echo’获取session_key失败!’;}}//用户注册publicfunctionregist($username=””,$sex=””,$phone=””,$password=””,$avatarimg=””){if($phone){//判断该用户是否已经注册$userdata=Db::name(‘user’)-where(‘phone’,$phone)-find();if($userdata){returnjson_encode(2);}//整合数组$salt=’1122′;$password=Md5(Md5($password).$salt);$data=[‘name’=$username,’sex’=$sex,’phone’=$phone,’password’=$password,’avatarimg’=$avatarimg,’logtime’=date(“Y-m-dH:i:s”),’addTime’=date(“Y-m-dH:i:s”)];//注册新用户$userid=db(‘user’)-insertGetId($data);if($userid){returnjson_decode(1);}else{returnjson_encode(0);}}}}wxBizDataCrypt.php文件下载地址:提取码为yzhl或者扫描下方二维码

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信获取用户信息,微信小程序获取用户信息及手机号后端TP5.0
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏