12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- // +----------------------------------------------------------------------
- // | [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018-2020 rights reserved.
- // +----------------------------------------------------------------------
- // | Author: TABLE ME
- // +----------------------------------------------------------------------
- // | Date: 2020-08-30 14:59
- // +----------------------------------------------------------------------
- namespace app\api\middleware;
- use app\Request;
- use app\model\api\User as UserModel;
- use Firebase\JWT\JWT;
- use library\exceptions\AuthException;
- use library\interfaces\MiddlewareInterface;
- use think\facade\Env;
- class UserMiddleware implements MiddlewareInterface
- {
- public function handle(Request $request, \Closure $next)
- {
- $token =$request->header('token','');
- $userData = $this->checkUser($token);
- $request->user = $userData;
- return $next($request);
- }
- /**
- * 检查数据是否正常
- * @param $secret_key
- */
- private function checkUser($token) {
- if(empty($token)) {
- throw new AuthException('用户登录凭证错误', 410000);
- }
- try{
- $memData = (new UserModel)
- ->alias("u")
- // ->field("u.*,IFNULL(wt.title,'') as work_type_title")
- ->field("u.*,wt.title as work_type_title")
- ->join("user_work_type wt","wt.id = u.work_type_id","left")
- ->where('u.token',$token)
- ->find();
- if(empty($memData)) {
- throw new AuthException('用户不存在', 410000);
- }
- if($memData['status']==0) {
- throw new AuthException('用户审核中', 410000);
- }
- if($memData['status']==-1) {
- throw new AuthException('用户已被禁用', 410000);
- }
- return $memData->toArray();
- }catch (\Throwable $e) {
- throw new AuthException('系统错误,请重新登录', 410000);
- }
- }
- }
|