Base.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\ErrorCode;
  4. use app\BaseController;
  5. use think\exception\HttpResponseException;
  6. use think\exception\ValidateException;
  7. use think\facade\Log;
  8. use think\Response;
  9. class Base
  10. {
  11. protected $checkTokenOpen = false;
  12. public function __construct()
  13. {
  14. Log::record("---------------------------","info");
  15. Log::record($_SERVER['REQUEST_METHOD'] . ' ' . getUrl(), 'info');
  16. Log::record('REFERER ' .(array_key_exists('HTTP_REFERER',$_SERVER)?$_SERVER['HTTP_REFERER']:""), 'info');
  17. Log::record('GET ' . json_encode($_GET, JSON_UNESCAPED_UNICODE), 'info');
  18. Log::record('POST ' . json_encode($_POST, JSON_UNESCAPED_UNICODE), 'info');
  19. Log::record('cookie ' . json_encode($_COOKIE, JSON_UNESCAPED_UNICODE), 'info');
  20. Log::record('input ' . json_encode(input('param.'), JSON_UNESCAPED_UNICODE), 'info');
  21. $this->checkApiSign();
  22. if($this->checkTokenOpen){
  23. $this->checkToken();
  24. }
  25. }
  26. /**
  27. * 获取token
  28. * @return array|mixed|string|null
  29. */
  30. protected function getToken(){
  31. $token=null;
  32. if(!$token){
  33. //from header
  34. $token=request()->header("token");
  35. }
  36. if(!$token){
  37. //from url
  38. $token=input("token");
  39. }
  40. return $token;
  41. }
  42. /**
  43. * 检测token
  44. * token规则
  45. * token由base64编码,解码后分为密文、主键、过期时间(时间戳)三部分,用竖线|隔开
  46. */
  47. public function checkToken()
  48. {
  49. $token=$this->getToken();
  50. if(!$token){
  51. $this->error(ErrorCode::getError(13),13);
  52. }
  53. $tokerReal=base64_decode($token);
  54. $tokenArr=explode("|",$tokerReal);//拆分token
  55. if(count($tokenArr)!=3){
  56. $this->error(ErrorCode::getError(14),14);
  57. }
  58. //判断token有没有超时
  59. if(time()>$tokenArr[2]){
  60. $this->error(ErrorCode::getError(11),11);
  61. }
  62. //以下部分根据自己的业务实现
  63. }
  64. /**
  65. * 返回成功信息
  66. * @param $data
  67. * @param string $msg
  68. */
  69. public function success($data,$msg=""){
  70. $res=returnFormat(0,$msg,$data);
  71. Log::record("response:" . mb_substr(json_encode($res, JSON_UNESCAPED_UNICODE), 0, 1000) . ",code:0" , "debug");
  72. throw new HttpResponseException(Response::create($res,"json"));
  73. }
  74. /**
  75. * 简易错误提示
  76. * @param $code
  77. */
  78. public function errorSimple($code){
  79. $this->error(ErrorCode::getError($code),$code);
  80. }
  81. /**
  82. * 返回失败信息
  83. * @param $msg
  84. * @param int $code
  85. * @param array $data
  86. */
  87. public function error($msg,$code=999,$data=[]){
  88. $res=returnFormat($code,$msg,$data);
  89. Log::record("response:" . mb_substr(json_encode($res, JSON_UNESCAPED_UNICODE), 0, 1000) . ",code:" . $code, "debug");
  90. throw new HttpResponseException(Response::create($res,"json"));
  91. }
  92. /**
  93. * 自动校验
  94. * @param $class
  95. * @param $param
  96. */
  97. public function autoValid($class,$param){
  98. try {
  99. validate($class)->check($param);
  100. } catch (ValidateException $e) {
  101. // 验证失败 输出错误信息
  102. $this->error($e->getError());
  103. }
  104. }
  105. /**
  106. * 检查签名
  107. */
  108. public function checkApiSign(){
  109. $timestampLimit=20;
  110. $param = request()->param();
  111. $this->autoValid([
  112. "_timestamp"=>"require",
  113. "_sign"=>"require",
  114. ], $param);
  115. if(!($param["_timestamp"]>=time()-$timestampLimit*60 && $param["_timestamp"]<=time()+$timestampLimit*60)){
  116. $this->error("时间戳不合法,请刷新");
  117. }
  118. $sign=$param["_sign"];
  119. unset($param["_sign"]);
  120. ksort($param);
  121. $param['_timestamp'] = $param['_timestamp'];
  122. $secret=config("common.api_sign_secret");
  123. $signStr=stripslashes(json_encode($param,JSON_UNESCAPED_UNICODE).$secret);
  124. $sign2=md5($signStr);
  125. if($sign!==$sign2){
  126. $this->error("签名错误。".$signStr);
  127. }
  128. }
  129. }