Fastadmin框架TOKEN的使用

大前提:

一、在需要获取用户信息的接口,前端需要在调用api的时候在头部传token,这个token在登录的时候返回给前端。

二、所有的操作都是基于User用户表进行的,如果是别的用户表需要进行相关的改动。改动的地方就两个文件。API跟Auth这两个类库里面进行系列操作即可。

零、终极大招

$this->auth->direct($info->id);
$info['token']=$this->auth->getToken();
$this->success('登录成功',$this->auth->getUserinfo());

这样就完成了一个账号的登录,但是要注意账号各个参数,比如说status是否是'normal',是否被限制登录了等等,基本上都可以直接完成登录

 

 

 

 

一、登录获取token

如果是走系统默认的User里面的登录,是没问题,会给你返回一个token。如果是自定义的登录也想要token,就需要进行三步。

①登录类库继承 app\common\controller\API,并且使用 如下方法获取token

Api::getRefreshtoken($userid);    //这里传值是传的user_id

②app\common\controller\Api 里面新增方法:

/*创建token*/
    public function getRefreshtoken($user_id){
        return $this->auth->getRefreshtoken($user_id);
    }

③app\common\library\Auth里面新增方法:

public function getRefreshtoken($user_id){
        $this->direct($user_id);
        return $this->_token;
    }

二、通过有效的token获取用户信息

①继承 app\common\controller\Api 类,并且在当前类库里面加上以下代码:

protected $noNeedLogin = ['login','test', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
protected $noNeedRight = '*';

public function _initialize()
{
   parent::_initialize();

}



测试通过api头部带token获取用户信息
public function getinfos(){
        $info=Api::getUser();
        return json($info);
}

② app\common\controller\API里面添加下面的方法:

/*获取用户信息*/
public function getUser(){
    return $this->auth->getUser();
}

 

你可能感兴趣的:(ThinkPHP,Fastadmin插件,技术栈,php)