1
huangxiaoqiang
2025-05-26 41702c7ce4c88ad70f52d83d153dd4c596ff69d5
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using SqlSugar;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.System;
using WIDESEA_IRepository;
using WIDESEA_IServices;
using WIDESEA_Model;
using WIDESEA_Model.Models;
 
namespace WIDESEA_Services
{
    public class Sys_MenuService : ServiceBase<Sys_Menu, ISys_MenuRepository>, ISys_MenuService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
        public Sys_MenuService(ISys_MenuRepository BaseDal, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
        }
 
        /// <summary>
        /// 获取当前用户所有菜单与权限
        /// </summary>
        /// <returns></returns>
        public object GetCurrentMenuActionList()
        {
            object obj = GetMenuActionList(App.User.RoleId);
            if (obj is IEnumerable<object> list)
            {
                if (list.Any())
                {
                    return obj;
                }
                else
                {
                    return WebResponseContent.Instance.Error("未获取到菜单信息");
                }
            }
            return obj;
        }
 
        public object GetCurrentMenuPhoneActionList()
        {
            object obj = GetMenuActionPhoneList(App.User.RoleId);
            if (obj is IEnumerable<object> list)
            {
                if (list.Any())
                {
                    return obj;
                }
                else
                {
                    return WebResponseContent.Instance.Error("未获取到菜单信息");
                }
            }
            return obj;
        }
 
        /// <summary>
        /// 根据角色ID获取菜单与权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public object GetMenuActionList(int roleId)
        {
            //return BaseDal.GetMenuByRoleId(roleId);
            if (App.User.IsRoleIdSuperAdmin(roleId))
            {
                return BaseDal.GetSuperAdminMenu();
            }
            return BaseDal.GetMenuByRoleId(roleId);
        }
 
        /// <summary>
        /// 根据角色ID获取菜单与权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public object GetMenuActionPhoneList(int roleId)
        {
            var allMenu = BaseDal.QueryData(x => (x.Enable == 1 || x.Enable == 2) && x.MenuType == 1 && x.ParentId > 0).OrderByDescending(a => a.OrderNo).ThenByDescending(q => q.ParentId).ToList();
 
            var menu = allMenu.Select(x => new
            {
                text = x.MenuName,
                url = x.Url,
                icon = x.Icon,
            });
            return menu.ToList();
        }
 
        public List<MenuDTO> GetUserMenuList(int roleId)
        {
            if (App.User.IsRoleIdSuperAdmin(roleId))
            {
                return BaseDal.GetAllMenu();
            }
            List<int> menuIds = BaseDal.GetPermissions(roleId).Select(x => x.MenuId).ToList();
            return BaseDal.GetAllMenu().Where(x => menuIds.Contains(x.MenuId)).ToList();
        }
 
        public List<ActionDTO> GetActions(int menuId, List<ActionDTO> menuActions, List<Permissions> permissions, int roleId)
        {
            if (App.User.IsRoleIdSuperAdmin(roleId))
            {
                return menuActions;
            }
 
            return menuActions.Where(p => permissions
                 .Exists(w => menuId == w.MenuId
                 && w.UserAuthArr.Contains(p.Value)))
                  .ToList();
        }
 
        /// <summary>
        /// 编辑修改菜单时,获取所有菜单
        /// </summary>
        /// <returns></returns>
        public object GetMenu()
        {
            if (App.User.IsRoleIdSuperAdmin(App.User.RoleId))
            {
                List<int> menuIds = BaseDal.QueryData().Select(x => x.MenuId).ToList();
                return BaseDal.GetMenu(menuIds);
            }
            else
            {
                List<int> menuIds = BaseDal.GetPermissions(App.User.RoleId).Select(x => x.MenuId).ToList();
                return BaseDal.GetMenu(menuIds);
            }
        }
 
        /// <summary>
        /// 编辑菜单时,获取菜单信息
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public object GetTreeItem(int menuId)
        {
            return BaseDal.GetTreeItem(menuId);
        }
 
        /// <summary>
        /// 新建或编辑菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public WebResponseContent Save(Sys_Menu menu)
        {
            WebResponseContent webResponse = new WebResponseContent();
            if (menu == null) return webResponse.Error("没有获取到提交的参数");
            if (menu.MenuId > 0 && menu.MenuId == menu.ParentId) return webResponse.Error("父级ID不能是当前菜单的ID");
            try
            {
                //webResponse = menu.ValidationEntity(x => new { x.MenuName, x.TableName });
                //if (!webResponse.Status) return webResponse;
                if (menu.TableName != "/" && menu.TableName != ".")
                {
                    // 2022.03.26增移动端加菜单类型判断
                    Sys_Menu sysMenu = BaseDal.QueryFirst(x => x.TableName == menu.TableName);
                    if (sysMenu != null)
                    {
                        sysMenu.MenuType ??= 0;
                        if (sysMenu.MenuType == menu.MenuType)
                        {
                            if ((menu.MenuId > 0 && sysMenu.MenuId != menu.MenuId)
                            || menu.MenuId <= 0)
                            {
                                return webResponse.Error($"视图/表名【{menu.TableName}】已被其他菜单使用");
                            }
                        }
                    }
                }
                bool _changed = false;
                if (menu.MenuId <= 0)
                {
                    BaseDal.AddData(menu);
                }
                else
                {
                    //2020.05.07新增禁止选择上级角色为自己
                    if (menu.MenuId == menu.ParentId)
                    {
                        return webResponse.Error($"父级id不能为自己");
                    }
                    if (BaseDal.QueryFirst(x => x.ParentId == menu.MenuId && menu.ParentId == x.MenuId) != null)
                    {
                        return webResponse.Error($"不能选择此父级id,选择的父级id与当前菜单形成依赖关系");
                    }
 
                    _changed = BaseDal.QueryData(c => c.MenuId == menu.MenuId).Select(s => s.Auth).FirstOrDefault() != menu.Auth;
 
                    BaseDal.UpdateData(menu);
                }
                webResponse.OK("保存成功", menu);
            }
            catch (Exception ex)
            {
                webResponse.Error(ex.Message);
            }
            return webResponse;
        }
    }
}