Skip to content

Commit 05ed749

Browse files
author
283591387@qq.com
committed
增加删除菜单时刷新缓存与修改菜单按钮时同步刷新角色权限缓存
1 parent 0e666e0 commit 05ed749

File tree

13 files changed

+160
-52
lines changed

13 files changed

+160
-52
lines changed

.Net6版本/VOL.Core/UserManager/UserContext.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public UserInfo GetUserInfo(int userId)
124124
private static readonly Dictionary<int, List<Permissions>> rolePermissions = new Dictionary<int, List<Permissions>>();
125125

126126

127+
127128
/// <summary>
128129
/// 获取用户所有的菜单权限
129130
/// </summary>
@@ -136,6 +137,22 @@ public List<Permissions> Permissions
136137
}
137138
}
138139

140+
/// <summary>
141+
/// 菜单按钮变更时,同时刷新权限缓存2022.05.23
142+
/// </summary>
143+
/// <param name="menuId"></param>
144+
public void RefreshWithMenuActionChange(int menuId)
145+
{
146+
foreach (var roleId in rolePermissions.Where(c => c.Value.Any(x => x.Menu_Id == menuId)).Select(s => s.Key))
147+
{
148+
if (rolePermissionsVersion.ContainsKey(roleId))
149+
{
150+
CacheService.Add(roleId.GetRoleIdKey(), DateTime.Now.ToString("yyyyMMddHHMMssfff"));
151+
}
152+
}
153+
154+
}
155+
139156
/// <summary>
140157
/// 获取单个表的权限
141158
/// </summary>
@@ -173,9 +190,11 @@ private List<Permissions> ActionToArray(List<Permissions> permissions)
173190
{
174191
try
175192
{
193+
var menuAuthArr = x.MenuAuth.DeserializeObject<List<Sys_Actions>>();
176194
x.UserAuthArr = string.IsNullOrEmpty(x.UserAuth)
177195
? new string[0]
178-
: x.UserAuth.Split(",");
196+
: x.UserAuth.Split(",").Where(c => menuAuthArr.Any(m => m.Value == c)).ToArray();
197+
179198
}
180199
catch { }
181200
finally

.Net6版本/VOL.System/IServices/System/Partial/ISys_MenuService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public partial interface ISys_MenuService
1717
Task<object> GetMenuActionList(int roleId);
1818
Task<WebResponseContent> Save(Sys_Menu menu);
1919

20+
Task<WebResponseContent> DelMenu(int menuId);
21+
22+
2023
Task<object> GetTreeItem(int menuId);
2124
}
2225
}

.Net6版本/VOL.System/Services/System/Partial/Sys_MenuService.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ public async Task<object> GetMenu()
5353
private List<Sys_Menu> GetAllMenu()
5454
{
5555
//每次比较缓存是否更新过,如果更新则重新获取数据
56-
if (_menuVersionn != "" && _menuVersionn == CacheContext.Get(_menuCacheKey))
56+
string _cacheVersion = CacheContext.Get(_menuCacheKey);
57+
if (_menuVersionn != "" && _menuVersionn == _cacheVersion)
5758
{
5859
return _menus ?? new List<Sys_Menu>();
5960
}
6061
lock (_menuObj)
6162
{
62-
if (_menuVersionn != "" && _menus != null) return _menus;
63+
if (_menuVersionn != "" && _menus != null && _menuVersionn == _cacheVersion) return _menus;
6364
//2020.12.27增加菜单界面上不显示,但可以分配权限
6465
_menus = repository.FindAsIQueryable(x => x.Enable == 1 || x.Enable == 2)
6566
.OrderByDescending(a => a.OrderNo)
@@ -199,7 +200,7 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
199200
}
200201
}
201202
}
202-
203+
bool _changed = false;
203204
if (menu.Menu_Id <= 0)
204205
{
205206
repository.Add(menu.SetCreateDefaultVal());
@@ -215,6 +216,9 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
215216
{
216217
return webResponse.Error($"不能选择此父级id,选择的父级id与当前菜单形成依赖关系");
217218
}
219+
220+
_changed = repository.FindAsIQueryable(c => c.Menu_Id == menu.Menu_Id).Select(s => s.Auth).FirstOrDefault() != menu.Auth;
221+
218222
repository.Update(menu.SetModifyDefaultVal(), p => new
219223
{
220224
p.ParentId,
@@ -231,7 +235,12 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
231235
});
232236
}
233237
await repository.SaveChangesAsync();
234-
_menuVersionn = DateTime.Now.ToString("yyyyMMddHHMMssfff");
238+
239+
CacheContext.Add(_menuCacheKey, DateTime.Now.ToString("yyyyMMddHHMMssfff"));
240+
if (_changed)
241+
{
242+
UserContext.Current.RefreshWithMenuActionChange(menu.Menu_Id);
243+
}
235244
_menus = null;
236245
webResponse.OK("保存成功", menu);
237246
}
@@ -247,6 +256,21 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
247256

248257
}
249258

259+
public async Task<WebResponseContent> DelMenu(int menuId)
260+
{
261+
WebResponseContent webResponse =new WebResponseContent();
262+
263+
if (await repository.ExistsAsync(x => x.ParentId == menuId))
264+
{
265+
return webResponse.Error("当前菜单存在子菜单,请先删除子菜单!");
266+
}
267+
repository.Delete(new Sys_Menu()
268+
{
269+
Menu_Id = menuId
270+
}, true);
271+
CacheContext.Add(_menuCacheKey, DateTime.Now.ToString("yyyyMMddHHMMssfff"));
272+
return webResponse.OK("删除成功");
273+
}
250274
/// <summary>
251275
/// 编辑菜单时,获取菜单信息
252276
/// </summary>

.Net6版本/VOL.WebApi/Controllers/System/Partial/Sys_MenuController.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,9 @@ public async Task<ActionResult> Save([FromBody] Sys_Menu menu)
4949
/// <returns></returns>
5050
[ApiActionPermission(ActionRolePermission.SuperAdmin)]
5151
[HttpPost, Route("delMenu")]
52-
public async Task<ActionResult> DelMenu(int menuId)
52+
public async Task<ActionResult> DelMenu(int menuId)
5353
{
54-
Core.Utilities.WebResponseContent webResponse = new Core.Utilities.WebResponseContent();
55-
var repository = Repositories.Sys_MenuRepository.Instance;
56-
if (await repository.ExistsAsync(x => x.ParentId == menuId))
57-
{
58-
return Json(webResponse.Error("当前菜单存在子菜单,请先删除子菜单!"));
59-
}
60-
repository.Delete(new Sys_Menu()
61-
{
62-
Menu_Id = menuId
63-
},true);
64-
return Json(webResponse.OK("删除成功"));
54+
return Json(await Service.DelMenu(menuId));
6555
}
6656

6757
}

Vol.Vue/src/views/document/docApi/doc_table.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
>查看vue3代码</Button
210210
>
211211
</div>
212-
<!-- <docParamTable name="voltable"></docParamTable> -->
212+
<docParamTable name="voltable"></docParamTable>
213213
<br />
214214
<br />
215215
<VolBox

Vue.Net/VOL.Core/UserManager/UserContext.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public UserInfo GetUserInfo(int userId)
124124
private static readonly Dictionary<int, List<Permissions>> rolePermissions = new Dictionary<int, List<Permissions>>();
125125

126126

127+
127128
/// <summary>
128129
/// 获取用户所有的菜单权限
129130
/// </summary>
@@ -136,6 +137,22 @@ public List<Permissions> Permissions
136137
}
137138
}
138139

140+
/// <summary>
141+
/// 菜单按钮变更时,同时刷新权限缓存2022.05.23
142+
/// </summary>
143+
/// <param name="menuId"></param>
144+
public void RefreshWithMenuActionChange(int menuId)
145+
{
146+
foreach (var roleId in rolePermissions.Where(c => c.Value.Any(x => x.Menu_Id == menuId)).Select(s => s.Key))
147+
{
148+
if (rolePermissionsVersion.ContainsKey(roleId))
149+
{
150+
CacheService.Add(roleId.GetRoleIdKey(), DateTime.Now.ToString("yyyyMMddHHMMssfff"));
151+
}
152+
}
153+
154+
}
155+
139156
/// <summary>
140157
/// 获取单个表的权限
141158
/// </summary>
@@ -173,9 +190,11 @@ private List<Permissions> ActionToArray(List<Permissions> permissions)
173190
{
174191
try
175192
{
193+
var menuAuthArr = x.MenuAuth.DeserializeObject<List<Sys_Actions>>();
176194
x.UserAuthArr = string.IsNullOrEmpty(x.UserAuth)
177195
? new string[0]
178-
: x.UserAuth.Split(",");
196+
: x.UserAuth.Split(",").Where(c => menuAuthArr.Any(m => m.Value == c)).ToArray();
197+
179198
}
180199
catch { }
181200
finally

Vue.Net/VOL.System/IServices/System/Partial/ISys_MenuService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public partial interface ISys_MenuService
1717
Task<object> GetMenuActionList(int roleId);
1818
Task<WebResponseContent> Save(Sys_Menu menu);
1919

20+
Task<WebResponseContent> DelMenu(int menuId);
21+
22+
2023
Task<object> GetTreeItem(int menuId);
2124
}
2225
}

Vue.Net/VOL.System/Services/System/Partial/Sys_MenuService.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ public async Task<object> GetMenu()
5353
private List<Sys_Menu> GetAllMenu()
5454
{
5555
//每次比较缓存是否更新过,如果更新则重新获取数据
56-
if (_menuVersionn != "" && _menuVersionn == CacheContext.Get(_menuCacheKey))
56+
string _cacheVersion = CacheContext.Get(_menuCacheKey);
57+
if (_menuVersionn != "" && _menuVersionn == _cacheVersion)
5758
{
5859
return _menus ?? new List<Sys_Menu>();
5960
}
6061
lock (_menuObj)
6162
{
62-
if (_menuVersionn != "" && _menus != null) return _menus;
63+
if (_menuVersionn != "" && _menus != null && _menuVersionn == _cacheVersion) return _menus;
6364
//2020.12.27增加菜单界面上不显示,但可以分配权限
6465
_menus = repository.FindAsIQueryable(x => x.Enable == 1 || x.Enable == 2)
6566
.OrderByDescending(a => a.OrderNo)
@@ -199,7 +200,7 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
199200
}
200201
}
201202
}
202-
203+
bool _changed = false;
203204
if (menu.Menu_Id <= 0)
204205
{
205206
repository.Add(menu.SetCreateDefaultVal());
@@ -215,6 +216,9 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
215216
{
216217
return webResponse.Error($"不能选择此父级id,选择的父级id与当前菜单形成依赖关系");
217218
}
219+
220+
_changed = repository.FindAsIQueryable(c => c.Menu_Id == menu.Menu_Id).Select(s => s.Auth).FirstOrDefault() != menu.Auth;
221+
218222
repository.Update(menu.SetModifyDefaultVal(), p => new
219223
{
220224
p.ParentId,
@@ -231,7 +235,12 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
231235
});
232236
}
233237
await repository.SaveChangesAsync();
234-
_menuVersionn = DateTime.Now.ToString("yyyyMMddHHMMssfff");
238+
239+
CacheContext.Add(_menuCacheKey, DateTime.Now.ToString("yyyyMMddHHMMssfff"));
240+
if (_changed)
241+
{
242+
UserContext.Current.RefreshWithMenuActionChange(menu.Menu_Id);
243+
}
235244
_menus = null;
236245
webResponse.OK("保存成功", menu);
237246
}
@@ -247,6 +256,21 @@ public async Task<WebResponseContent> Save(Sys_Menu menu)
247256

248257
}
249258

259+
public async Task<WebResponseContent> DelMenu(int menuId)
260+
{
261+
WebResponseContent webResponse =new WebResponseContent();
262+
263+
if (await repository.ExistsAsync(x => x.ParentId == menuId))
264+
{
265+
return webResponse.Error("当前菜单存在子菜单,请先删除子菜单!");
266+
}
267+
repository.Delete(new Sys_Menu()
268+
{
269+
Menu_Id = menuId
270+
}, true);
271+
CacheContext.Add(_menuCacheKey, DateTime.Now.ToString("yyyyMMddHHMMssfff"));
272+
return webResponse.OK("删除成功");
273+
}
250274
/// <summary>
251275
/// 编辑菜单时,获取菜单信息
252276
/// </summary>

Vue.Net/VOL.WebApi/Controllers/System/Partial/Sys_MenuController.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,9 @@ public async Task<ActionResult> Save([FromBody] Sys_Menu menu)
4949
/// <returns></returns>
5050
[ApiActionPermission(ActionRolePermission.SuperAdmin)]
5151
[HttpPost, Route("delMenu")]
52-
public async Task<ActionResult> DelMenu(int menuId)
52+
public async Task<ActionResult> DelMenu(int menuId)
5353
{
54-
Core.Utilities.WebResponseContent webResponse = new Core.Utilities.WebResponseContent();
55-
var repository = Repositories.Sys_MenuRepository.Instance;
56-
if (await repository.ExistsAsync(x => x.ParentId == menuId))
57-
{
58-
return Json(webResponse.Error("当前菜单存在子菜单,请先删除子菜单!"));
59-
}
60-
repository.Delete(new Sys_Menu()
61-
{
62-
Menu_Id = menuId
63-
},true);
64-
return Json(webResponse.OK("删除成功"));
54+
return Json(await Service.DelMenu(menuId));
6555
}
6656

6757
}

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/UserManager/UserContext.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public UserInfo GetUserInfo(int userId)
124124
private static readonly Dictionary<int, List<Permissions>> rolePermissions = new Dictionary<int, List<Permissions>>();
125125

126126

127+
127128
/// <summary>
128129
/// 获取用户所有的菜单权限
129130
/// </summary>
@@ -136,6 +137,22 @@ public List<Permissions> Permissions
136137
}
137138
}
138139

140+
/// <summary>
141+
/// 菜单按钮变更时,同时刷新权限缓存2022.05.23
142+
/// </summary>
143+
/// <param name="menuId"></param>
144+
public void RefreshWithMenuActionChange(int menuId)
145+
{
146+
foreach (var roleId in rolePermissions.Where(c => c.Value.Any(x => x.Menu_Id == menuId)).Select(s => s.Key))
147+
{
148+
if (rolePermissionsVersion.ContainsKey(roleId))
149+
{
150+
CacheService.Add(roleId.GetRoleIdKey(), DateTime.Now.ToString("yyyyMMddHHMMssfff"));
151+
}
152+
}
153+
154+
}
155+
139156
/// <summary>
140157
/// 获取单个表的权限
141158
/// </summary>
@@ -173,9 +190,11 @@ private List<Permissions> ActionToArray(List<Permissions> permissions)
173190
{
174191
try
175192
{
193+
var menuAuthArr = x.MenuAuth.DeserializeObject<List<Sys_Actions>>();
176194
x.UserAuthArr = string.IsNullOrEmpty(x.UserAuth)
177195
? new string[0]
178-
: x.UserAuth.Split(",");
196+
: x.UserAuth.Split(",").Where(c => menuAuthArr.Any(m => m.Value == c)).ToArray();
197+
179198
}
180199
catch { }
181200
finally

0 commit comments

Comments
 (0)