<?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;
}
}