| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace app\api\service;
- use app\BaseService;
- use app\common\model\AdminModel;
- use app\common\model\AuthGroupAccessModel;
- use app\common\model\AuthGroupModel;
- use app\common\model\StoreModel;
- use think\facade\Cache;
- class LoginService extends BaseService
- {
- private $model;
- private $storeModel;
- private $groupModel;
- private $groupAccessModel;
- public function __construct()
- {
- $this->model = new AdminModel();
- $this->groupModel = new AuthGroupModel();
- $this->storeModel = new StoreModel();
- $this->groupAccessModel = new AuthGroupAccessModel();
- }
- public function loadByLogin($username, $password) {
- $admin = $this->model->loadByLogin($username,$password);
- if (!$admin)
- return $this->fail(lang('The account password is incorrect'));
- $access = $this->groupAccessModel->findByAdminId($admin->id);
- if (!$access)
- return $this->fail(lang('Background permission is not set'));
- $group = $this->groupModel->findById($access->group_id);
- if (!$group)
- return $this->fail(lang('The role group does not exist'));
- $stores = $this->storeModel->findByIds($admin->store_ids ? explode(',', $admin->store_ids) : []);
- $admin['group'] = $group;
- $admin['stores'] = $stores;
- $token = \Jwt::getToken([
- 'iss'=>'jwt_user', //该JWT的签发者
- 'iat'=>time(), //签发时间
- 'exp'=>time()+7200*7, //过期时间
- 'nbf'=>time(), //该时间之前不接收处理该Token
- 'jti'=>md5(uniqid('JWT').time()), //该Token唯一标识
- 'admin_id' => $admin->id,
- 'account' => $admin->account,
- 'nickname' => $admin->nickname,
- 'group_id' => $group->id,
- 'group_name' => $group->name,
- ]);
- if($token === false) return $this->fail(lang('Failed to obtain token'));
- $admin->token = $token;
- // $redis = Cache::store('redis')->handler(); //返回句柄对象,可执行其它高级方法
- // $sadd = $redis->sadd('user:infos', $token);
- $refreshToken = $this->model->refreshToken($admin->id, $token);
- return $refreshToken ? $this->ok($admin) : $this->fail("Fail");
- }
- }
|