Base.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\Result;
  4. use think\App;
  5. use think\Response;
  6. use think\facade\Log;
  7. use app\BaseController;
  8. use app\common\ErrorCode;
  9. use app\common\model\Admin;
  10. use app\common\middleware\WriteLog;
  11. use think\annotation\route\Middleware;
  12. use think\exception\ValidateException;
  13. use think\exception\HttpResponseException;
  14. #[Middleware([WriteLog::class])]
  15. class Base extends BaseController
  16. {
  17. protected $checkTokenOpen = false; //是否校验token
  18. protected $checkApiSignOpen = false; //是否校验签名
  19. public $admin; //管理员
  20. public function __construct(App $app)
  21. {
  22. parent::__construct($app);
  23. if ($this->checkApiSignOpen) {
  24. $this->checkApiSign();
  25. }
  26. if ($this->checkTokenOpen) {
  27. $this->checkToken();
  28. }
  29. }
  30. /**
  31. * 获取token
  32. * @return array|mixed|string|null
  33. */
  34. protected function getToken()
  35. {
  36. $token = null;
  37. if (!$token) {
  38. //from header
  39. $token = request()->header("token");
  40. }
  41. if (!$token) {
  42. //from url
  43. $token = input("token");
  44. }
  45. return $token;
  46. }
  47. /**
  48. * 检测token
  49. * token规则
  50. * token由base64编码,解码后分为密文、主键、过期时间(时间戳)三部分,用竖线|隔开
  51. */
  52. public function checkToken()
  53. {
  54. $token = $this->getToken();
  55. if (!$token) {
  56. $this->error(ErrorCode::getError(ErrorCode::CODE_TOKEN_NONE), ErrorCode::CODE_TOKEN_NONE);
  57. }
  58. $tokerReal = base64_decode($token);
  59. $tokenArr = explode("|", $tokerReal); //拆分token
  60. if (count($tokenArr) != 3) {
  61. $this->error(ErrorCode::getError(ErrorCode::CODE_TOKEN_FORMAT_ERR), ErrorCode::CODE_TOKEN_FORMAT_ERR);
  62. }
  63. //判断token有没有超时
  64. if (time() > $tokenArr[2]) {
  65. $this->error(ErrorCode::getError(ErrorCode::CODE_TOKEN_EXPIRE), ErrorCode::CODE_TOKEN_EXPIRE);
  66. }
  67. //以下部分根据自己的业务实现
  68. //$field = "id,login_name,valid,last_login_time,login_count,token";
  69. $user = \app\common\model\Admin::where("token", "=", $token)->find(); //找到token
  70. if (!$user) {
  71. $this->error(ErrorCode::getError(ErrorCode::CODE_TOKEN_ERR), ErrorCode::CODE_TOKEN_ERR);
  72. }
  73. $this->admin = $user;
  74. bind(Admin::class, $this->admin);
  75. }
  76. /**
  77. *
  78. * 返回成功信息
  79. * @param $data
  80. * @param string $msg
  81. */
  82. public function success($data, $msg = "")
  83. {
  84. Log::record("response:" . mb_substr(json_encode($data, JSON_UNESCAPED_UNICODE), 0, 1000) . ",code:0", "debug");
  85. return Result::rest($data);
  86. }
  87. /**
  88. *
  89. * 简易错误提示
  90. * @param $code
  91. */
  92. public function errorSimple($code)
  93. {
  94. $this->error(ErrorCode::getError($code), $code);
  95. }
  96. /**
  97. *
  98. * 返回失败信息
  99. * @param $msg
  100. * @param int $code
  101. * @param array $data
  102. */
  103. public function error($msg, $code = 999, $data = [])
  104. {
  105. $res = returnFormat($code, $msg, $data);
  106. Log::record("response:" . mb_substr(json_encode($res, JSON_UNESCAPED_UNICODE), 0, 1000) . ",code:" . $code, "debug");
  107. throw new HttpResponseException(Response::create($res, "json"));
  108. }
  109. /**
  110. * 自动校验
  111. * @param $rules 规则
  112. * @param $param 验证对象
  113. */
  114. public function autoValid($rules, $param)
  115. {
  116. try {
  117. validate($rules)->check($param);
  118. } catch (ValidateException $e) {
  119. // 验证失败 输出错误信息
  120. $this->error($e->getError());
  121. }
  122. }
  123. /**
  124. * 检查签名
  125. */
  126. public function checkApiSign()
  127. {
  128. $timestampLimit = 20;
  129. $param = request()->param();
  130. $this->autoValid([
  131. "_timestamp" => "require",
  132. "_sign" => "require",
  133. ], $param);
  134. if (!($param["_timestamp"] >= time() - $timestampLimit * 60 && $param["_timestamp"] <= time() + $timestampLimit * 60)) {
  135. $this->error("时间戳不合法,请刷新");
  136. }
  137. $sign = $param["_sign"];
  138. unset($param["_sign"]);
  139. ksort($param);
  140. $param['_timestamp'] = $param['_timestamp'];
  141. $secret = config("common.api_sign_secret");
  142. $signStr = stripslashes(json_encode($param, JSON_UNESCAPED_UNICODE) . $secret);
  143. $sign2 = md5($signStr);
  144. if ($sign !== $sign2) {
  145. Log::record("签名错误:sign: $sign sign2: $sign2", "debug");
  146. Log::record("sign2 签名key:" . $secret, "debug");
  147. Log::record("sign2 签名字符串:" . $signStr, "debug");
  148. $this->error("签名错误。" . $signStr);
  149. }
  150. }
  151. }