1、uniapp前端
-
- //授权手机号
- decryptPhoneNumber (e) {
- let uids = uni.getStorageSync('UID');
- let data = {uid:uids,code:e.detail.code}
- manghe_phoen(data).then(res => {
- this.user = res.data;
- });
- console.log(e.detail.code)
- },
2、php后台
- /**
- *授权手机号
- */
- public function manghe_phoen(Request $reques){
- $appid = '';
- $secret = '';
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
-
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_HEADER,0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- $res = curl_exec($ch);
- curl_close($ch);
-
- $tmptoken = json_decode($res);
- $token = $tmptoken->access_token;
-
- $uid = $reques->get("uid");
- $code = $reques->get("code");
- $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$token";
- $data['code'] = $code;
- $date = $this->http_request($url,json_encode($data),'json');
- return json(['status'=>200,'data'=>$date,'msg'=>'ok']);
- }
-
- public function http_request($url, $data = null)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
-
- if (!empty($data)) {
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json'
- ));
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
- $output = curl_exec($curl);
- curl_close($curl);
-
- return $output;
- exit();
-
- }