CheckAuth.php 2.1 KB

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