| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020-03-24
- *
- *
- */
- namespace app\middleware;
- use app\controller\Auth;
- use app\Request;
- use think\Response;
- use app\exceptions\AuthException;
- class UserTokenMiddleware
- {
- public function handle(Request $request,\Closure $next)
- {
- $XToken = $request->header('X-Token');
- if(!$XToken) throw new AuthException('token不存在');
- $token = trim($request->header('X-Token'));
- if (!$token) throw new AuthException('无效的token');
- $data = Auth::checkToken($token,$request->param());
- if(!$data) throw new AuthException('无效的token!');
- if(isset($data['code']) && $data['code'] == 101) throw new AuthException('用户不存在');
- if(isset($data['code']) && $data['code'] == 500) throw new AuthException('系统错误');
- $request->macro('token', function () use (&$token) {
- return $token;
- });
- $request->macro('userInfo', function () use (&$data) {
- return $data['userInfo'];
- });
- $request->macro('uid', function () use (&$data) {
- return $data['userInfo']['uid'];
- });
- return $next($request);
- }
- public function after(Response $response)
- {
- // TODO: Implement after() method.
- }
- }
|