分享在用的微信公众号扫码登录方法

浏览:1070 发布日期:2019/02/19 分类:技术分享
首先要有一个已认证公众号
用到的公众号类库

我采用的是SOCKET.IO来发送扫码的状态,使用redis缓存标识信息 的具体的可以自己修改只是大概分享一下流程
https://github.com/dodgepudding/wechat-php-sdk

获取登录二维码public function qrcode(Request $request){
        $uuid = $request->param("uuid");//客户端的UUID标识
        $upid = $request->param("upid");
        $loginId = "Login_".$uuid;
        $weObj = new Wechat(Config::get("wechat."));
        $tick = $weObj->getQRCode($loginId,3,300);
        if(!$tick){
            return Result::sendJson(0,"登录二维码获取失败,请刷新重新获取");
        }
        $qr = $weObj->getQRUrl($tick['ticket']);
        //$qr = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFp7jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyTUppZU5TeERkU2wxaWhWeGhzY0YAAgRVOWFcAwQ8AAAA";
        $data = json_encode(['uuid'=>$uuid,'ip'=>request()->ip(),'upid'=>$upid>0?$upid:0]);
        if(setRedis($loginId,$data,300)){
            $json['code'] = 200;
            $json['msg'] = "";
            $json['qrcode'] = $qr;//二维码地址
        }else{
            $json['code'] = 0;
            $json['msg'] = "登录码获取失败";
        }
        return json($json);
    }
登录流程实现扫码登录未关注的关注后自动登录
这个是对接公众号开发者的控制器class Wechat
{
    private function getToken($userinfo,$ip){
        $key = Config::get("setting.");
        $key = $key['tokenKey'];
        $nowtime = time();
        $token = [
            'iss' => 'http://www.helloweba.net', //签发者
            'aud' => 'http://www.helloweba.net', //jwt所面向的用户
            'iat' => $nowtime, //签发时间
            'nbf' => $nowtime, //在什么时间之后该jwt才可用
            'exp' => $nowtime + 3600*24, //过期时间-10min
            'data' => [
                'userid' => $userinfo['id'],
                'mch_id' => $userinfo['mch_id'],
                'ip'=>$ip
            ]
        ];
        $jwt = JWT::encode($token, $key);
        return $jwt;
    }
    private function hanleLogin($weObj,$key,$openid){
        $arr = getRedis($key);
        if($arr){
            $cacheData = json_decode($arr,true);
            //判断是否已绑定如果已绑定则读取相关信息缓存,否则创建新商户
            //$uid = WechatModel::where('openid',$openid)->value('uid');
            $wechatRs = WechatModel::get(['openid' =>$openid]);
            $uid = $wechatRs['uid'];
            $weid = $wechatRs['id'];
            if(!$uid){
                //创建商户
                $mchRs = [
                    这里创建新用户具体
                ];
                $user = Member::create($mchRs);
                $uid = $user->id;
                $mchRs['id'] = $uid;
                WechatModel::where('openid',$openid)->update(['uid'=>$uid]);
            }else{
                $mchRs = Member::where(['id'=>$uid])->field("id,mch_id")->find();
                $logindata['loginip'] = $cacheData['ip'];
                $logindata['logintime'] = time();
                Member::where(['id'=>$uid])->update($logindata);
            }
            delCach("redis",$key);
            $sendJosn = [
                'type' => 'login',
                'msg' => '登录成功',
                'token'=>$this->getToken($mchRs,$cacheData['ip']),
                'sign' =>md5($mchRs['mch_id']."123456"),
                'mch_id'=>$mchRs['mch_id']
            ];
            sendSocketTo($cacheData['uuid'],json_encode($sendJosn));//socket.io发送登录成功给客户端
            $weObj->text("登录成功")->reply();
        }
    }
    private function hanleOpenid($weObj,$openid){
        $WechatModel = new WechatModel();
        if(!$WechatModel->where(['openid'=>$openid])->value("id")){
            $userinfo = $weObj->getUserInfo($openid);
            if($userinfo){
                $userinfo['time'] = time();
                $WechatModel->data($userinfo);
                $WechatModel->allowField(true)->save();
            }
        }
    }
    public function index(Request $request){
        $weObj = new WxApi(Config::get("wechat."));
        $weObj->valid();
        $type = $weObj->getRev()->getRevType();
        $openid = $weObj->getRev()->getRevFrom();
        try{
            switch($type) {
                case WxApi::MSGTYPE_TEXT:
                    break;
                case WxApi::MSGTYPE_EVENT:
                    $eveArr = $weObj->getRev()->getRevEvent();

                    if($eveArr['event'] == "SCAN"){
                        $eveKey = $eveArr['key'];
                        if(strpos($eveKey,'Login_') !== false){
                            $this->hanleOpenid($weObj,$openid);//判断数据库是否有新用户
                            $this->hanleLogin($weObj,$eveKey,$openid);
                        }
                    }else{
                        if($eveArr['event'] =="subscribe" && strpos($eveArr['key'],'Login_') !== false){
                            $keystr = explode("qrscene_",$eveArr['key']);
                            $this->hanleOpenid($weObj,$openid);//判断数据库是否有新用户
                            $this->hanleLogin($weObj,$keystr[1],$openid);
                        }else{
                            file_put_contents("data.txt",json_encode($eveArr));
                            $weObj->text("欢迎您注册易码支付,如未登录请重新扫码")->reply();
                        }
                    }
                    //判断是否为登录事件如果是则设置缓存标识为登录状态
                    break;
            }
        }catch (Exception $e){
            file_put_contents("wxError",json_encode($e));
        }

    }
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>