Ver código fonte

权限管理

ltwork 1 ano atrás
pai
commit
b5fd208a3d

+ 1 - 1
BlankApp1/BizService/IUserService.cs

@@ -9,6 +9,6 @@ namespace BizService
 {
     public  interface IUserService : IBaseService<User>
     {
-        public bool FindDataByUserAndPass(String userName, String password);
+        public int FindDataByUserAndPass(String userName, String password);
     }
 }

+ 3 - 3
BlankApp1/BizService/UserService.cs

@@ -10,17 +10,17 @@ namespace BizService
 {
     public class UserService : BaseService<User>, IUserService
     {
-        public bool FindDataByUserAndPass(string userName, string password)
+        public int FindDataByUserAndPass(string userName, string password)
         {
             var all = base.QueryList();
             var user = base.QueryList().FirstOrDefault(x => ((x.UserName.Trim() == userName.Trim()) && (x.Password.Trim() == password.Trim())));
             if (user != null)
             {
-                return true;
+                return user.Id;
             }
             else
             {
-                return false;
+                return -1;
             }
         }
     }

+ 1 - 1
BlankApp1/BlankApp1/App.config

@@ -2,7 +2,7 @@
 <configuration>
 	<appSettings>
 		<!--连接字符串 SQL Server-->
-		<add key="MySql" value="Data Source=localhost;Database=plc_point_db3;User Id='root';Password='521125';port=3306;charset=utf8mb4;"/>
+		<add key="MySql" value="Data Source=localhost;Database=plc_point_db5;User Id='root';Password='521125';port=3306;charset=utf8mb4;"/>
 	    <!--1表示欧姆龙PLC,2表示为三菱PLC-->
 		<add key="PLCType" value="2"/>
 		<add key="PLCIp" value="192.168.0.38"/>

+ 2 - 1
BlankApp1/BlankApp1/Common/Appsession.cs

@@ -9,8 +9,9 @@ namespace PLCTool.Common
 {
     public  class Appsession
     {
-        public static string UserName = "Admin";
+        public static string UserName = "Admin"; 
 
+        public static int RoleId = -1;
     }
   
 }

+ 12 - 3
BlankApp1/BlankApp1/ViewModels/LoginViewModel.cs

@@ -1,4 +1,5 @@
 using BizService;
+using PLCTool.Common;
 using Prism.Commands;
 using Prism.Mvvm;
 using Prism.Services.Dialogs;
@@ -14,9 +15,11 @@ namespace PLCTool.ViewModels
     public class LoginViewModel : BindableBase, IDialogAware
     {
         private readonly IUserService _iUserService;
-        public LoginViewModel(IUserService iUserService)
+        private readonly IUserRoleService _iUserRoleService;
+        public LoginViewModel(IUserService iUserService,IUserRoleService iUserRoleService)
         {
             _iUserService = iUserService;
+            _iUserRoleService = iUserRoleService;
             ExecuteCommand = new DelegateCommand<string>(Execute);
         }
         public string Title { set; get; } = "登录";
@@ -60,9 +63,15 @@ namespace PLCTool.ViewModels
                 return;
             }
             //进度条设为可见
-
-            if (_iUserService.FindDataByUserAndPass(UserName, PassWord))
+            int userId = _iUserService.FindDataByUserAndPass(UserName, PassWord);
+            if (userId!=-1)
             {
+                //roleID赋值给全局变量
+                var userRole=_iUserRoleService.QueryList().FirstOrDefault(x=>x.UserId==userId);
+                if(userRole!=null)
+                {
+                    Appsession.RoleId = (int)userRole.RoleId;
+                }
                 RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
             }
             else

+ 16 - 3
BlankApp1/BlankApp1/ViewModels/TreeMenuViewModel.cs

@@ -1,6 +1,7 @@
 using BizService;
 using BlankApp1.Models;
 using Model.Entities;
+using PLCTool.Common;
 using Prism.Regions;
 using SqlSugar;
 using System;
@@ -19,18 +20,29 @@ namespace BlankApp1.ViewModels
         public List<MenuItemModel> Menus { get; set; } = new List<MenuItemModel>();
 
         // 列表,没有树型结构
-        private List<Model.Entities.Menu> origMenus = null;
+        private List<Model.Entities.Menu> origMenus = new List<Model.Entities.Menu>();
         private string basePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
 
         IRegionManager _regionManager;
-        public TreeMenuViewModel(IRegionManager regionManager, IMenuService menuService)
+        public TreeMenuViewModel(IRegionManager regionManager, IMenuService menuService,IRoleMenuService iRoleMenuService)
         {
             _regionManager = regionManager;
             // 需要获取菜单数据
             try
             {
-                origMenus = menuService.QueryList();
+                //查找这个角色的菜单
+                var allMenus = menuService.QueryList();
 
+                var roleMenus= iRoleMenuService.FindAllByRoleId(Appsession.RoleId);
+                foreach(var menu in roleMenus)
+                {
+                    var findMenu= allMenus.FirstOrDefault(x=>x.MenuId== menu.MenuId);
+                    if(findMenu != null)
+                    {
+                        origMenus.Add(findMenu);
+                    }
+                   
+                }
                 this.FillMenus(Menus, 0);
             }
              catch(Exception ex)
@@ -68,6 +80,7 @@ namespace BlankApp1.ViewModels
         private void FillMenus(List<MenuItemModel> menus, int parentId)
         {
             var sub = origMenus.Where(m => m.ParentId == parentId).OrderBy(o => o.Index);
+            //查找roleID对应的菜单
 
             if (sub.Count() > 0)
             {