mobileMiddleware.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\mobile\middleware;
  3. use app\mobile\logic\BaseLogic;
  4. use app\model\AccountModel;
  5. use app\model\AccountTokenModel;
  6. use app\model\CommonModel;
  7. use think\exception\ValidateException;
  8. use think\facade\Config;
  9. use think\facade\Validate;
  10. use think\Response;
  11. use app\model\AccountLogModel;
  12. //中间件
  13. class mobileMiddleware
  14. {
  15. //白名单
  16. private $white_list = ['login', 'notify'];
  17. //请求入口
  18. public function handle($request, \Closure $next)
  19. {
  20. //请求的唯一标识
  21. $request->request_id = date('YmdHis') . mt_rand(100000, 999999);
  22. //接收参数
  23. $param = $request->param();
  24. //记录日志
  25. AccountLogModel::add($request->request_id, $param);
  26. //判断白名单
  27. if (!in_array(request()->pathinfo(), $this->white_list)) {
  28. try {
  29. $val = Validate::rule(['token' => 'require']);
  30. if (!$val->check($param)) throw new ValidateException($val->getError());
  31. //获取用户信息
  32. $account = $this->verifyMobileToken($param['token']);
  33. BaseLogic::setUserInfo($account['aid'], $account['aname'], $account['company_id'], $account['card_id'], $account['group_id']);
  34. $request->aid = $account['aid'];
  35. $request->aname = $account['aname'];
  36. $request->company_id = $account['company_id'];
  37. $request->card_id = $account['card_id'];
  38. $request->group_id = $account['group_id'];
  39. } catch (ValidateException $validateException) {
  40. return json_show(CommonModel::$error_token, $validateException->getError());
  41. }
  42. }
  43. return $next($request);
  44. }
  45. //请求结束的回调(如果返回数据用的是app_show/error_show,即直接echo,则不会触发该方法)
  46. public function end(Response $response)
  47. {
  48. //只做记录,不做输出
  49. AccountLogModel::where('request_id', request()->request_id)->save([
  50. 'response' => $response->getContent(),
  51. 'uid' => request()->uid ?? 0,
  52. 'uname' => request()->uname ?? '',
  53. 'updatetime' => date('Y-m-d H:i:s')
  54. ]);
  55. }
  56. //校验手机端token
  57. private function verifyMobileToken(string $token = '')
  58. {
  59. $has = AccountTokenModel::where(['token' => $token])->findOrEmpty();
  60. if ($has->isEmpty()) throw new ValidateException('token不存在');
  61. if (strtotime($has->expiretime) <= time()) throw new ValidateException('token已失效');
  62. $account = AccountModel::alias('a')
  63. ->where(['a.id' => $has->accountid, 'a.is_del' => CommonModel::$del_normal])
  64. ->field('a.id,a.status,a.username,a.salt,a.company_id,a.card_id,b.id group_id')
  65. ->leftJoin('group b', 'b.company_id=a.company_id AND b.card_id=a.card_id AND b.is_del=' . CommonModel::$del_normal)
  66. ->findOrEmpty();
  67. if ($account->isEmpty()) throw new ValidateException('未找到账户');
  68. if ($account->status != AccountModel::$status_activated) throw new ValidateException('账户不在激活状态,无法使用');
  69. $token_str = base64_decode($token);
  70. $account_str = substr($token_str, 0, -10);
  71. if ($account_str == $account->username . $account->salt) {
  72. AccountTokenModel::where(['token' => $token])
  73. ->save(['expiretime' => date('Y-m-d H:i:s', time() + Config::get('common.expire'))]);
  74. return [
  75. 'aid' => $account->id,
  76. 'aname' => $account->username,
  77. 'company_id' => $account->company_id,
  78. 'card_id' => $account->card_id,
  79. 'group_id' => $account->group_id
  80. ];
  81. } else throw new ValidateException('账户token无效');
  82. }
  83. }