1.public目录下隐藏入口文件
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ api.php?/$1 [QSA,PT,L]
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
2.处理跨域预检请求
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
//允许的源域名
header("Access-Control-Allow-Origin: *");
//允许的请求头信息
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
//允许的请求类型
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
exit;
}
3.验证码路由
Route::get('captcha/:id', "\\think\\captcha\\CaptchaController@index");//访问图片需要
Route::get('captcha', 'Login/captcha');
? ?获取验证码:
/**
* 获取验证码图片地址
*/
public function captcha(Request $request): Json
{
$id = mt_rand(100000, 999999);
//验证码标识
$uniqid = uniqid("$id");
//返回数据 验证码图片路径、验证码标识
$data = [
'src' => $request->domain() . captcha_src($uniqid),
'uniqid' => $uniqid
];
return success($data);
}
4.公共的方法:成功方法,失败方法,无限极分类,树形结构
<?php
// 这是系统自动生成的公共文件
//公共响应函数
if (!function_exists("responses")) {
function responses($code, $msg, $data): \think\response\Json
{
return json([
'code' => $code,
'msg' => $msg,
'data' => $data,
]);
}
}
//响应成功
if (!function_exists("success")) {
function success($data = [], $code = 200, $msg = "success"): \think\response\Json
{
return responses($code, $msg, $data);
}
}
//响应失败
if (!function_exists("fail")) {
function fail($msg = "", $code = 500, $data = []): \think\response\Json
{
return responses($code, $msg, $data);
}
}
if(!function_exists("get_cate_list")){
//返回无限级分类菜单
function get_cate_list($list,$pid=0,$f=0){
//声明一个静态数组存储处理后的数据
static $arr = [];
foreach($list as $val){
if($val['pid']==$pid){
$val['level'] = $f;
$arr[]=$val;
get_cate_list($list,$val['id'],$f+1);
}
}
return $arr;
}
}
if(!function_exists('get_tree_list')){
//引用方式实现 父子级树状结构
function get_tree_list($list){
//将每条数据中的id值作为其下标
$temp = [];
foreach($list as $v){
$v['son'] = [];
$temp[$v['id']] = $v;
}
//获取分类树
foreach($temp as $k=>$v){
$temp[$v['pid']]['son'][] = &$temp[$v['id']];
}
return isset($temp[0]['son']) ? $temp[0]['son'] : [];
}
}
//递归,rbac层级菜单,无限极分类
if (!function_exists('rbac')) {
function rbac($node,$pid=0,$level=1)
{
static $arr = [];
foreach ($node as $val) {
if ($val['pid'] == $pid) {
$val['level'] = $level;
$arr[] = $val;
rbac($node, $val['nid'], $level + 1);
}
}
return $arr;
}
}
//密码加盐处理
if (!function_exists("encrypt_password")) {
function encrypt_password($password)
{
$salt = "123123";
return md5(md5(trim($password)) . $salt);
}
}
5.退出登录
/**
* 后台退出接口
*/
public function logout()
{
//取出Token值(寄托在header)
//return success("[]");
$token = JWT::getRequestToken();
//查看缓存中是否存在delete_token这个键
$delete_token = cache("delete_token") ?: [];
//将这个token值放入delete_token数组中
$delete_token[] = $token;
//将数组塞回缓存中
cache("delete_token", $delete_token,86400);
//销毁成功
return success();
}
|