|
|
@@ -2,8 +2,8 @@
|
|
|
declare(strict_types=1);
|
|
|
namespace app\api\service;
|
|
|
|
|
|
-use app\api\service\auth\Adapter;
|
|
|
-
|
|
|
+use think\facade\Cookie;
|
|
|
+use app\api\service\auth\MysqlAdapter;
|
|
|
use app\common\model\User;
|
|
|
use app\common\model\UserToken;
|
|
|
use app\common\service\AuthService;
|
|
|
@@ -12,35 +12,35 @@ use app\common\service\AuthService;
|
|
|
class ApiAuthService extends AuthService
|
|
|
{
|
|
|
protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'balance', 'score'];
|
|
|
- private Adapter $adapter;
|
|
|
+
|
|
|
|
|
|
public function userinfo(bool $allinfo = false)
|
|
|
- {
|
|
|
- $user=$this->adapter->userinfo();
|
|
|
- if(!$user){
|
|
|
+ {
|
|
|
+ $time=time();
|
|
|
+ $token = Cookie::get('token');
|
|
|
+ if(!$token){
|
|
|
return false;
|
|
|
}
|
|
|
- if($allinfo){
|
|
|
- return $user;
|
|
|
- }else{
|
|
|
- return array_intersect_key($user,array_flip($this->allowFields));
|
|
|
- }
|
|
|
+ $user = UserToken::where('token',$token)->where('expire','>',$time)->field('mobile')->value('user_id');
|
|
|
+ return $user?User::where('id',$user)->field($this->allowFields)->find()->toArray():false;
|
|
|
}
|
|
|
|
|
|
public function logout()
|
|
|
- {
|
|
|
- $this->adapter->logout();
|
|
|
+ {
|
|
|
+ $adapter = new MysqlAdapter();
|
|
|
+ $adapter->logout();
|
|
|
}
|
|
|
|
|
|
- public function getToken()
|
|
|
- {
|
|
|
- $usertoken=$this->adapter->getUserToken();
|
|
|
- return $usertoken->token;
|
|
|
+ public static function getToken($token):int
|
|
|
+ {
|
|
|
+ $time=time();
|
|
|
+ return UserToken::where('token',$token)->where('expire','>',$time)->count();
|
|
|
}
|
|
|
|
|
|
+ //登录
|
|
|
public function login(string $username, string $password)
|
|
|
{
|
|
|
- $token=uuid();
|
|
|
+ $token= uuid();
|
|
|
$user=User::where('username',$username)->find();
|
|
|
if(!$user){
|
|
|
throw new \Exception('账号或密码错误');
|
|
|
@@ -51,16 +51,20 @@ class ApiAuthService extends AuthService
|
|
|
if($user->status!='normal'){
|
|
|
throw new \Exception('账号已经被禁用');
|
|
|
}
|
|
|
- $this->adapter->login($token,$user);
|
|
|
- $this->login_user=$this->adapter->userinfo();
|
|
|
+
|
|
|
+ $adapter = new MysqlAdapter();
|
|
|
+ $token = $adapter->login($token,$user);
|
|
|
+ $this->login_user=$adapter->userinfo();
|
|
|
+ return ['userinfo' => $this->login_user, 'token' => $token];
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public function loginByMobile(string $mobile, string $code)
|
|
|
{
|
|
|
// TODO: Implement loginByMobile() method.
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
public function updateToken(array $arr)
|
|
|
{
|
|
|
$usertoken=$this->adapter->getUserToken();
|