CheckAuth.php 1.4 KB

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