微信小程序如何获取手机号

2025-10-25 04:01:02

1、首先为了方便理解,附上一张小程序获取用户手机号码的流程图。

微信小程序如何获取手机号

2、按照上图所示,需要先获取用户的登陆凭证, 下面的这个代码是使用小程序登录接口以后在获取的,其中wx.request()是微信官方的API,它的作用是发送网络请求到后端,然后在后端用code换区session_key和openid;

相关代码如下:

wx.login({

    success: function (res) {

      if (res.code) { //使用小程序登录的接口完成后端用户登录

        wx.request({

          url: app.d.hostUrl + 'getOpenid',//你自己相关api接口的路径

          data: {

            code: res.code,

            appid: "小程序AppID",

            secret: "小程序secret",

          },

          success: function (res) {

            //将openid保存到缓存里

            wx.setStorageSync("openid", res.openid);

            wx.setStorageSync("session_key", res.session_key);

          }

        })

      } else {

        console.log('获取用户登录态失败!' + res.errMsg)

      }

    }

  })。

微信小程序如何获取手机号

3、现在前端已经将获取的openid和session_key所需的参数传递给后端了,在后端写入相关代码:

//用code换session_key和openid

public function getOpenid(){

global $_GPC;//这是微擎框架 $_GPC想当于$_GET和$_POST

$code = $_GPC['code'];

$appid = $_GPC['appid'];

$secret = $_GPC['secret']; 

$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";

    $str = $this->httpGet($api);

        return $str;

}

private function httpGet($url){

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($curl, CURLOPT_TIMEOUT, 500);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);

        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);

        curl_close($curl);

        return $res;

}。

微信小程序如何获取手机号

4、这时前端success就可以取到openid和session_key了,那么就需要在后端用session_key和openid来获取解密手机号码了;

相关代码:

public function getPhoneNumber(){

//这是解密手机号码的接口,等会前端还要写个请求访问这个接口拿到手机号码

global $_GPC;

require_once dirname(__FILE__) . '/mail/WXBizDataCrypt.php';

$appid = $_GPC['appid'];

        $sessionKey = $_GPC['session_key'];

        $encryptedData = $_GPC['encryptedData'];

        $iv = $_GPC['iv'];

        $pc = new WXBizDataCrypt($appid, $sessionKey);

$errCode = $pc->decryptData($encryptedData, $iv, $data );

if ($errCode == 0) {

    print($data . "\n");

} else {

    print($errCode . "\n");

}

}。

微信小程序如何获取手机号

5、在小程序的wxml文件中写一个button按钮来获取用户手机号码;

相关代码:

<button bindgetphonenumber='getPhoneNumber'> 使用微信登录 </button>。

微信小程序如何获取手机号

6、然后在js代码中写好获取代码就可以了;

相关代码:

getPhoneNumber:function (e) {

    var detail = e.detail;

    wx.request({

      url: app.d.hostUrl + 'getPhoneNumber',  //解密手机号码接口

      data: {

        "appid": app.d.appId,

        "session_key": wx.getStorageSync('session_key'),

        "encryptedData": detail.encryptedData,

        "iv": detail.iv

      },

      success: function (res) {

        console.log(res.data.phoneNumber);

        wx.setStorageSync("phonenumber", res.data.phoneNumber);

        

      }

    })

  }。

微信小程序如何获取手机号

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢