CheckAuth.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\txx\middleware;
  4. use app\txx\common\Sign;
  5. use think\facade\Db;
  6. use think\facade\Log;
  7. use think\Response;
  8. class CheckAuth
  9. {
  10. /**
  11. * 处理请求
  12. *
  13. * @param \think\Request $request
  14. * @param \Closure $next
  15. * @return Response
  16. */
  17. public function handle($request, \Closure $next)
  18. {
  19. $request->isCx=0;
  20. $request->uid=0;
  21. $request->uname='';
  22. $param = $request->post();
  23. $header = $request->header();
  24. $check =$this->check($header,$param);
  25. Log::write("IPAddr:".$request->server("REMOTE_ADDR"),"info");
  26. Log::write("Action:".$request->server("REQUEST_URI"),"info");
  27. Log::write("param:".json_encode($param),"info");
  28. Log::write("header:".json_encode($header),"info");
  29. if($check['code']==1){
  30. return json_show(104,$check['msg']);
  31. }
  32. $response = $next($request);
  33. return $response;
  34. }
  35. //请求结束的回调(如果返回数据用的是app_show/error_show,即直接echo,则不会触发该方法)
  36. public function end(Response $response)
  37. {
  38. Log::info("response:{data}",["data"=>json_encode($response->getContent(),JSON_UNESCAPED_UNICODE)]);
  39. }
  40. /**数据接口签名验证
  41. * @param $data
  42. * @param $param
  43. * @return array
  44. */
  45. private function check($data,$param){
  46. //check sign
  47. if (!isset($data['appid']) || !$data['appid']) {
  48. return ['code'=>1,'msg'=>'发送的应用参数不存在'];
  49. }
  50. $appinf =Db::name("act_company")->where(["app_id"=>$data['appid'],"is_del"=>0,"status"=>1])->findOrEmpty();
  51. if(empty($appinf)){
  52. return ['code'=>1,'msg'=>'发送的应用参数错误'];
  53. }
  54. $mege=["appid"=>$data['appid'],"noce"=>$data['noce']??'',"sign"=>$data['sign']??'',"timestamp"=>$data['timestamp']??''];
  55. $value =array_merge($mege,$param);
  56. $Sign=new Sign($appinf['app_id'],$appinf['app_key']);
  57. $result =$Sign->verifySign($value);
  58. return $result;
  59. }
  60. }