REAPI/app/channel/service/ProductCategory.php
2024-09-29 15:43:18 +08:00

102 lines
3.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\channel\service;
use app\core\Service;
use think\admin\extend\DataExtend;
use think\admin\service\AdminService;
use think\admin\service\NodeService;
class ProductCategory extends Service
{
protected $table='product_category';
public function getTrees($pid=0,$level=0,$cate_array=[]){
if(empty($cate_array)) {
$cate_array=$this->db()->where('id|pid','=',$pid)->where('status','=',1)->select()->toArray();
}
static $list = [];
foreach ($cate_array as $key => $value){
//第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
if ($value['pid'] == $pid){
//父节点为根节点的节点,级别为0也就是第一级
$value['level'] = $level;
//把数组放到list中
$list[] = $value;
//把这个节点从数组中移除,减少后续递归消耗
unset($cate_array[$key]);
//开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
$this->getTrees($value['id'], $level+1,$cate_array);
}
}
return $list;
}
/**
* 菜单分组语言包
* @param string $name
* @return string
*/
private static function lang(string $name): string
{
$lang = lang("menus_{$name}");
if (stripos($lang, 'menus_') === 0) {
return lang(substr($lang, 6));
} else {
return $lang;
}
}
/**
* 获取可选菜单节点
* @param boolean $force 强制刷新
* @return array
*/
public static function getList(bool $force = false): array
{
$nodes = sysvar('think-library-menus') ?: [];
if (empty($force) && count($nodes) > 0) return $nodes; else $nodes = [];
foreach (NodeService::getMethods($force) as $node => $method) {
if ($method['ismenu']) $nodes[] = ['node' => $node, 'title' => self::lang($method['title'])];
}
return sysvar('think-library-menus', $nodes);
}
/**
* 获取系统菜单树数据
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getTree(): array
{
$menus = $this->db()->where(['status' => 1])->order('sort desc,id asc')->select()->toArray();
if (function_exists('admin_menu_filter')) $menus = call_user_func('admin_menu_filter', $menus);
foreach ($menus as &$menu) $menu['title'] = self::lang($menu['title']);
return static::filter(DataExtend::arr2tree($menus));
}
/**
* 后台主菜单权限过滤
* @param array $menus 当前菜单列表
* @return array
*/
private static function filter(array $menus): array
{
foreach ($menus as $key => &$menu) {
if (!empty($menu['sub'])) {
$menu['sub'] = static::filter($menu['sub']);
}
if (!empty($menu['sub'])) {
$menu['url'] = '#';
} elseif (empty($menu['url']) || $menu['url'] === '#' || !(empty($menu['node']) || AdminService::check($menu['node']))) {
unset($menus[$key]);
} elseif (preg_match('#^(https?:)?//\w+#i', $menu['url'])) {
if ($menu['params']) $menu['url'] .= (strpos($menu['url'], '?') === false ? '?' : '&') . $menu['params'];
} else {
$node = join('/', array_slice(str2arr($menu['url'], '/'), 0, 3));
$menu['url'] = admuri($menu['url']) . ($menu['params'] ? '?' . $menu['params'] : '');
if (!AdminService::check($node)) unset($menus[$key]);
}
}
return $menus;
}
}