CheckAuth.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\middleware;
  4. use think\facade\Db;
  5. use think\middleware\AllowCrossDomain;
  6. class CheckAuth
  7. {
  8. protected $noCheck=[];
  9. // header头配置
  10. protected $header = [
  11. 'Access-Control-Allow-Credentials' => 'true',
  12. 'Access-Control-Max-Age' => 1800,
  13. 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
  14. 'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With,token',
  15. ];
  16. /**
  17. * 处理请求
  18. *
  19. * @param \think\Request $request
  20. * @param \Closure $next
  21. * @return Response|\think\response\Json
  22. */
  23. public function handle($request, \Closure $next)
  24. {
  25. if($request->isOptions()){
  26. return json();
  27. }
  28. // $header =$request->header();
  29. // $header = !empty($header) ? array_merge($this->header, $header) : $this->header;
  30. // if (!isset($header['Access-Control-Allow-Origin'])) {
  31. // $origin = $request->header('origin');
  32. // $header['Access-Control-Allow-Origin'] = $origin;
  33. // }
  34. //
  35. $param =$request->post();
  36. // if($header['is_strict_login']==True){
  37. // $check =$this->check($header,$param);
  38. // if($check['code']==1){
  39. // return json_show(104,$check['msg']);
  40. // }
  41. // }
  42. return $next($request);
  43. }
  44. /**数据接口签名验证
  45. * @param $data
  46. * @param $param
  47. * @return array
  48. */
  49. private function check($data,$param){
  50. //check sign
  51. if (!isset($data['appid']) || !$data['appid']) {
  52. return ['code'=>1,'msg'=>'发送的应用参数不存在'];
  53. }
  54. $appinf =Db::name("auth")->where(["app_id"=>$data['appid'],"is_del"=>0,"status"=>1])->findOrEmpty();
  55. if(empty($appinf)){
  56. return ['code'=>1,'msg'=>'发送的应用参数错误'];
  57. }
  58. $mege=["appid"=>$data['appid'],"noce"=>$data['noce']??'',"sign"=>$data['sign']??'',"timestamp"=>$data['timestamp']??''];
  59. $value =array_merge($mege,$param);
  60. $Sign= new \Sign($appinf['app_id'],$appinf['app_key']);
  61. $result =$Sign->verifySign($value);
  62. return $result;
  63. }
  64. }