quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
 
namespace app\operations\service\region;
 
use app\common\model\plus\operations\Access;
use app\common\service\region\AuthService;
 
/**
 * 区域代理菜单服务
 */
class MenusService
{
    /**
     * 获取菜单列表
     * @param int $userId 用户ID
     * @return array
     */
    public function getMenus($userId)
    {
        // 获取用户有权限的菜单
        return AuthService::getUserMenus($userId);
    }
 
    /**
     * 获取所有菜单列表(用于权限分配)
     * @return array
     */
    public function getAllMenus()
    {
        $accessModel = new Access();
        $accessList = $accessModel->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
        
        return $this->buildMenuTree($accessList ? $accessList->toArray() : []);
    }
 
    /**
     * 构建菜单树结构
     * @param array $data 权限数据
     * @param int $parentId 父级ID
     * @return array
     */
    private function buildMenuTree($data, $parentId = 0)
    {
        $tree = [];
        foreach ($data as $item) {
            if ($item['parent_id'] == $parentId) {
                $item['children'] = $this->buildMenuTree($data, $item['access_id']);
                $tree[] = $item;
            }
        }
        return $tree;
    }
 
    /**
     * 获取面包屑导航
     * @param string $path 当前路径
     * @return array
     */
    public function getBreadcrumb($path)
    {
        $accessModel = new Access();
        $current = $accessModel->where('path', '=', $path)->find();
        
        if (!$current) {
            return [];
        }
 
        $breadcrumb = [$current];
        $parentId = $current['parent_id'];
        
        while ($parentId > 0) {
            $parent = $accessModel->where('access_id', '=', $parentId)->find();
            if (!$parent) {
                break;
            }
            array_unshift($breadcrumb, $parent);
            $parentId = $parent['parent_id'];
        }
 
        return $breadcrumb;
    }
}