Browse Source

新增快捷入口操作相关接口

Dun.Jason 2 years ago
parent
commit
96a48e1d4c

+ 54 - 6
src/Hotline.Api/Controllers/HomeController.cs

@@ -1,9 +1,13 @@
 using Hotline.Repository.SqlSugar;
 using Hotline.Settings;
+using Hotline.Share.Dtos.Home;
+using MapsterMapper;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
 using SqlSugar;
 using XF.Domain.Authentications;
+using XF.Domain.Exceptions;
+
 namespace Hotline.Api.Controllers;
 
 /// <summary>
@@ -15,17 +19,23 @@ public class HomeController : BaseController
     private readonly ISessionContext _sessionContext;
     private readonly ISystemAuthorityRepository _systemAuthorityRepository;
     private readonly ISystemMenuRepository _systemMenuRepository;
+    private readonly IMapper _mapper;
+    private readonly IUserFastMenuRepository _userFastMenuRepository;
 
     public HomeController(
         ISugarUnitOfWork<HotlineDbContext> uow,
         ISessionContext sessionContext,
         ISystemAuthorityRepository systemAuthorityRepository,
-        ISystemMenuRepository systemMenuRepository)
+        ISystemMenuRepository systemMenuRepository,
+        IMapper mapper,
+        IUserFastMenuRepository userFastMenuRepository)
     {
         _uow = uow;
         _sessionContext = sessionContext;
         _systemAuthorityRepository = systemAuthorityRepository;
         _systemMenuRepository = systemMenuRepository;
+        _mapper = mapper;
+        _userFastMenuRepository = userFastMenuRepository;
     }
     
     #region 开放请求接口
@@ -36,14 +46,12 @@ public class HomeController : BaseController
     {
         var db = _uow.Db;
         db.DbMaintenance.CreateDatabase();
-        db.CodeFirst.InitTables<SystemMenu, SystemAuthority,SystemButton,SystemOrganize,OrgUser>();
-        db.CodeFirst.InitTables<SystemDataAuthority>();
-
+        db.CodeFirst.InitTables<UserFastMenu>();
         return Task.CompletedTask;
     }
 
     /// <summary>
-    /// 获取可选快入口
+    /// 获取可选快入口
     /// </summary>
     /// <returns></returns>
     [AllowAnonymous]
@@ -51,9 +59,49 @@ public class HomeController : BaseController
     public async Task<IReadOnlyList<SystemMenu>> GetFastMenuByToken()
     {
         var roles = _sessionContext.Roles;
-        return await _systemMenuRepository.GetFastMenuByToken(roles);
+        return await _systemMenuRepository.GetFastMenu(roles,_sessionContext.UserId);
+    }
+
+    /// <summary>
+    /// 设置快捷入口
+    /// </summary>
+    /// <returns></returns>
+    [AllowAnonymous]
+    [HttpPost("set-fastmenu")]
+    public async Task SetFastMenu([FromBody]SetFastMenuDto dto)
+    {
+        if (!string.IsNullOrEmpty(_sessionContext.UserId))
+        {
+            string userId = _sessionContext.UserId;
+            var model = await _userFastMenuRepository.GetAsync(x => x.UserId == dto.UserId);
+            if (model is null)
+            {
+                var fastmenu = _mapper.Map<UserFastMenu>(dto);
+                await _userFastMenuRepository.AddAsync(fastmenu);
+            }
+            else
+            {
+                _mapper.Map(dto, model);
+                await _userFastMenuRepository.UpdateAsync(model);
+            }
+        }
+        else
+        {
+            throw UserFriendlyException.SameMessage("无效用户");
+        }
     }
 
+    /// <summary>
+    /// 获取我的快捷入口
+    /// </summary>
+    /// <returns></returns>
+    [AllowAnonymous]
+    [HttpGet("get-myfastmenu")]
+    public async Task<IReadOnlyList<SystemMenu>> GetMyFastMenu()
+    {
+        var roles = _sessionContext.Roles;
+        return await _systemMenuRepository.GetMyFastMenu(roles, _sessionContext.UserId);
+    }
 
     #endregion
 

+ 46 - 5
src/Hotline.Repository.SqlSugar/SystemMenuRepository.cs

@@ -28,11 +28,11 @@ namespace Hotline.Repository.SqlSugar
 
 
         /// <summary>
-        /// 获取当前用户所有菜单
+        /// 获取可选快速入口(排除已有)
         /// </summary>
         /// <param name="roles"></param>
         /// <returns></returns>
-        public async Task<IReadOnlyList<SystemMenu>> GetFastMenuByToken(string[] roles)
+        public async Task<IReadOnlyList<SystemMenu>> GetFastMenu(string[] roles,string userId)
         {
             var list = await Db.Queryable<SystemAuthority>()
                 .Where(x => roles.Contains(x.RoleCode)).ToListAsync();
@@ -41,9 +41,50 @@ namespace Hotline.Repository.SqlSugar
             {
                 permissionarr.AddRange(item.SystemMenuArr);
             }
-            var menulist = await Db.Queryable<SystemMenu>()
-                .Where(x => x.IsFast && permissionarr.Contains(x.PermissionCode)).ToListAsync();
-            return menulist;
+            var model = await Db.Queryable<UserFastMenu>().FirstAsync(x => x.UserId == userId);
+            var perList = new List<string>();
+            if (model!=null)
+            {
+                perList = permissionarr.Except(model.FastMenuArr).ToList();
+            }
+            if (perList.Count>0)
+            {
+                //过滤已存在的
+                var menulist = await Db.Queryable<SystemMenu>()
+               .Where(x => x.IsFast && perList.Contains(x.PermissionCode)).ToListAsync();
+                return menulist;
+            }
+            else
+            {
+                var menulist = await Db.Queryable<SystemMenu>()
+                    .Where(x => x.IsFast).ToListAsync();
+                return menulist;
+            }
+            
+        }
+
+        /// <summary>
+        /// 获取我的快速入口
+        /// </summary>
+        /// <param name="roles"></param>
+        /// <param name="userId"></param>
+        /// <returns></returns>
+        public async Task<IReadOnlyList<SystemMenu>> GetMyFastMenu(string[] roles,string userId)
+        {
+            var list = await Db.Queryable<SystemAuthority>()
+                .Where(x => roles.Contains(x.RoleCode)).ToListAsync();
+            var permissionarr = new List<string>();
+            foreach (var item in list)
+            {
+                permissionarr.AddRange(item.SystemMenuArr);
+            }
+            var model = await Db.Queryable<UserFastMenu>().FirstAsync(x => x.UserId == userId);
+            if (model!=null)
+            {
+                var perList = permissionarr.Intersect(model.FastMenuArr).ToList();
+                return await Db.Queryable<SystemMenu>().Where(x => perList.Contains(x.PermissionCode)).ToListAsync();
+            }
+            return new List<SystemMenu>();
         }
     }
 }

+ 13 - 0
src/Hotline.Repository.SqlSugar/UserFastMenuRepository.cs

@@ -0,0 +1,13 @@
+using Hotline.Settings;
+using SqlSugar;
+using XF.Domain.Dependency;
+
+namespace Hotline.Repository.SqlSugar
+{
+    public class UserFastMenuRepository : BaseRepository<UserFastMenu>, IUserFastMenuRepository, IScopeDependency
+    {
+        public UserFastMenuRepository(ISugarUnitOfWork<HotlineDbContext> uow) : base(uow)
+        {
+        }
+    }
+}

+ 15 - 0
src/Hotline.Share/Dtos/Home/HomeDto.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hotline.Share.Dtos.Home
+{
+    public record SetFastMenuDto
+    {
+        public string UserId { get; set; }
+
+        public List<string> FastMenuArr { get; set; }
+    }
+}

+ 9 - 1
src/Hotline/Settings/ISystemMenuRepository.cs

@@ -12,6 +12,14 @@ namespace Hotline.Settings
         /// </summary>
         /// <param name="roles"></param>
         /// <returns></returns>
-        Task<IReadOnlyList<SystemMenu>> GetFastMenuByToken(string[] roles);
+        Task<IReadOnlyList<SystemMenu>> GetFastMenu(string[] roles,string userId);
+
+        /// <summary>
+        /// 获取我的快速入口
+        /// </summary>
+        /// <param name="roles"></param>
+        /// <param name="userId"></param>
+        /// <returns></returns>
+        Task<IReadOnlyList<SystemMenu>> GetMyFastMenu(string[] roles,string userId);
     }
 }

+ 8 - 0
src/Hotline/Settings/IUserFastMenuRepository.cs

@@ -0,0 +1,8 @@
+using XF.Domain.Repository;
+
+namespace Hotline.Settings
+{
+    public interface IUserFastMenuRepository : IRepository<UserFastMenu>
+    {
+    }
+}

+ 22 - 0
src/Hotline/Settings/UserFastMenu.cs

@@ -0,0 +1,22 @@
+using SqlSugar;
+using XF.Domain.Entities;
+
+namespace Hotline.Settings
+{
+    /// <summary>
+    /// 用户快捷入口设置
+    /// </summary>
+    public class UserFastMenu:CreationEntity
+    {
+        /// <summary>
+        /// 用户ID
+        /// </summary>
+        public string UserId { get; set; }
+
+        /// <summary>
+        /// 快捷菜单集合
+        /// </summary>
+        [SugarColumn(ColumnDataType = "varchar(4000)", IsJson = true)]
+        public List<string> FastMenuArr { get; set; }
+    }
+}