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

前端之家收集整理的这篇文章主要介绍了小程序如何获取手机号( thinkphp3.2.3框架)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本篇文章给大家带来的内容是关于小程序如何获取手机号( thinkPHP3.2.3框架),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1、直接上代码PHP

  1. namespace Home\Controller;
  2. use Think\Controller;
  3. class ApiController extends Controller {
  4. /**
  5. * error code 说明.
  6. *
          • -41001: encodingAesKey 非法
            • -41003: aes 解密失败
              • -41004: 解密后得到的buffer非法
                • -41005: base64加密失败
                  • -41016: base64解密失败
                    • */
                    • 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 ='';

                    • // <a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>openId session-key 等
                    • public function getopenId($value=&#39;&#39;)
                    • {   
                    •     $code = I(&#39;post.code&#39;);
                    •     $appid = self::$appid;
                    •     $secret = self::$secret;
                    •     $url = &#39;https://api.weixin.qq.com/sns/jscode2session?appid=&#39;. $appid.&#39;&amp;secret=&#39;.$secret.&#39;&amp;js_code=&#39;.$code.&#39;&amp;grant_type=authorization_code&#39;;
                    •     $result = httpGet($url);
                    •     $res = json_decode($result);
                    •     // session([&#39;sessionKey&#39;=>$res,&#39;expire&#39;=>7200]);
                    •     $this->ajaxReturn($res);
                    • }
                    • // <a href="/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="/tag/xiaochengxu/" target="_blank" class="keywords">小程序</a>手机号api 接口,对应下面<a href="/tag/xiaochengxu/" target="_blank" class="keywords">小程序</a> js
                    • public function <a href="/tag/getPhoneNumber/" target="_blank" class="keywords">getPhoneNumber</a>($value=&#39;&#39;)
                    • {   
                    •    $encryptedData = I(&#39;get.encryptedData&#39;);
                    •    $iv = I(&#39;get.iv&#39;);
                    •    $this->sessionKey=I(&#39;get.session_key&#39;);
                    •    $res = $this->decryptData($encryptedData,$iv);
                    •    // $res = json_decode($res);
                    •    if($res->phoneNumber){
                    •         // $res->phoneNumbe 就是手机号可以 写入<a href="/tag/shujuku/" target="_blank" class="keywords">数据库</a>或者做其他操作
                    •    }
                    •    $this->ajaxReturn([&#39;msg&#39;=>$res,&#39;status&#39;=>&#39;1&#39;]); //把手机号返回
                    • }
                    • // <a href="/tag/xiaochengxu/" target="_blank" class="keywords">小程序</a>解密
                    • 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,CURLOPT_URL,$url);

                    • $res = curl_exec($curl);
                    • curl_close($curl);
                    • return $res;
                    • }

                    • }

                    //2、小程序

                    2.1在app.js 启动页面里先login

                    1. // 登录
                    2. // if (!wx.getStorageSync('session_key') || wx.getStorageSync('time') < Date.parse(new Date())){ // 判断session_key是不是存在获者过期
                    3. wx.login({
                    4. success: res => {
                    5. console.log(res)
                    6. // 发送 res.code 到后台换取 openId,sessionKey,unionId
                    7. wx.request({
                    8. 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) {
                    9. console.log(res.data);
                    10. wx.setStorageSync('openid',res.data.openid)
                    11. wx.setStorageSync('session_key',res.data.session_key)
                    12. wx.setStorageSync('time',parseInt(Date.parse(new Date())) + 7200)
                    13. }
                    14. })

                    15.     }
                    16.   })</pre><p>//2.2 在<a href="/tag/xiaochengxu/" target="_blank" class="keywords">小程序</a>模板里写组件</p><pre ><button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" >//这是官方的组件点击会弹出授权<a href="/tag/yemian/" target="_blank" class="keywords">页面</a></pre><p>在js里写下面的<a href="/tag/hanshu/" target="_blank" class="keywords">函数</a></p><pre ><a href="/tag/getPhoneNumber/" target="_blank" class="keywords">getPhoneNumber</a>: function (e) {
                    17. var that =this;
                    18. var session_key = wx.getStorageSync(&#39;session_key&#39;)
                    19. if (e.detail.errMsg == &#39;<a href="/tag/getPhoneNumber/" target="_blank" class="keywords">getPhoneNumber</a>:fail user deny&#39;) {
                    20.   wx.showModal({
                    21.     title: &#39;<a href="/tag/tishi/" target="_blank" class="keywords">提示</a>&#39;,showCancel: false,content: &#39;未授权&#39;,success: function (res) { }
                    22.   })
                    23. } else {//确认授权
                    24.   wx.request({
                    25.     url: &#39;https://www.showoow.com/index.<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>?m=mini&amp;c=api&amp;a=<a href="/tag/getPhoneNumber/" target="_blank" class="keywords">getPhoneNumber</a>&amp;openid=&#39; + wx.getStorageSync(&#39;openid&#39;),//openid是app.js 已经存的
                    26.     header: {"Content-Type": "application/x-www-form-urlencoded" },method: "get",data: {
                    27.       encryptedData: e.detail.encryptedData,iv: e.detail.iv,session_key:session_key
                    28.     },success:function(res){
                    29.       if (res.data.msg.phoneNumber){
                    30.         console.log(res);
                    31.         wx.showModal({
                    32.           title: &#39;<a href="/tag/tishi/" target="_blank" class="keywords">提示</a>&#39;,content: &#39;授权成功&#39;,success: function () {
                    33.             wx.setStorageSync(&#39;phoneNumber&#39;,res.data.msg.phoneNumber);
                    34.             var time = Date.parse(new Date()) + 60 * 60 * 24 * 2
                    35.             wx.setStorageSync(&#39;exp&#39;,time );
                    36.           }
                    37.         })
                    38.         setTimeout(function(){
                    39.           wx.navigateTo({
                    40.             url: &#39;/pages/form/form&#39;,})
                    41.         },1500);
                    42.         that.setData({
                    43.           show:&#39;show&#39;,hiden:&#39;&#39;
                    44.         })
                    45.       }else{
                    46.         wx.showToast({
                    47.           title: &#39;授权失败&#39;,icon:&#39;loading&#39;
                    48.         })
                    49.       }
                    50.     },fail:function(){
                    51.       wx.showToast({
                    52.         title: &#39;授权失败&#39;,icon: &#39;loading&#39;
                    53.       })
                    54.     }
                    55.   })
                    56. }
                    57. },

                    到此小程序获取手机号完结,在公司写了个小程序,可以正常获取手机号

                    猜你在找的ThinkPHP相关文章