当前位置: 首页 > news >正文

thinkphp6项目初始化配置方案二次修正版本

数据返回统一格式

app/BaseController.php新增文件内容在末尾,并在构造函数中实例化数据模型类

// 成功统一返回格式
function Result($data, $msg = '', $code = 200, $httpCode = 200): \think\response\Json
{$res = ['code' => $code,'msg'  => $msg,'data' => $data];return json($res, $httpCode);
}// 成功返回,无数据格式
function ResultOK($msg = 'ok', $code = 200, $httpCode = 200): \think\response\Json
{$res = ['code' => $code,'msg'  => $msg,'data' => null];return json($res, $httpCode);
}// 失败返回
function ResultError($msg = "error", $code = 400, $httpCode = 200): \think\response\Json
{$res = ['code' => $code,'msg'  => $msg,'data' => null];return json($res, $httpCode);
}

在config目录新建redis.php

<?php
return [//激活Token"active_pre" => "active_account_pre_",//登录Token"token_pre" => "access_token_pre_",//登录Token过期时间"login_expire" => 24 * 3600,//重置密码Token"reset_pre" => "reset_password_pre_",//登录验证码"code_pre" => "login_pre_",//登录验证码过期时间"code_expire" => 120,//登录验证码错误次数"code_error" => 3,//文件数据过期时间"file_expire" => 3600 / 4,
];

修改全局报错返回数据样式app/ExceptionHandle.php

<?php
namespace app;use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;/*** 应用异常处理类*/
class ExceptionHandle extends Handle
{/*** 不需要记录信息(日志)的异常类列表* @var array*/protected $ignoreReport = [HttpException::class,HttpResponseException::class,ModelNotFoundException::class,DataNotFoundException::class,ValidateException::class,];/*** 记录异常信息(包括日志或者其它方式记录)** @access public* @param  Throwable $exception* @return void*/public function report(Throwable $exception): void{// 使用内置的方式记录异常日志parent::report($exception);}/*** Render an exception into an HTTP response.** @access public* @param \think\Request $request* @param Throwable $e* @return Response*/public function render(\think\Request $request, Throwable $e): Response{// 自定义异常处理机制$response = ['code' => 500,'msg'  => $e->getMessage(),'data' => null,];return json($response);}
}

修改缓存配置config/cache.php

<?php// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------return [// 默认缓存驱动'default' => 'redis',// 缓存连接方式配置'stores'  => ['file' => [// 驱动方式'type'       => 'File',// 缓存保存目录'path'       => '',// 缓存前缀'prefix'     => '',// 缓存有效期 0表示永久缓存'expire'     => 0,// 缓存标签前缀'tag_prefix' => 'tag:',// 序列化机制 例如 ['serialize', 'unserialize']'serialize'  => [],],// 更多的缓存连接'redis'=>[// 驱动方式'type'       => 'redis',// 服务器地址'host'       => '****',// 服务器端口'port'       => 6379,// 密码'password'   => '',// 缓存有效期 0表示永久缓存'expire'     => 0]],
];

给用户颁发jwt鉴权证书

安装 firebase/php-jwt:

composer require firebase/php-jwt

创建自定义中间件 Auth:

前置中间件

<?php
namespace app\middleware;use think\Middleware;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\facade\Config;
use think\Request;
use think\Response;class Auth extends Middleware
{public function handle(Request $request, \Closure $next){// 从请求头获取JWT$token = $request->header('Authorization');if (!$token) {return json(['code' => 401, 'msg' => '未提供Token', 'data' => null], 401);}try {// 解析JWT$decoded = JWT::decode($token, new Key(Config::get('app.jwt_secret'), 'HS256'));$userId = $decoded->uid;// 查询用户信息$user = Db::table('staff')->where('id', $userId)->where('status', 1)->find();if (!$user) {return json(['code' => 403, 'msg' => '账户异常', 'data' => null], 403);}// 将用户信息传递给控制器$request->user = $user;} catch (\Exception $e) {return json(['code' => 401, 'msg' => '账号或密码错误', 'data' => null], 401);}return $next($request);}
}
http://www.lryc.cn/news/416638.html

相关文章:

  • XXE靶机教学
  • 干货 | 2024步入数字化转型深水区,云原生业务稳定性如何保障(免费下载)
  • for(char c:s),std::vector<int> numbers 和std::int numbers[],.size()和.sizeof()区别
  • 桌面云备份可以删除吗?安不安全
  • 【爬虫实战】利用代理爬取电商数据
  • python如何统计列表中元素出现的次数
  • 【算法】山脉数组的峰顶索引
  • 牛客 JZ31.栈的压入,弹出序列 C++写法
  • PageHelper在Mybatis的一对多表关联时total数错误
  • (20240806)硫氧镁 / 碱式硫酸镁-混凝土
  • string类的模拟实现(C++)
  • C++_sizeof的相关知识点
  • Istio Proxy的Envoy代理架构中,Upstream提供的功能是:
  • LeetCode 热题 HOT 100 (015/100)【宇宙最简单版】
  • 【HarmonyOS】鸿蒙应用实现截屏
  • Conda包依赖侦探:conda inspect命令全解析
  • 数模——灰色关联分析算法
  • Python爬虫技术 第27节 API和RESTful服务
  • 音视频入门基础:WAV专题(4)——FFmpeg源码中获取WAV文件音频压缩编码格式、采样频率、声道数量、采样位数、码率的实现
  • 环境变量在Conda中的魔法:控制包安装的秘诀
  • VS Code C/C++ MSVC编译器
  • 【技巧】IDEA 个性化配置
  • `pytest` 中一些常用的选项
  • fme从json中提取位置到kml中
  • 在Ubuntu 18.04上安装和配置pgAdmin 4服务器模式的方法
  • NiFi :1 初识这把“十年一剑”的利器
  • Pyside6实战教程专栏目录
  • 【Dash】使用 Dash Design Kit (DDK) 创建图表
  • C++ 几何算法 - 向量点乘,叉乘及其应用
  • Taro学习记录(具体项目实践)