1.客户端生成一个uuid请求服务端

2.服务端保存uuid到redis服务器,并设置过期时间,然后使用该uuid生成二维码并返回

3.客户端展示二维码,并设置ajax定时请求服务端判断是否登录

4.手机APP扫码,获取uuid,将uuid和自身用户信息传递给服务端登录接口

5.服务端判断uuid是否被使用以及过期,然后将uuid和用户信息保存,将uuid作为客户端的token

6.客户端请求到成功登录,并获取到用户信息

 /**
     * 生成登陆二维码信息,存储客户端发过来的token
     * @url app/{controller}/qrCode
     * @method POST
     * @param string client_token
     * @return json
     */
    public function qrCode($data = null){
        if (is_string($data)) $data = json_decode($data, true);
        //参数验证
        $validate = Validate::make([
            'client_token' => 'require|min:1|max:100',
        ]);
        if (!$validate->check($data)) {
            $msg = $validate->getError();
            return wx_errorReturn(null, -1, $msg);
        }

        //将客户端发来的token存入redis
        $redis = Cache::store('redis')->handler();
        $prefix = config('const.redis_prefix');
        $key = $prefix['c_user_token'].$data['client_token'];
        $redis->Set($key,'');
        $redis->expire($key,120);//设置token,2分钟过期
        return wx_successReturn(['url'=>config('app.app_host').'/app/user/smlogin']);//二维码也交给前端生成了
    }
  /**
     * 扫码登陆,手机获取到client_token,将client_token和用户的token一起传过来
     */
    public function smLogin($data = null){
        if (is_string($data)) $data = json_decode($data, true);
        //参数验证
        $validate = Validate::make([
            'client_token' => 'require|min:1|max:100',
            'token' => 'require'
        ]);
        if (!$validate->check($data)) {
            $msg = $validate->getError();
            return wx_errorReturn(null, -1, $msg);
        }

        //将客户端token和用户id信息存入redis
        $user = getAppUserInfo($data['token']);
        $redis = Cache::store('redis')->handler();
        $prefix = config('const.redis_prefix');
        $token_key = $prefix['c_user_token'].$data['client_token'];
        //同一个二维码信息只能用一次
        if($redis->get($token_key)) return wx_errorReturn(null,-1,'二维码已失效');
        $user_key = $prefix['c_user'].$user['id'];
        $redis->set($token_key,$user['id']);  //token --> id
        $redis->hmset($user_key,$user); //id --> user
        $redis->expire($token_key,86400);//设置token,1天过期
        $redis->expire($user_key,86400);//设置token,1天过期
        return wx_successReturn(null,'登录成功');
    }
 /**
     * 客户端定时请求该接口判断是否登录
     */
    public function isLoginClient($data = null){
        if (is_string($data)) $data = json_decode($data, true);
        //参数验证
        $validate = Validate::make([
            'client_token' => 'require|min:1|max:100',
        ]);
        if (!$validate->check($data)) {
            $msg = $validate->getError();
            return wx_errorReturn(null, -1, $msg);
        }
        $redis = Cache::store('redis')->handler();
        $prefix = config('const.redis_prefix');
        $token_key = $prefix['c_user_token'].$data['client_token'];
        //判断token_key是否过期
        $exists = $redis->exists($token_key);
        if(!$exists) return wx_errorReturn(null,-1,'二维码过期');
        $user_id = $redis->get($token_key);
        $user_key = $prefix['c_user'].$user_id;
        $user = $redis->hGetAll($user_key);
        return wx_successReturn(['user_info' => $user],'登录成功');
    }

 

posted on 2019-10-29 16:39  ljstu  阅读(855)  评论(0编辑  收藏  举报