homeMiddleware.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 homeMiddleware
  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. $header = $request->header();
  26. $val_header = Validate::rule([
  27. 'uid' => 'require|number|gt:0',
  28. 'nickname' => 'require|max:255',
  29. 'mobile' => 'require|number|length:11|mobile',
  30. 'email' => 'email|max:255',
  31. 'supplierno' => 'require|alphaNum|length:18',
  32. 'suppliername' => 'require|max:255',//header自动把大写字母转换成小写的了
  33. ]);
  34. if ($val_header->check($header)) {
  35. $request->user = [
  36. 'uid' => $header['uid'],
  37. 'nickname' => $header['nickname'],
  38. 'mobile' => $header['mobile'],
  39. 'email' => '',
  40. 'supplierNo' => $header['supplierno'],
  41. 'supplierName' => $header['suppliername']
  42. ];
  43. } else {
  44. //获取用户信息
  45. $user = $this->getUserInfo($param);
  46. if (is_array($user)) $request->user = $user;
  47. else return $user;//响应信息,说明报错,直接返回给请求方
  48. }
  49. }
  50. //3.记录日志
  51. RequestLogModel::add($request->request_id, $param);
  52. return $next($request);
  53. }
  54. //请求结束的回调(如果返回数据用的是app_show/error_show,即直接echo,则不会触发该方法)
  55. public function end(Response $response)
  56. {
  57. //只做记录,不做输出
  58. RequestLogModel::where('request_id', request()->request_id)->save([
  59. 'response' => $response->getContent(),
  60. 'supplierNo' => request()->user['supplierNo'] ?? '',
  61. 'uid' => request()->user['uid'] ?? 0,
  62. //如果是白名单接口(例如登录接口),这个时候是没有supplierNo和uid字段的
  63. ]);
  64. }
  65. /**
  66. * //校验相关参数
  67. * private function checkHeader(array $header = [])
  68. * {
  69. * $val = Validate::rule([
  70. * 'clientid' => 'require',
  71. * 'timestamp|时间戳' => 'require|number|length:10',
  72. * 'randomstring|随机字符串' => 'require|alphaNum|length:20',
  73. * 'sign|签名' => 'require',
  74. * 'token' => 'require|length:40|alphaNum',
  75. * ]);
  76. *
  77. * if (!$val->check($header)) throw new ValidateException('请求基础参数有误:' . $val->getError());
  78. *
  79. * }
  80. *
  81. *
  82. * //获取供应商的信息
  83. * private function getSupplierInfoByClientID(string $clientID = '')
  84. * {
  85. *
  86. * $key .= $clientID;
  87. *
  88. * $data = Cache::get($key);
  89. *
  90. * if (!$data) {
  91. *
  92. * $data = Db::name('abutment_supplier_development')
  93. * ->alias('a')
  94. * ->field('a.id,a.supplierNo,a.supplier,a.clientID,a.clientSecret,s.person,s.personid')
  95. * ->leftJoin('supplier s', 's.code=a.supplierNo AND s.is_del=0')
  96. * ->where(['a.clientID' => $clientID, 'a.is_del' => 0])
  97. * ->findOrEmpty();
  98. *
  99. * Cache::set($key, $data, 3600 * 24);
  100. * }
  101. *
  102. * return $data;
  103. * }
  104. *
  105. *
  106. * //校验签名
  107. * private function checkSign(array $header = [], string $clientSecret = '')
  108. * {
  109. *
  110. * $str = substr($header['randomstring'], 0, 10) . $header['clientid'] . $header['timestamp'] . $header['token'] . substr($header['randomstring'], 10) . $clientSecret;
  111. *
  112. * //签名 = md5(随机字符串前10位 . clientID .时间戳 . token . 随机字符串后10位 . $clientSecret, 时间戳);
  113. * $sign = md5($str);
  114. *
  115. * if ($sign != $header['sign']) throw new ValidateException('签名错误');
  116. *
  117. * }
  118. **/
  119. //获取用户信息
  120. public function getUserInfo(array $param = [])
  121. {
  122. //101 < code < 104 ,表示用户鉴权错误,需要用户重新登录
  123. //code==101 token不存在
  124. //code==102 token已失效
  125. //code==103 未找到对应的账户
  126. //code==104 token无效
  127. //code==105 供应商编码错误
  128. //code==106 你尚未绑定当前供应商
  129. $val = Validate::rule(['token|用户token' => 'require|length:40|alphaNum']);
  130. if (!$val->check($param)) return json_show(101, '参数错误,' . $val->getError());
  131. $val2 = Validate::rule(['supplierNo|供应商编码' => 'require|length:18|alphaNum']);
  132. if (!$val2->check($param)) return json_show(105, '参数错误,' . $val2->getError());
  133. $data = SupplierUser::field('uid,nickname,mobile,email')
  134. ->alias('a')
  135. ->where(['is_del' => SupplierUser::$is_del_normal, 'status' => SupplierUser::$status_normal, 'token' => $param['token']])
  136. ->where('expire_time', '>=', date('Y-m-d H:i:s'))
  137. ->findOrEmpty()
  138. ->toArray();
  139. if (empty($data)) return json_show(102, 'token不存在或已过期');
  140. $res = SupplierRelationUser::field('id,supplierNo,supplierName')
  141. ->where(['is_del' => SupplierUser::$is_del_normal, 'supplierNo' => $param['supplierNo'], 'uid' => $data['uid']])
  142. ->findOrEmpty()
  143. ->toArray();
  144. if (empty($res)) return json_show(106, '你尚未绑定当前供应商');
  145. return array_merge($data, ['supplierNo' => $param['supplierNo'], 'supplierName' => $res['supplierName']]);
  146. }
  147. }