美文网首页
微信小程序-登陆流程-获取openId

微信小程序-登陆流程-获取openId

作者: 韩小禹 | 来源:发表于2019-01-15 15:25 被阅读0次

截图

QQ截图20190115152423.png QQ截图20190115152456.png QQ截图20190115152541.png

页面

<!-- 部分代码 -->
<view class="exercise_btn">
        <button open-type='getUserInfo' lang="zh_CN" bindgetuserinfo="onGotUserInfo">立即体验</button>
    </view>

JS

//index.js写在Page中
onGotUserInfo: function (res) {
        var url = 'https://***.***.com/login/login.php';
        var that = this;
        if (res.detail.userInfo) {
            var info = res.detail.userInfo;
            wx.login({
                success : function(res){
                    if(res.code){
                        wx.getUserInfo({
                            withCredentials: true,
                            success : function(res_user){
                                var params = {
                                    code: res.code,
                                    encryptedData: res_user.encryptedData,
                                    iv: res_user.iv,
                                    signature: res_user.signature,
                                    rawData: res_user.rawData
                                }
                                console.log(params)

                                wx.request({
                                    url: url,
                                    data: params,
                                    header: { 'content-type': 'application/json'},
                                    success : function(res){
                                        console.log(res);
                                        console.log(res.data['openId'])
                                    }
                                })
                            }
                        })
                    }
                }
            })
        } else {
            console.log("用户拒绝授权1")
        }
    }

PHP后台

<?php
    
    class wxLogin
    {
        public $appid = '你的appid';
        public $appsecret = '你的appsecret';

        public function login($params)
        {
            $appid = $this->appid;
            $appsecret = $this->appsecret;
            $code = $params['code'];
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=".$code."&grant_type=authorization_code";
            
            $arr = $this->wxget($url);
            $arr = json_decode($arr, true);
            $openid = $arr['openid'];
            $session_key = $arr['session_key'];

            $signature = $params['signature'];
            $rawData = $params['rawData'];
            $signature2 = sha1($rawData . $session_key);

            if($signature != $signature2) {
                return json(['code' => 500, 'msg' => '数据签名验证失败!']);
            }

            // 根据你得后台框架修改对应得路径信息
            require_once dirname(__FILE__).'/wxBizDataCrypt.php';

            $encryptedData = $params['encryptedData'];
            $iv = $params['iv'];
            $pc = new WXBizDataCrypt($appid, $session_key);

            //这里的$data或许会有点迷惑,因为上面并没有定义这个变量,先不要考虑这里,里面只管写这个变量就行
            $errCode = $pc->decryptData($encryptedData, $iv, $data); //其中$data包含用户的所有数据
            $data = json_decode($data);

            if ($errCode == 0) {
               //这里只可以用echo ,如果使用return则前端小程序获取不到
               echo json_encode($data);exit;
            }else{
               echo $errCode;
            }
        }

        public function wxget($url)
        {
            $info=curl_init();
            curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($info,CURLOPT_HEADER,0);
            curl_setopt($info,CURLOPT_NOBODY,0);
            curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($info,CURLOPT_URL,$url);
            $output= curl_exec($info);
            curl_close($info);
            return $output;
        }
    }

    $login = new wxLogin;
    $params = $_GET;
    $login->login($params);
?>

相关文章

网友评论

      本文标题:微信小程序-登陆流程-获取openId

      本文链接:https://www.haomeiwen.com/subject/gzrydqtx.html