小程序之Token的获取(PHP)

token是在输入了用户名和密码之后获取的,利用这个token你可以拥有查看或者操作相应的资源的权限。

接下来直接上代码,里面有详细注释。

controller/token
goCheck();
        $ut = new UserToken();
        $token = $ut->get($code);
        return [
            'token' => $token
        ];
    }
}
service/Token
header('token');
        $vars = Cache::get($token);
        if(!$vars){
            throw new TokenException();
        }
        else{
            if(!is_array($vars)){
                $vars = json_decode($vars,true); // 字符串变数组
            }
            if(array_key_exists($key,$vars)){
                return $vars[$key];
            }
            else{
                throw new Exception('尝试获取的token变量并不存在');
            }
        }
    }
    //
    public static function getCurrentUid(){
        // token
        $uid = self::getCurrentTokenVar('uid');
        return $uid;
    }
}
service/UserToken
code = $code;
        $this->wxAppID = config('wx.app_id');
        $this->wxAppSecret = config('wx.app_secret');
        //sprintf函数   把url中的s% 替换成想要的数据
        $this->wxLoginUrl = sprintf(config('wx.login_url'),$this->wxAppID,$this->wxAppSecret,$this->code);
    }

    public function get()
    {
        $result = curl_get($this->wxLoginUrl);
        $wxResult = json_decode($result, true);//result 变成json字符串
        if (empty($wxResult)) {
            throw new Exception('获取session_key及openID时异常,微信内部错误');
        } else {
            $loginFail = array_key_exists('errcode', $wxResult);
            if ($loginFail) {
                $this->processLoginError($wxResult);
            }
            else {
                return $this->grantToken($wxResult);
            }
        }
    }

    //封装一个成功接收code的方法
    private function grantToken($wxResult){
        //拿到openID
        //数据库里看一下,openID是否存在
        //如果存在则不处理,不存在新增一条记录
        //生成令牌,准备缓存数据,写入缓存
        //把令牌返回到客户端
        // key 令牌
        // value  :wxResult,uid,scope
        $openid = $wxResult['openid'];
        $user = UserModel::getByOpenID($openid);
        if($user){
            $uid = $user->id;
        }
        else{
            $uid = $this->newUser($openid);
        }
        $cachedValue = $this->preparCacheValue($wxResult,$uid);
        $token = $this->saveToCache($cachedValue);
        return $token;
    }


    // 写入缓存
    private function saveToCache($cachedValue){
        $key = self::generateToken();
        $value = json_encode($cachedValue);//把数组转换成字符串
        $expire_in = config('setting.token_expire_in');

        $request = cache($key,$value,$expire_in);
        if(!$request){
            throw new TokenException([
                'msg' => '服务器缓存异常',
                'errorCode' => 10005
            ]);
        }
        return $key;
    }
    //准备value的一系列数据
    private function preparCacheValue($wxResult,$uid){
        $cachedValue = $wxResult;
        $cachedValue['uid'] = $uid;
        $cachedValue['scope'] = 16;  //权限高低2,4,6,8,12,14,16
        return $cachedValue;
    }
    //openID不存在  则新增一条记录
    private function newUser($openid){
        $user = UserModel::create([
            'openid' => $openid
        ]);
        return $user->id;
    }
    //封装一个抛出异常的方法
    private function processLoginError($wxResult){
        throw new WeChatException([
            'msg' => $wxResult['errmsg'],
            'errcode' => $wxResult['errcode']
        ]);
    }

}
validate/BaseValidate
param();

        $result = $this->batch()->check($params);
        if(!$result){
            $ex = new ParameterException([
                'msg' => $this->error,
            ]);
            throw $ex;
        }
        else{
            return true;
        }
    }

    protected function isPositiveInteger($value,$rule = '',$data = '',$field = ''){
        if(is_numeric($value) && is_int($value+0) && ($value+0) > 0){
            return true;
        }
        else{
            return false;
//            return $field."必须是正整数";
        }
    }

    protected function isNotEmpty($value,$rule = '',$data = '',$field = ''){
        if(empty($value)){
            return false;
        }
        else{
            return true;
        }
    }
}

validate/TokenGet
 'require|isNotEmpty'
    ];
    protected $message = [
        'code' => '没有code还想获取Token,做梦呢'
    ];
}







你可能感兴趣的:(thinkPHP)