1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace app\data\controller\api;
- use app\data\service\UserService;
- use think\admin\Controller;
- use think\exception\HttpResponseException;
- abstract class Auth extends Controller
- {
-
- protected $type;
-
- protected $uuid;
-
- protected $user;
-
- protected function initialize()
- {
-
- $this->type = input('api') ?: $this->request->header('api-name');
- $this->type = $this->type ?: $this->request->header('api-type');
- if (empty($this->type) || empty(UserService::TYPES[$this->type])) {
- $this->error("接口通道未定义!");
- }
-
- $this->user = $this->getUser();
- $this->uuid = $this->user['id'] ?? '';
- if (empty($this->uuid)) {
- $this->error('用户登录失败!', '{-null-}', 401);
- }
- }
-
- protected function getUser(): array
- {
- try {
- if (empty($this->uuid)) {
- $token = input('token') ?: $this->request->header('api-token');
- if (empty($token)) $this->error('登录认证TOKEN不能为空!');
- [$state, $info, $this->uuid] = UserService::instance()->check($this->type, $token);
- if (empty($state)) $this->error($info, '{-null-}', 401);
- }
- return UserService::instance()->get($this->type, $this->uuid);
- } catch (HttpResponseException $exception) {
- throw $exception;
- } catch (\Exception $exception) {
- $this->error($exception->getMessage());
- }
- }
- }
|