apiMiddleware.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\abutment\middleware;
  3. use think\Exception;
  4. use think\exception\ValidateException;
  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'];
  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. $request->user = $this->getUserInfo($param);
  26. }
  27. //3.记录日志
  28. RequestLogModel::add($request->request_id, $param);
  29. return $next($request);
  30. }
  31. //请求结束的回调(如果返回数据用的是app_show/error_show,即直接echo,则不会触发该方法)
  32. public function end(Response $response)
  33. {
  34. //只做记录,不做输出
  35. RequestLogModel::where('request_id', request()->request_id)->save([
  36. 'response' => $response->getContent(),
  37. 'supplierNo' => request()->user['supplierNo'] ?? '',
  38. 'uid' => request()->user['uid'] ?? 0,
  39. //如果是白名单接口(例如登录接口),这个时候是没有supplierNo和uid字段的
  40. ]);
  41. }
  42. /**
  43. * //校验相关参数
  44. * private function checkHeader(array $header = [])
  45. * {
  46. * $val = Validate::rule([
  47. * 'clientid' => 'require',
  48. * 'timestamp|时间戳' => 'require|number|length:10',
  49. * 'randomstring|随机字符串' => 'require|alphaNum|length:20',
  50. * 'sign|签名' => 'require',
  51. * 'token' => 'require|length:40|alphaNum',
  52. * ]);
  53. *
  54. * if (!$val->check($header)) throw new ValidateException('请求基础参数有误:' . $val->getError());
  55. *
  56. * }
  57. *
  58. *
  59. * //获取供应商的信息
  60. * private function getSupplierInfoByClientID(string $clientID = '')
  61. * {
  62. * $key = Config::get('config.redis_key.supplier_info');
  63. *
  64. * $key .= $clientID;
  65. *
  66. * $data = Cache::get($key);
  67. *
  68. * if (!$data) {
  69. *
  70. * $data = Db::name('abutment_supplier_development')
  71. * ->alias('a')
  72. * ->field('a.id,a.supplierNo,a.supplier,a.clientID,a.clientSecret,s.person,s.personid')
  73. * ->leftJoin('supplier s', 's.code=a.supplierNo AND s.is_del=0')
  74. * ->where(['a.clientID' => $clientID, 'a.is_del' => 0])
  75. * ->findOrEmpty();
  76. *
  77. * Cache::set($key, $data, 3600 * 24);
  78. * }
  79. *
  80. * return $data;
  81. * }
  82. *
  83. *
  84. * //校验签名
  85. * private function checkSign(array $header = [], string $clientSecret = '')
  86. * {
  87. *
  88. * $str = substr($header['randomstring'], 0, 10) . $header['clientid'] . $header['timestamp'] . $header['token'] . substr($header['randomstring'], 10) . $clientSecret;
  89. *
  90. * //签名 = md5(随机字符串前10位 . clientID .时间戳 . token . 随机字符串后10位 . $clientSecret, 时间戳);
  91. * $sign = md5($str);
  92. *
  93. * if ($sign != $header['sign']) throw new ValidateException('签名错误');
  94. *
  95. * }
  96. **/
  97. //获取用户信息
  98. public function getUserInfo(array $param = [])
  99. {
  100. $val = Validate::rule([
  101. 'token|用户token' => 'require|length:40|alphaNum',
  102. 'supplierNo|供应商编码' => 'require|length:18|alphaNum',
  103. ]);
  104. if (!$val->check($param)) json_show(101, '参数错误,' . $val->getError());
  105. $data = Cache::get(Config::get('redis_key.user_info_token') . $param['token']);
  106. $data = json_decode($data, true);
  107. if (!$data) return json_show(102, 'token已过期');
  108. $supplierNames = array_column($data['supplier_list'], 'supplierName', 'supplierNo');
  109. if (!isset($supplierNames[$param['supplierNo']])) json_show(103, '你尚未绑定当前供应商');
  110. return array_merge($data, ['supplierNo' => $param['supplierNo'], 'supplierName' => $supplierNames[$param['supplierNo']]]);
  111. }
  112. }