thinkphp6 公共方法

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值