CheckAuth.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. if(!isset($param['token'])||$param['token']==''){
  22. $header = $request->header();
  23. $check =$this->check($header,$param);
  24. if($check['code']==1){
  25. return json_show(104,$check['msg']);
  26. }
  27. }else{
  28. $acct =VerifyTokens($param['token']);
  29. if(!isset($acct['code']) || $acct['code']!=0){
  30. return json_show(102,$acct['message']);
  31. }
  32. $request->uid=isset($acct['data']['user']['id']) ?$acct['data']['user']['id']:"";
  33. $request->uname=isset($acct['data']['user']['nickname']) ?$acct['data']['user']['nickname']:"";
  34. $request->isCx=1;
  35. }
  36. $response = $next($request);
  37. return $response;
  38. }
  39. /**数据接口签名验证
  40. * @param $data
  41. * @param $param
  42. * @return array
  43. */
  44. private function check($data,$param){
  45. //check sign
  46. if (!isset($data['appid']) || !$data['appid']) {
  47. return ['code'=>1,'msg'=>'发送的应用参数不存在'];
  48. }
  49. $appinf =Db::name("act_company")->where(["app_id"=>$data['appid'],"is_del"=>0,"status"=>1])->findOrEmpty();
  50. if(empty($appinf)){
  51. return ['code'=>1,'msg'=>'发送的应用参数错误'];
  52. }
  53. $mege=["appid"=>$data['appid'],"nonce"=>$data['noce']??'',"sign"=>$data['sign']??'',"timestamp"=>$data['timestamp']??''];
  54. $value =array_merge($mege,$param);
  55. $Sign=new Sign($appinf['app_id'],$appinf['app_key']);
  56. $result =$Sign->verifySign($value);
  57. return $result;
  58. }
  59. }