Base.php 4.8 KB

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