微信小程序一键获取手机号码

需求要做一个一键获取手机号码的功能

<view class="gh_item">
              <view class="gh_text">
                联系方式<view class="xing">*</view>
              </view>
              <input class="phone" name="phone" value="{{phoneNumber}}" type="number" maxlength="11"  placeholder="请输入手机号码(必填)" />
              <button class="getphone" size="min" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">一键输入</button>
          </view>

微信wxml代码如上,定义一个按钮执行

/**
   * 页面的初始数据
   */
    data: {
        phoneNumber:"",
    },
    onLoad(){

    },
    /**
     * 一键获取手机号码
     * @param {*}
     */
    getPhoneNumber (e) {
      if (e.detail.errMsg === 'getPhoneNumber:ok') {
      //console.log(e.detail.code); //最新官方文档里是这个参数,很重要;
        request({ url: "/phone/getPhoneNumber",data:{code:e.detail.code}})
        .then(result => {
          this.setData({
            phoneNumber: result.data.data.phoneNumber
          })
        })

      }
    },

url是你自己的接口代码

我自己的接口路径

/*获取access_token,不能用于获取用户信息的token*/
  public  function getAccessToken()
    {
        $appid = '你的appid';
        $secret = '你的AppSecret';

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全    校验。
        // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
    //图片合法性验证
    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();

    }
    //  获取手机号
    public function getPhoneNumber(){
        $tmp = $this->getAccessToken();
        $tmptoken = json_decode($tmp);
        $token = $tmptoken->access_token;
        $data['code'] = $_GET['code'];//前端获取code

        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$token";
        $info = $this->http_request($url,json_encode($data),'json');
        // 一定要注意转json,否则汇报47001错误
        $tmpinfo = json_decode($info);

        $code = $tmpinfo->errcode;
        $phone_info = $tmpinfo->phone_info;
        //手机号
        $phoneNumber = $phone_info->phoneNumber;
        if($code == '0'){
            return $this->success('请求成功',['phoneNumber'=>$phoneNumber]);
        }else{
            return $this->error('请求失败');
        }

    }

 

posted @ 2025-07-24 15:15  冷晨  阅读(50)  评论(0)    收藏  举报