CheckAuth.php 2.3 KB

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