apiMiddleware.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\abutment\middleware;
  3. use app\abutment\model\SupplierRelationUser;
  4. use app\abutment\model\SupplierUser;
  5. use think\facade\Cache;
  6. use think\facade\Config;
  7. use think\facade\Validate;
  8. use think\Response;
  9. use app\abutment\model\RequestLog as RequestLogModel;
  10. //中间件
  11. class apiMiddleware
  12. {
  13. //白名单
  14. private $white_list = ['login','getuserinfo'];
  15. //请求入口
  16. public function handle($request, \Closure $next)
  17. {
  18. //请求的唯一标识
  19. $request->request_id = date('YmdHis') . mt_rand(100000, 999999);
  20. //1.接收参数
  21. $param = $request->post();
  22. //判断白名单
  23. if (!in_array(strtolower(request()->pathinfo()), $this->white_list)) {
  24. //获取用户信息
  25. $user = $this->getUserInfo($param);
  26. if (is_array($user)) $request->user = $user;
  27. else return $user;//响应信息,说明报错,直接返回给请求方
  28. }
  29. //3.记录日志
  30. RequestLogModel::add($request->request_id, $param);
  31. return $next($request);
  32. }
  33. //请求结束的回调(如果返回数据用的是app_show/error_show,即直接echo,则不会触发该方法)
  34. public function end(Response $response)
  35. {
  36. //只做记录,不做输出
  37. RequestLogModel::where('request_id', request()->request_id)->save([
  38. 'response' => $response->getContent(),
  39. 'supplierNo' => request()->user['supplierNo'] ?? '',
  40. 'uid' => request()->user['uid'] ?? 0,
  41. //如果是白名单接口(例如登录接口),这个时候是没有supplierNo和uid字段的
  42. ]);
  43. }
  44. /**
  45. * //校验相关参数
  46. * private function checkHeader(array $header = [])
  47. * {
  48. * $val = Validate::rule([
  49. * 'clientid' => 'require',
  50. * 'timestamp|时间戳' => 'require|number|length:10',
  51. * 'randomstring|随机字符串' => 'require|alphaNum|length:20',
  52. * 'sign|签名' => 'require',
  53. * 'token' => 'require|length:40|alphaNum',
  54. * ]);
  55. *
  56. * if (!$val->check($header)) throw new ValidateException('请求基础参数有误:' . $val->getError());
  57. *
  58. * }
  59. *
  60. *
  61. * //获取供应商的信息
  62. * private function getSupplierInfoByClientID(string $clientID = '')
  63. * {
  64. * $key = Config::get('config.redis_key.supplier_info');
  65. *
  66. * $key .= $clientID;
  67. *
  68. * $data = Cache::get($key);
  69. *
  70. * if (!$data) {
  71. *
  72. * $data = Db::name('abutment_supplier_development')
  73. * ->alias('a')
  74. * ->field('a.id,a.supplierNo,a.supplier,a.clientID,a.clientSecret,s.person,s.personid')
  75. * ->leftJoin('supplier s', 's.code=a.supplierNo AND s.is_del=0')
  76. * ->where(['a.clientID' => $clientID, 'a.is_del' => 0])
  77. * ->findOrEmpty();
  78. *
  79. * Cache::set($key, $data, 3600 * 24);
  80. * }
  81. *
  82. * return $data;
  83. * }
  84. *
  85. *
  86. * //校验签名
  87. * private function checkSign(array $header = [], string $clientSecret = '')
  88. * {
  89. *
  90. * $str = substr($header['randomstring'], 0, 10) . $header['clientid'] . $header['timestamp'] . $header['token'] . substr($header['randomstring'], 10) . $clientSecret;
  91. *
  92. * //签名 = md5(随机字符串前10位 . clientID .时间戳 . token . 随机字符串后10位 . $clientSecret, 时间戳);
  93. * $sign = md5($str);
  94. *
  95. * if ($sign != $header['sign']) throw new ValidateException('签名错误');
  96. *
  97. * }
  98. **/
  99. //获取用户信息
  100. public function getUserInfo(array $param = [])
  101. {
  102. //101 < code < 104 ,表示用户鉴权错误,需要用户重新登录
  103. //code==101 token不存在
  104. //code==102 token已失效
  105. //code==103 未找到对应的账户
  106. //code==104 token无效
  107. $val = Validate::rule(['token|用户token' => 'require|length:40|alphaNum']);
  108. if (!$val->check($param)) return json_show(101, '参数错误,' . $val->getError());
  109. $val2 = Validate::rule(['supplierNo|供应商编码' => 'require|length:18|alphaNum']);
  110. if (!$val2->check($param)) return json_show(1004, '参数错误,' . $val2->getError());
  111. $data = Cache::get(Config::get('redis_key.user_info_token') . $param['token']);
  112. $data = json_decode($data, true);
  113. if (!$data) return json_show(102, 'token已过期');
  114. $res = SupplierRelationUser::field('id,supplierNo,supplierName')
  115. ->where(['is_del' => SupplierUser::$is_del_normal, 'supplierNo' => $param['supplierNo'], 'uid' => $data['uid']])
  116. ->findOrEmpty()
  117. ->toArray();
  118. if (empty($res)) return json_show(1004, '你尚未绑定当前供应商');
  119. return array_merge($data, ['supplierNo' => $param['supplierNo'], 'supplierName' => $res['supplierName']]);
  120. }
  121. }