fastadmin中redis+token的实现方法

由于线上是linux系统,所以redis的安装和配置,应该有所区别。等回去的时候,再研究把。

主要是宝塔面板对redis的支持。不仅要下载安装redis,还要放行相关的端口。这种放行,好像有两部分,第一宝塔面板放行,第二云服务器放行。

再redis驱动里配置链接方式。并且新增一个方法

/**
 * 存储reqToken
 * @param   string $token   Token
 * @param   int    $user_id 会员ID
 * @param   int    $expire  过期时长,0表示无限,单位秒
 * @return bool
 */
public function set_reqtoken($token, $value,$expire = 0)
{


    // return 1111;
        $result = $this->handler->set($token, $value,$expire);
    
    
    return $result;
}

新增一个函数,不要继承api的基类,用来鉴定每次请求的token

<?php

namespace app\api\controller;

use app\common\library\Token;
use fast\Random;
use think\Request;
use think\Response;

// use app\common\library\Auth;
// use think\Config;
use think\exception\HttpResponseException;
// use think\exception\ValidateException;
// use think\Hook;
// use think\Lang;
// use think\Loader;
// use think\Request;
// use think\Response;
// use think\Route;
// use think\Validate;

class Reqtoken{
     /**
     * @var Request Request 实例
     */
    protected $request;

    /**
     * 默认响应输出类型,支持json/xml
     * @var string
     */
    protected $responseType = 'json';


    public function __construct(Request $request = null)
    {
        $this->request = is_null($request) ? Request::instance() : $request;

        // 控制器初始化
        // $this->_initialize();

        // // 前置操作方法
        // if ($this->beforeActionList) {
        //     foreach ($this->beforeActionList as $method => $options) {
        //         is_numeric($method) ?
        //             $this->beforeAction($options) :
        //             $this->beforeAction($method, $options);
        //     }
        // }
    }
    public function maketoken(){


        // if ($this->request->isPost()){
        if ($this->request->isPost()){
                // halt($this->request->param("appKey"));
            if(!$this->request->param('appKey')||$this->request->param('appKey')!="caotama123!!"){
                $this->error("apitoken不合法!");
            }
            
            $reqtoken=Random::uuid();

            // halt(Token);
            Token::set_reqtoken("req:".$reqtoken, time().'', 3600);
            // Token::set($reqtoken, 1, 3600);
    
            // return time().'';
            // return $reqtoken;
            $this->success('请求成功',['reqtoken'=>$reqtoken]);
        }else{
            return "1111";
        }

        
    }




    /**
     * 操作成功返回的数据
     * @param string $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码,默认为1
     * @param string $type   输出类型
     * @param array  $header 发送的 Header 信息
     */
   public function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
    {

        // halt(111111);
        $this->result($msg, $data, $code, $type, $header);
    }

    /**
     * 操作失败返回的数据
     * @param string $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码,默认为0
     * @param string $type   输出类型
     * @param array  $header 发送的 Header 信息
     */
   public function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码,默认为0
     * @param string $type   输出类型,支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    public function result($msg, $data = null, $code = 0, $type = null, array $header = [])
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
        ];

       
        // 如果未设置类型则自动判断
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
        
        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }


        // halt(21212);
        $response = Response::create($result, $type, $code)->header($header);

        // halt(21212);
        throw new HttpResponseException($response);
    }
}

上一篇:fastadmin展示多个视频按钮


下一篇:fastadmin 压缩js和css的时候,出现错误