Commit 9f676bb2 by zhufx

从redis获取用户信息

parent 21c7d3a0
......@@ -12,6 +12,8 @@ class AuthError
const TOKEN_ERROR = ["msg" => "token写入失败", "code" => 500, "errorCode" => 25100];
const WX_ERROR = ["msg" => "获取信息失败", "code" => 500, "errorCode" => 25101];
const CONFIG_ERROR = ["msg" => "配置不正确", "code" => 500, "errorCode" => 25102];
const GET_TOKEN_ERROR = ["msg" => "获取token失败", "code" => 500, "errorCode" => 25103];
const NOT_FOUND_USER = ["msg" => "获取用户信息失败", "code" => 404, "errorCode" => 25104];
public static function atranslate($message,$param)
......@@ -23,4 +25,9 @@ class AuthError
return sprintf(...$param);
}
public static function getError($code) {
$reflect = new \ReflectionClass(StoreError::class);
return $reflect->getConstant('E'.$code);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/7/27
* Time: 14:47
*/
namespace Hdll\Services\Auth\Exception;
use Hdll\Services\Common\Exception\CommonException;
class AuthException extends CommonException
{
}
\ No newline at end of file
......@@ -10,7 +10,11 @@
namespace Hdll\Services\Common\Entity;
use Hdll\Services\Auth\Enum\AuthError;
use Hdll\Services\Auth\Exception\AuthException;
use Swoft\App;
use Swoft\Bean\Annotation\Bean;
use Swoft\Cache\Cache;
/**
* 用户实体
......@@ -34,17 +38,20 @@ class User
private $token;
private $cmId;
private $sessionKey;
private $scope;
private $referId;
private $unionid;
private $openId;
public function __construct($token='')
{
$this->setHeadImg("test");
$this->setId(1);
$this->setNickName("zhangsan");
$this->setStoreId("2");
$this->setToken($token);
}
public function getId()
......@@ -92,9 +99,54 @@ class User
return $this->headImg = $value;
}
public function setSessionKey($value)
{
$this->sessionKey = $value;
}
public function setOpenId($value)
{
$this->openId = $value;
}
public function setUnionid($value)
{
$this->unionid = $value;
}
public function setReferId($value)
{
$this->referId = $value;
}
public function setScope($value)
{
$this->scope = $value;
}
public function setToken($value)
{
return $this->token = $value;
if ( empty($value) ) {
return [false,AuthError::GET_TOKEN_ERROR];
}
/**
* @var Cache $cache
*/
$cache = App::getBean("cache");
$info = $cache->get('AUTH:'.$value);
if ( empty($info) ) {
return [false,AuthError::NOT_FOUND_USER];
}
$info = json_decode($info, true);
$this->fill($info);
$this->token = $value;
return true;
}
public function setCmId($value)
......@@ -106,5 +158,47 @@ class User
{
return $this->cmId;
}
public function fill($arr)
{
foreach ($arr as $name => $value) {
$methodName = sprintf('set%s', ucfirst($name));
var_dump(method_exists($this, $methodName),$this,$methodName);
if (method_exists($this, $methodName)) {
$this->$methodName($value);
}
}
}
public function getSessionKey($value)
{
return $this->sessionKey;
}
public function getOpenId($value)
{
return $this->openId;
}
public function getUnionid($value)
{
return $this->unionid;
}
public function getReferId($value)
{
return $this->referId;
}
public function getScope($value)
{
return $this->scope;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment