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 CheckAdmin
- * @package app\middleware
- */
- class Login
- {
- protected static int $CODE_SUCCESS = 0; //成功
- protected static int $CODE_TOKEN_EXPIRE = 11; //token过期
- protected static int $CODE_TOKEN_ERR = 12; //token不正确或已失效
- protected static int $CODE_TOKEN_NONE = 13; //缺少token
- protected static int $CODE_TOKEN_FORMAT_ERR = 14;//token格式不正确
- protected static int $SYSTEM_ERR = 999;//系统异常
- /**
- * 处理请求
- * @param Request $request
- * @param Closure $next
- * @return mixed
- */
- public function handle(Request $request, Closure $next): mixed
- {
- $admin = $this->checkToken();
- /* $department_ids = \app\common\model\DepartmentAdmin::where('admin_id', $admin->id)->column('department_id');
- $result =[];
- foreach ($department_ids as $k => $v) {
- $this->getDept($v, $result);
- }
- $n_array = array_merge($department_ids,$result);
- $relate_admin_ids = \app\common\model\Admin::where('department_id', 'in', $n_array)->column('id');
- $request->admin = $admin;
- $request->company_id = $admin->company_id;
- $request->relate_admin_ids = $relate_admin_ids;*/
- 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);
- }
- }
- /**
- * 结束调度
- * @param Response $response
- */
- public function end(Response $response): void
- {
- // 回调行为
- // $response->send();
- }
- /**
- * 检测token
- * token规则
- * token由base64编码,解码后分为密文、主键、过期时间(时间戳)三部分,用竖线|隔开
- */
- 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);//拆分token
- 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"));
- }
- //判断token有没有超时
- 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"));
- }//找到token
- if (!$admin) {
- $res = returnFormat(self::$CODE_TOKEN_ERR, self::getError(self::$CODE_TOKEN_ERR));
- throw new HttpResponseException(Response::create($res, "json"));
- }
- return $admin;
- }
- /**
- * 返回TOKEN错误代内容
- * @param $code
- * @return string
- */
- private static function getError($code): string
- {
- $errArr = self::getErrorArr();
- if (!key_exists($code, $errArr)) {
- return "未知错误";
- }
- return $errArr[$code];
- }
- /**
- * 获取TOKEN错误码数组
- * @return array
- */
- 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 => "系统异常",
- ];
- }
- }
|