fastadmin php jwt(token)登录

1.公共base接口 

<?php

namespace app\api\controller;

use app\api\controller\Code;
use app\common\library\Auth;
use app\common\controller\Api;
use app\common\model\Account;
use think\Db;


/**
 * 登录注册接口
 */
class Base extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    protected $userInfo = [];

    protected $is_Login = False;

    protected $salt = 'Inspection';


    /**
     * Notes:登陆
     * Author: yun
     * DateTime: 2021/5/13 16:04
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize () {
        //移除HTML标签
        $this->request->filter('strip_tags');
        $this->auth = Auth::instance();
        $modulename = $this->request->module();
        $controllername = strtolower($this->request->controller());
        $actionname = strtolower($this->request->action());
        // token
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
        $path = str_replace('.', '/', $controllername) . '/' . $actionname;
        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        // 检测是否需要验证登录
        if (!$this->auth->match($this->noNeedLogin) && $this->noNeedLogin!='*')
        {
            $token = Db::name("devops_personnel_token")->where('token',$token)->find();
            //检测是否登录
            if (!$token['token'])
            {
                $this->error(__('Please login first'), null, 401);
            }
            // 检测是否过期
            if ($token['expiretime']!=0){
                if ($token['expiretime']<time()){
                    // 过期了
                    $this->error(__('Please login first'), null, 401);
                }else{
                    // 更新下token
                    Db('devops_personnel_token')->where('id',$token['id'])->update(['expiretime'=>time()+7200]);
                }
            }
            $user = Db::name("devops_personnel")->where(['id'=>$token['devops_id']])->find();
            $this->is_Login = true;
            $this->userInfo = $user;
        }
    }

    /**
     * Notes:获取本人用户信息
     * DateTime: 2021/5/13 16:36
     * @return array
     */
    public function getUserInfo()
    {
        if ($this->is_Login){
            return $this->userInfo;
        }else{
            $this->error(__('Please login first'), null, 401);
        }
    }

}

2.登录接口 

<?php

namespace app\api\controller;

use app\api\controller\Code;
use app\common\model\Account;
use think\Db;



/**
 * 登录注册接口
 */
class Inlogin extends Base
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    protected $userInfo = [];

    protected $salt = 'Inspection';
    public function __construct () {
        parent::__construct();
    }
    /**
     * Notes:登陆
     * Author: yun
     * DateTime: 2021/5/13 16:04
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function doLogin () {

        $username = $this->request->request('username')?trim($this->request->request('username')):'';
        $password = $this->request->request('password')?trim($this->request->request('password')):'';
        $user = Db::name("devops_personnel")->where(['username'=>$username])->find();
        $msg = "登录成功";
        if ($user) {
            // 判断密码
            if (md5(md5($password).'tg_') == $user['password']){
                // 存token
                $token = md5(md5($user['id'].'-'.'name').$this->salt);
                Db::name("devops_personnel_token")->insert(['devops_id'=>$user['id'],'token'=>$token,'createtime'=>time(),'expiretime'=>0]);
                $user['token'] = $token;
                $this->userInfo = $user;
                $data = ['userinfo' => $this->userInfo];
                $this->success($msg, $data,0);
            }else{
                $this->error('密码错误');
            }
        }else{
            $this->error('账号不存在');
        }
    }
}

3.token表结构

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值