您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

小程序如何获取手机号( thinkphp3.2.3框架)

2025/6/4 19:34:31发布16次查看
本篇文章给大家带来的内容是关于小程序如何获取手机号(  thinkphp3.2.3框架),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、直接上代码php
namespace home\controller;use think\controller;class apicontroller extends controller { /** * error code 说明. * <ul> * <li>-41001: encodingaeskey 非法</li> * <li>-41003: aes 解密失败</li> * <li>-41004: 解密后得到的buffer非法</li> * <li>-41005: base64加密失败</li> * <li>-41016: base64解密失败</li> * </ul> */ public static $ok = 0; public static $illegalaeskey = -41001; public static $illegaliv = -41002; public static $illegalbuffer = -41003; public static $decodebase64error = -41004; // 小程序 public static $appid = 'xxx'; //小程序appid public static $secret = 'xxx'; //小程序秘钥 public $sessionkey =''; // 获取openid session-key 等 public function getopenid($value='') { $code = i('post.code'); $appid = self::$appid; $secret = self::$secret; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='. $appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code'; $result = httpget($url); $res = json_decode($result); // session(['sessionkey'=>$res,'expire'=>7200]); $this->ajaxreturn($res); } // 获取小程序手机号api 接口,对应下面小程序 js public function getphonenumber($value='') { $encrypteddata = i('get.encrypteddata'); $iv = i('get.iv'); $this->sessionkey=i('get.session_key'); $res = $this->decryptdata($encrypteddata, $iv); // $res = json_decode($res); if($res->phonenumber){ // $res->phonenumbe 就是手机号可以 写入数据库或者做其他操作 } $this->ajaxreturn(['msg'=>$res,'status'=>'1']); //把手机号返回 } // 小程序解密 public function decryptdata($encrypteddata, $iv) { if (strlen($this->sessionkey) != 24) { return self::$illegalaeskey; } $aeskey=base64_decode($this->sessionkey); if (strlen($iv) != 24) { return self::$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 self::$illegalbuffer; } if( $dataobj->watermark->appid != self::$appid ) { return self::$illegalbuffer; } return $dataobj; // return self::$ok; }function httpget($url) { $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;}}
//2、小程序
2.1在app.js 启动页面里先login
// 登录 // if (!wx.getstoragesync('session_key') || wx.getstoragesync('time') < date.parse(new date())){ // 判断session_key是不是存在获者过期 wx.login({ success: res => { console.log(res) // 发送 res.code 到后台换取 openid, sessionkey, unionid wx.request({ url: 'https://www.zhixiaobing.com/index.php?m=&c=api&a=getopenid', header: { "content-type": "application/x-www-form-urlencoded" }, method: 'post', data: { code: res.code }, success: function (res) { console.log(res.data); wx.setstoragesync('openid', res.data.openid) wx.setstoragesync('session_key', res.data.session_key) wx.setstoragesync('time', parseint(date.parse(new date())) + 7200) } }) } })
//2.2 在小程序模板里写组件
<button open-type="getphonenumber" bindgetphonenumber="getphonenumber" >//这是官方的组件点击会弹出授权页面
在js里写下面的函数
getphonenumber: function (e) { var that =this; var session_key = wx.getstoragesync('session_key') if (e.detail.errmsg == 'getphonenumber:fail user deny') { wx.showmodal({ title: '提示', showcancel: false, content: '未授权', success: function (res) { } }) } else {//确认授权 wx.request({ url: 'https://www.showoow.com/index.php?m=mini&c=api&a=getphonenumber&openid=' + wx.getstoragesync('openid'), //openid是app.js 已经存的 header: {"content-type": "application/x-www-form-urlencoded" }, method: "get", data: { encrypteddata: e.detail.encrypteddata, iv: e.detail.iv, session_key:session_key }, success:function(res){ if (res.data.msg.phonenumber){ console.log(res); wx.showmodal({ title: '提示', showcancel: false, content: '授权成功', success: function () { wx.setstoragesync('phonenumber', res.data.msg.phonenumber); var time = date.parse(new date()) + 60 * 60 * 24 * 2 wx.setstoragesync('exp', time ); } }) settimeout(function(){ wx.navigateto({ url: '/pages/form/form', }) },1500); that.setdata({ show:'show', hiden:'' }) }else{ wx.showtoast({ title: '授权失败', icon:'loading' }) } }, fail:function(){ wx.showtoast({ title: '授权失败', icon: 'loading' }) } }) } },
到此小程序获取手机号完结,在公司写了个小程序,可以正常获取手机号
相关推荐:
微信小程序传递参数以及接收数据的方法
小程序中页面兼容h5标签的解析
以上就是小程序如何获取手机号( thinkphp3.2.3框架)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product