123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- declare (strict_types=1);
- namespace app\admin\middleware;
- use app\common\model\Admin;
- use app\facade\Encryption;
- use app\Request;
- use Closure;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\HttpResponseException;
- use think\Response;
- class Login
- {
- protected static int $CODE_SUCCESS = 0;
- protected static int $CODE_TOKEN_EXPIRE = 11;
- protected static int $CODE_TOKEN_ERR = 12;
- protected static int $CODE_TOKEN_NONE = 13;
- protected static int $CODE_TOKEN_FORMAT_ERR = 14;
- protected static int $SYSTEM_ERR = 999;
-
- public function handle(Request $request, Closure $next): mixed
- {
- $admin = $this->checkToken();
-
- return $next($request);
- }
- public function getDept($parent_id, &$result): void
- {
- $dept = \app\common\model\Department::where('parent_id', $parent_id)->column('id');
- foreach ($dept as $k => $v) {
- array_push($result, $v);
- $this->getDept($v, $result);
- }
- }
-
- public function end(Response $response): void
- {
-
- }
-
- public function checkToken()
- {
- $token = Encryption::getToken();
- if ($token == 1) {
- return (new Admin)->detail(['id' => 27]);
- }
- if (!$token) {
- $res = returnFormat(self::$CODE_TOKEN_NONE, self::getError(self::$CODE_TOKEN_NONE));
- throw new HttpResponseException(Response::create($res, "json"));
- }
- $tokenReal = base64_decode($token);
- $tokenArr = explode("|", $tokenReal);
- if (count($tokenArr) != 3) {
- $res = returnFormat(self::$CODE_TOKEN_FORMAT_ERR, self::getError(self::$CODE_TOKEN_FORMAT_ERR));
- throw new HttpResponseException(Response::create($res, "json"));
- }
-
- if (time() > $tokenArr[2]) {
- $res = returnFormat(self::$CODE_TOKEN_EXPIRE, self::getError(self::$CODE_TOKEN_EXPIRE));
- throw new HttpResponseException(Response::create($res, "json"));
- }
- $adminId = $tokenArr[1];
- try {
- $admin = (new Admin)->where("id", "=", $adminId)->find();
- } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
- $res = returnFormat(self::$SYSTEM_ERR, $e->getMessage());
- throw new HttpResponseException(Response::create($res, "json"));
- }
- if (!$admin) {
- $res = returnFormat(self::$CODE_TOKEN_ERR, self::getError(self::$CODE_TOKEN_ERR));
- throw new HttpResponseException(Response::create($res, "json"));
- }
- return $admin;
- }
-
- private static function getError($code): string
- {
- $errArr = self::getErrorArr();
- if (!key_exists($code, $errArr)) {
- return "未知错误";
- }
- return $errArr[$code];
- }
-
- private static function getErrorArr(): array
- {
- return [
- self::$CODE_SUCCESS => "成功",
- self::$CODE_TOKEN_EXPIRE => "token过期",
- self::$CODE_TOKEN_ERR => "token不正确或已失效",
- self::$CODE_TOKEN_NONE => "缺少token",
- self::$CODE_TOKEN_FORMAT_ERR => "token格式不正确",
- self::$SYSTEM_ERR => "系统异常",
- ];
- }
- }
|