123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace app\admin\middleware;
- use app\admin\logic\BaseLogic;
- use app\model\CommonModel;
- use think\Exception;
- use think\exception\ValidateException;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Validate;
- use think\Response;
- use app\model\ActionLogModel;
- //中间件
- class adminMiddleware
- {
- //白名单
- private $white_list = ['login','orderExport'];
- //请求入口
- public function handle($request, \Closure $next)
- {
- //请求的唯一标识
- $request->request_id = date('YmdHis') . mt_rand(100000, 999999);
- //接收参数
- $param = $request->post();
- //记录日志
- ActionLogModel::add($request->request_id, $param);
- //判断白名单
- if (!in_array(request()->pathinfo(), $this->white_list)) {
- try {
- $val = Validate::rule(['token' => 'require']);
- if (!$val->check($param)) throw new ValidateException($val->getError());
- //获取用户信息
- $user = verify_token($param['token']);
- BaseLogic::setUserInfo($user);
- $request->uid = $user['uid'];
- $request->uname = $user['uname'];
- $request->roleid = $user['roleid'];
- } catch (ValidateException $validateException) {
- return json_show(CommonModel::$error_token, $validateException->getError());
- }
- }
- return $next($request);
- }
- //请求结束的回调(如果返回数据用的是app_show/error_show,即直接echo,则不会触发该方法)
- public function end(Response $response)
- {
- //只做记录,不做输出
- ActionLogModel::where('request_id', request()->request_id)->save([
- 'response' => $response->getContent(),
- 'uid' => request()->uid ?? 0,
- 'uname' => request()->uname ?? '',
- ]);
- }
- }
|