Skip to content
关注公众号,获取新课通知
【重要通知】uniapp实战社区交友交流群更换为:602180461,靓仔/靓女可以重新申请加入哦~

auth中间件封装


权限验证层

  1. 创建中间件
php
// 检测token合法性(用户是否已登陆)
php think make:middleware ApiUserAuth
    
// 检测第三方登录用户是否已经绑定手机(如果没有绑定手机,在User主表中没有记录,没法进行下面操作)
php think make:middleware ApiUserBindPhone
    
// 检测用户是否被禁用
php think make:middleware ApiUserStatus
  1. 注册中间件(config/middleware.php)
php
return [
    'ApiUserAuth' => app\http\middleware\ApiUserAuth::class,
    'ApiUserStatus' => app\http\middleware\ApiUserStatus::class,
    'ApiUserBindPhone' => app\http\middleware\ApiUserBindPhone::class,
];
  1. 中间件定义

(1) 检测token合法性(app\http\middleware\ApiUserAuth.php)

php
<?php

namespace app\http\middleware;
// 引入异常类
use app\lib\exception\BaseException;

class ApiUserAuth
{
    public function handle($request, \Closure $next)
    {
        // 获取头部信息
        $param = $request->header();
        // 不含token
        if (!array_key_exists('token',$param)) throw new BaseException(['code'=>200,'msg'=>'非法token,禁止操作','errorCode'=>20003]);
        // 当前用户token是否存在(是否登录)
        $token = $param['token'];
        $user = \Cache::get($token);
        // 验证失败(未登录或已过期)
        if(!$user) throw new BaseException(['code'=>200,'msg'=>'非法token,请重新登录','errorCode'=>20003]);
        // 将token和userid这类常用参数放在request中
        $request->userToken = $token;
        $request->userId = array_key_exists('type',$user) ? $user['user_id'] : $user['id'];
        $request->userTokenUserInfo = $user; 
        return $next($request);
    }
}

(2)检测第三方登录用户是否已经绑定手机(app\http\middleware\ApiUserBindPhone.php)

php
<?php

namespace app\http\middleware;
use app\common\model\User;
class ApiUserBindPhone
{
    public function handle($request, \Closure $next)
    {
        $param = $request->userTokenUserInfo;
        (new User()) -> OtherLoginIsBindPhone($param);
        return $next($request);
    }
}

model层:application\common\model\User.php

php
// 验证第三方登录是否绑定手机
public function OtherLoginIsBindPhone($user){
    // 验证是否是第三方登录
    if(array_key_exists('type',$user)){
        if($user['user_id']<1){
            throw new BaseException(['code'=>200,'msg'=>'请先绑定手机!','errorCode'=>20008]);
        }
        return $user['user_id'];
    }
    // 账号密码登录
    return $user['id'];
}

(3)检测用户是否被禁用(app\http\middleware\ApiUserStatus.php)

php
<?php

namespace app\http\middleware;
use app\common\model\User;
class ApiUserStatus
{
    public function handle($request, \Closure $next)
    {
        $param = $request->userTokenUserInfo;
        (new User()) -> checkStatus($param,true);
        return $next($request);
    }
}

model层:application\common\model\User.php

php
// 用户是否被禁用(在前面课程的基础上扩充)
public function checkStatus($arr,$isReget = false){
    $status = 1;
    if ($isReget) {
        // 账号密码登录
        $userid = array_key_exists('user_id',$arr)?$arr['user_id']:$arr['id'];
        $user = $this->find($userid)->toArray();
        $status = $user['status'];
    }else{
        $status = $arr['status'];
    }
    if($status==0) throw new BaseException(['code'=>200,'msg'=>'该用户已被禁用','errorCode'=>20001]);
    return $arr;
}
  1. 中间件使用(在路由中使用 route/route.php)
php
// 需要验证token
Route::group('api/:version/',function(){
    ...
})->middleware(['ApiUserAuth']);

// 需要验证token,是否绑定手机,用户是否禁用(必须按顺序)
Route::group('api/:version/',function(){
    ...
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);