| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- declare(strict_types=1);
- namespace app\api\service\auth;
- use app\api\service\auth\Adapter;
- use think\facade\Cache;
- use app\common\model\User;
- use app\common\model\UserToken;
- use think\facade\Config;
- class ApiAuthService
- {
- protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'balance', 'score'];
-
- public function userinfo(bool $allinfo = false)
- {
-
- }
- public function logout()
- {
- //$this->adapter->logout();
- }
- public function getToken()
- {
- //$usertoken=$this->adapter->getUserToken();
- //return $usertoken->token;
- }
- public function login(string $username, string $password)
- {
- $token=uuid();
- $user=User::where('username',$username)->find();
- if(!$user){
- throw new \Exception('账号或密码错误');
- }
- if($user->password!=md5(md5($password.$user->salt))){
- throw new \Exception('账号或密码错误');
- }
- if($user->status!='normal'){
- throw new \Exception('账号已经被禁用');
- }
- //刷新token
- $token = MysqlAdapter::login($token, $user);
- $user->loginfailure = 0;
- $user->logintime = time();
- $user->loginip = request()->ip();
- $user->save();
- Cache::set('user_info_'.$user->id, $user->toArray(), Config::get('app.user_login.keepalive_time'));
- return ['userinfo'=>$user,'token'=>$token];
- }
-
- public function updateToken(int $uid, array $arr)
- {
- return UserToken::where('user_id', $uid)->update($arr);
- }
- }
|