CheckAuth.php 1.7 KB

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