Base.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\ErrorCode;
  4. use think\exception\HttpResponseException;
  5. use think\exception\ValidateException;
  6. use think\facade\Log;
  7. use think\Response;
  8. /**
  9. * 控制器基类
  10. * Class Base
  11. * @package app\common\controller
  12. */
  13. class Base
  14. {
  15. protected $debug=false;//是否启用调试模式
  16. protected $checkSignOpen = false;//是否校验签名,true校验,false不校验
  17. //=== begin 处理跨域====
  18. /**
  19. * 构造函数,处理跨域
  20. * Base constructor
  21. */
  22. public function __construct()
  23. {
  24. if ($this->catchOptions()) {
  25. die("this is option");
  26. }
  27. //启用调试模式
  28. }
  29. protected function catchOptions()
  30. {
  31. $request = request();
  32. return $request->method() == "OPTIONS";
  33. }
  34. //===end 处理跨域====
  35. /**
  36. * 成功返回内容
  37. * @param $data
  38. * @param string $msg
  39. * @param int $code
  40. */
  41. public function success($data,$msg="",$code=0)
  42. {
  43. if($msg==""){
  44. $msg=ErrorCode::getError($code);
  45. }
  46. $response = returnFormat($code, $msg, $data);
  47. Log::record(json_encode($response, JSON_UNESCAPED_UNICODE), "response");
  48. throw new HttpResponseException(Response::create($response, "json"));
  49. }
  50. /**
  51. * 失败返回内容
  52. * @param int $code
  53. * @param null $data
  54. */
  55. public function error($code = 9999,$msg="", $data = null)
  56. {
  57. if($msg==""){
  58. $msg=ErrorCode::getError($code);
  59. }
  60. if ($data === null) {
  61. $data = new \stdClass();
  62. }
  63. $response = returnFormat($code, $msg, $data);
  64. Log::record(json_encode($response, JSON_UNESCAPED_UNICODE), "response");
  65. throw new HttpResponseException(Response::create($response, "json"));
  66. }
  67. /**
  68. * 自动校验
  69. * @param $class
  70. * @param $param
  71. */
  72. protected function autoValid($class, $param)
  73. {
  74. try {
  75. validate($class)->check($param);
  76. } catch (ValidateException $e) {
  77. // 验证失败 输出错误信息
  78. return returnFormat(999,$e->getError());
  79. //$this->error($e->getError());
  80. }
  81. return returnFormat(0);
  82. }
  83. /**
  84. * 接口签名校验
  85. */
  86. protected function checkApiSign()
  87. {
  88. $param = request()->param();
  89. $validRes= $this->autoValid([
  90. "_timestamp" => "require",
  91. "_sign" => "require",
  92. ], $param);
  93. if($validRes['code']!==0){
  94. return $validRes;
  95. }
  96. $effectMinute=2;//有效时间,分钟数
  97. if (!($param["_timestamp"] >= time() - $effectMinute * 60 && $param["_timestamp"] <= time() + $effectMinute * 60)) {
  98. return returnFormat(9004);
  99. }
  100. $sign = $param["_sign"];
  101. unset($param["_sign"]);//删除原签名字段,用来重新生成签名
  102. ksort($param);
  103. $secret = config("common.api_sign_secret");
  104. $sign2 = md5(json_encode($param, JSON_UNESCAPED_UNICODE) . $secret);
  105. if ($sign !== $sign2) {
  106. return returnFormat(9002," $secret 原签名:$sign 新签名:$sign2 ");
  107. }
  108. return returnFormat(0);
  109. }
  110. }