Login.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\middleware;
  4. use app\common\model\Admin;
  5. use app\facade\Encryption;
  6. use app\Request;
  7. use Closure;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\exception\HttpResponseException;
  12. use think\Response;
  13. /**
  14. * 登录认证
  15. * Class CheckAdmin
  16. * @package app\middleware
  17. */
  18. class Login
  19. {
  20. protected static int $CODE_SUCCESS = 0; //成功
  21. protected static int $CODE_TOKEN_EXPIRE = 11; //token过期
  22. protected static int $CODE_TOKEN_ERR = 12; //token不正确或已失效
  23. protected static int $CODE_TOKEN_NONE = 13; //缺少token
  24. protected static int $CODE_TOKEN_FORMAT_ERR = 14;//token格式不正确
  25. protected static int $SYSTEM_ERR = 999;//系统异常
  26. /**
  27. * 处理请求
  28. * @param Request $request
  29. * @param Closure $next
  30. * @return mixed
  31. */
  32. public function handle(Request $request, Closure $next): mixed
  33. {
  34. $admin = $this->checkToken();
  35. /* $department_ids = \app\common\model\DepartmentAdmin::where('admin_id', $admin->id)->column('department_id');
  36. $result =[];
  37. foreach ($department_ids as $k => $v) {
  38. $this->getDept($v, $result);
  39. }
  40. $n_array = array_merge($department_ids,$result);
  41. $relate_admin_ids = \app\common\model\Admin::where('department_id', 'in', $n_array)->column('id');
  42. $request->admin = $admin;
  43. $request->company_id = $admin->company_id;
  44. $request->relate_admin_ids = $relate_admin_ids;*/
  45. return $next($request);
  46. }
  47. public function getDept($parent_id, &$result): void
  48. {
  49. $dept = \app\common\model\Department::where('parent_id', $parent_id)->column('id');
  50. foreach ($dept as $k => $v) {
  51. array_push($result, $v);
  52. $this->getDept($v, $result);
  53. }
  54. }
  55. /**
  56. * 结束调度
  57. * @param Response $response
  58. */
  59. public function end(Response $response): void
  60. {
  61. // 回调行为
  62. // $response->send();
  63. }
  64. /**
  65. * 检测token
  66. * token规则
  67. * token由base64编码,解码后分为密文、主键、过期时间(时间戳)三部分,用竖线|隔开
  68. */
  69. public function checkToken()
  70. {
  71. $token = Encryption::getToken();
  72. if ($token == 1) {
  73. return (new Admin)->detail(['id' => 27]);
  74. }
  75. if (!$token) {
  76. $res = returnFormat(self::$CODE_TOKEN_NONE, self::getError(self::$CODE_TOKEN_NONE));
  77. throw new HttpResponseException(Response::create($res, "json"));
  78. }
  79. $tokenReal = base64_decode($token);
  80. $tokenArr = explode("|", $tokenReal);//拆分token
  81. if (count($tokenArr) != 3) {
  82. $res = returnFormat(self::$CODE_TOKEN_FORMAT_ERR, self::getError(self::$CODE_TOKEN_FORMAT_ERR));
  83. throw new HttpResponseException(Response::create($res, "json"));
  84. }
  85. //判断token有没有超时
  86. if (time() > $tokenArr[2]) {
  87. $res = returnFormat(self::$CODE_TOKEN_EXPIRE, self::getError(self::$CODE_TOKEN_EXPIRE));
  88. throw new HttpResponseException(Response::create($res, "json"));
  89. }
  90. $adminId = $tokenArr[1];
  91. try {
  92. $admin = (new Admin)->where("id", "=", $adminId)->find();
  93. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  94. $res = returnFormat(self::$SYSTEM_ERR, $e->getMessage());
  95. throw new HttpResponseException(Response::create($res, "json"));
  96. }//找到token
  97. if (!$admin) {
  98. $res = returnFormat(self::$CODE_TOKEN_ERR, self::getError(self::$CODE_TOKEN_ERR));
  99. throw new HttpResponseException(Response::create($res, "json"));
  100. }
  101. return $admin;
  102. }
  103. /**
  104. * 返回TOKEN错误代内容
  105. * @param $code
  106. * @return string
  107. */
  108. private static function getError($code): string
  109. {
  110. $errArr = self::getErrorArr();
  111. if (!key_exists($code, $errArr)) {
  112. return "未知错误";
  113. }
  114. return $errArr[$code];
  115. }
  116. /**
  117. * 获取TOKEN错误码数组
  118. * @return array
  119. */
  120. private static function getErrorArr(): array
  121. {
  122. return [
  123. self::$CODE_SUCCESS => "成功",
  124. self::$CODE_TOKEN_EXPIRE => "token过期",
  125. self::$CODE_TOKEN_ERR => "token不正确或已失效",
  126. self::$CODE_TOKEN_NONE => "缺少token",
  127. self::$CODE_TOKEN_FORMAT_ERR => "token格式不正确",
  128. self::$SYSTEM_ERR => "系统异常",
  129. ];
  130. }
  131. }