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;use think\Response;
  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. }
  47. /**数据接口签名验证
  48. * @param $data
  49. * @param $param
  50. * @return array
  51. */
  52. private function check($data,$param){
  53. //check sign
  54. if (!isset($data['appid']) || !$data['appid']) {
  55. return ['code'=>1,'msg'=>'发送的应用参数不存在'];
  56. }
  57. $appinf =Db::name("act_company")->where(["app_id"=>$data['appid'],"is_del"=>0,"status"=>1])->findOrEmpty();
  58. if(empty($appinf)){
  59. return ['code'=>1,'msg'=>'发送的应用参数错误'];
  60. }
  61. $mege=["appid"=>$data['appid'],"noce"=>$data['noce']??'',"sign"=>$data['sign']??'',"timestamp"=>$data['timestamp']??''];
  62. $value =array_merge($mege,$param);
  63. $Sign=new Sign($appinf['app_id'],$appinf['app_key']);
  64. $result =$Sign->verifySign($value);
  65. return $result;
  66. }
  67. }