小程序获取手机号

页面按钮

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">授权手机号</button>

js文件

getPhoneNumber: function (e) {
    wx.showLoading({
      title: '正在授权',
    })
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: api.login_getphone,
        data: {
          openid: that.data.openid,
          code:e.detail.code,
        },
        method: "post",
        success: function (res) {
          if(res.data.status == '200'){
            wx.redirectTo({
              url: '/pages/index/index',
            })
            app.data.phone=true
          }else{
            wx.showToast({
              title: res.msg,
              icon: 'none', 
              duration: 2000
            })
          }
        }
      })
    }
    wx.hideLoading()
  },

php文件

//获取用户手机号
    public function getphone(){
        $code = input('code');
        $openid = input('openid');
        $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$this->access_token();
        $arr = json_encode(array('code'=>$code));
        $data = json_decode($this->httpRequest($url,$arr,'POST'),true);
        if($data['errcode'] == 0){
            $phone = $data['phone_info']['phoneNumber'];
            $user = Users::where('openid',$openid)->find();
            $user->phone = $phone;
            $user->save();
            return json(['status'=>'200']);
        }else{
            return json(['status'=>'201','msg'=>'获取失败']);
        }
    }
    function httpRequest($url,$data='',$method='GET'){
        try{
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL,$url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
            //        curl_setopt($curl, CURLOPT_USERAGENT,CURLOPT_USERAGENT);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
            if($method=='POST')
            {
                curl_setopt($curl, CURLOPT_POST, 1);
                if ($data !='')
                {
                    curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
                }
            }

            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($curl);
            curl_close($curl);
            return $result;
        }catch(\Exception $e){
            abort(config('error_log_code'),$e->getMessage());
//            var_dump($e->getMessage());die;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值