diff --git a/Api/Ewide.Core/Ewide.Core.Common/Authorized/ApiAuthorized.cs b/Api/Ewide.Core/Ewide.Core.Common/Authorized/ApiAuthorized.cs new file mode 100644 index 0000000..781b87a --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Common/Authorized/ApiAuthorized.cs @@ -0,0 +1,64 @@ +using Dapper; +using DapperExtensions; +using Ewide.Core.Data; +using Ewide.Core.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Common +{ + public class ApiAuthorized + { + public static bool Authorized(string url, string userID = "") + { + using (var db = new DapperHelper()) + { + // 将 /api/controller/action 形式转换成 api::controller::action + url = url.ToLower().Substring(1).Replace("/", "::"); + var appliaction = db.Conn.GetList(new { Url = url }).FirstOrDefault(); + + // 默认没有配置的接口为开放接口 + if (appliaction == null) + { + return true; + } + + // 不需要验证权限和登录 + if (appliaction.AuthorityType == (int)AuthorityType.开放接口) + { + return true; + } + + // 只需要验证登录 + if (appliaction.AuthorityType == (int)AuthorityType.内部接口) + { + return !String.IsNullOrEmpty(userID); + } + + // 需要验证权限 + if (!String.IsNullOrEmpty(userID)) + { + var roles = db.Conn.GetList(new { UserID = userID }).Select(p => p.RoleID).ToList(); + var apps = db.Conn.ExecuteScalar(@"SELECT COUNT(0) FROM ec_user_role UR +INNER JOIN ec_role_menu RM ON UR.roleID = RM.roleID +INNER JOIN ec_menu M ON RM.menuID = M.id AND M.enabled = 1 +INNER JOIN ec_role_menu_appliaction RMA ON RM.ID = RMA.roleMenuID +INNER JOIN ec_menu_appliaction MA ON RMA.menuAppliactionID = MA.id AND MA.relationType = 2 +WHERE UR.userID = @UserID AND MA.relationID = @RelationID", new + { + UserID = userID, + RelationID = appliaction.ID + }); + + return apps > 0; + } + + } + + return false; + } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedHelper.cs b/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedHelper.cs index e5b242c..49c590e 100644 --- a/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedHelper.cs +++ b/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedHelper.cs @@ -14,11 +14,9 @@ namespace Ewide.Core.Common { public class AuthorizedHelper : AuthorizedWhiteListHelper { - private static readonly string JWT_Secret = System.Configuration.ConfigurationManager.ConnectionStrings["Token_JwtSecret"]?.ToString(); + private static readonly string JWT_Secret = System.Configuration.ConfigurationManager.AppSettings["Token_JwtSecret"]?.ToString(); - private static readonly string JWT_User = System.Configuration.ConfigurationManager.ConnectionStrings["Token_JwtUser"]?.ToString(); - - private static readonly string WhiteList_Key = System.Configuration.ConfigurationManager.ConnectionStrings["Token_WhiteList"]?.ToString(); + private static readonly string JWT_User = System.Configuration.ConfigurationManager.AppSettings["Token_JwtUser"]?.ToString(); /// /// 从Request中获取Token值。 @@ -69,6 +67,20 @@ namespace Ewide.Core.Common } } + public static string[] CurrentUserRoles + { + get + { + var userID = CurrentUserID; + if (String.IsNullOrEmpty(userID)) + { + return null; + } + var roles = GetWhiteListUserRoles(userID); + return roles; + } + } + /// /// 将字符串转成JwtToken类型,并指示是否转换成功。 /// @@ -207,7 +219,9 @@ namespace Ewide.Core.Common { var str = password.Trim().ToLower(); - str = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(UTF8Encoding.Default.GetBytes(str))).Replace("-", ""); + str = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(UTF8Encoding.Default.GetBytes(str))) + .Replace("-", "") + .ToLower(); return str; } diff --git a/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedWhiteListHelper.cs b/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedWhiteListHelper.cs index 6ed4c55..3261ae4 100644 --- a/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedWhiteListHelper.cs +++ b/Api/Ewide.Core/Ewide.Core.Common/Authorized/AuthorizedWhiteListHelper.cs @@ -13,7 +13,7 @@ namespace Ewide.Core.Common { public class AuthorizedWhiteListHelper { - private static readonly string WhiteList_Key = System.Configuration.ConfigurationManager.ConnectionStrings["Token_WhiteList"]?.ToString(); + protected static readonly string WhiteList_Key = System.Configuration.ConfigurationManager.AppSettings["Token_WhiteList"]?.ToString(); private static readonly string CachePath = HttpContext.Current.Server.MapPath("\\" + Path.Combine("Cache", WhiteList_Key + ".bin")); @@ -128,25 +128,29 @@ namespace Ewide.Core.Common using (var db = new DapperHelper()) { var user = db.Conn.Get(userID); + var roles = db.Conn.GetList(new { UserID = userID }).Select(p => p.RoleID).ToArray(); var exp = DateTime.Now.AddDays(30); if (list == null) { list = new List - { - new WhiteListToken { - UserID = userID, - User = user, - Token = token, - ExpDate = exp - } - }; + new WhiteListToken + { + UserID = userID, + User = user, + Roles = roles, + Token = token, + ExpDate = exp + } + }; } else { var item = list.FirstOrDefault(p => p.UserID.Equals(userID, StringComparison.CurrentCultureIgnoreCase)); if (item != null) { + item.User = user; + item.Roles = roles; item.Token = token; item.ExpDate = exp; } @@ -156,6 +160,7 @@ namespace Ewide.Core.Common { UserID = userID, User = user, + Roles = roles, Token = token, ExpDate = exp }); @@ -218,6 +223,22 @@ namespace Ewide.Core.Common WhiteList = list; } + /// + /// 根据UserID获取白名单中的角色编号 + /// + /// + /// + protected static string[] GetWhiteListUserRoles(string userID) + { + var item = GetWhiteListByUserID(userID); + if (item == null) + { + return null; + } + + return item.Roles; + } + /// /// /// diff --git a/Api/Ewide.Core/Ewide.Core.Common/Authorized/LoginHelper.cs b/Api/Ewide.Core/Ewide.Core.Common/Authorized/LoginHelper.cs new file mode 100644 index 0000000..d83fce9 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Common/Authorized/LoginHelper.cs @@ -0,0 +1,58 @@ +using Ewide.Core.Data; +using DapperExtensions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Ewide.Core.Model; +using Ewide.Core.DTO; + +namespace Ewide.Core.Common +{ + public class LoginHelper + { + public LoginRDTO Login(string account, string password, out string token, out string message) + { + token = String.Empty; + message = String.Empty; + using (var db = new DapperHelper()) + { + var user = db.Conn.GetList(new { Account = account }).FirstOrDefault(); + + if (user == null) + { + message = "此用户不存在"; + return null; + } + + var passwordMD5 = AuthorizedHelper.GetPasswordMD5(password); + if (!passwordMD5.Equals(user.Password)) + { + message = "密码错误"; + return null; + } + + if (!user.Enabled) + { + message = "当前用户已被禁止登录"; + return null; + } + + message = "登录成功"; + token = AuthorizedHelper.AddToken(user); + return new LoginRDTO + { + Account = user.Account, + Name = user.Name, + Type = user.Type, + Sex = user.Sex, + Avatar = user.Avatar, + Code = user.Code, + Phone = user.Phone, + Desc = user.Desc + }; + } + } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Common/Authorized/WhiteListToken.cs b/Api/Ewide.Core/Ewide.Core.Common/Authorized/WhiteListToken.cs index 2b34a6b..761a2c5 100644 --- a/Api/Ewide.Core/Ewide.Core.Common/Authorized/WhiteListToken.cs +++ b/Api/Ewide.Core/Ewide.Core.Common/Authorized/WhiteListToken.cs @@ -11,6 +11,7 @@ namespace Ewide.Core.Common { public string UserID { get; set; } public EC_User User { get; set; } + public string[] Roles { get; set; } public string Token { get; set; } public DateTime ExpDate { get; set; } } diff --git a/Api/Ewide.Core/Ewide.Core.Common/EnumCode/AuthorityType.cs b/Api/Ewide.Core/Ewide.Core.Common/EnumCode/AuthorityType.cs new file mode 100644 index 0000000..3f77f3e --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Common/EnumCode/AuthorityType.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Common +{ + public enum AuthorityType + { + /// + /// 一般接口,需要验证权限 + /// + 一般接口 = 1, + + /// + /// 内部接口,只需要验证登录 + /// + 内部接口 = 2, + + /// + /// 开放接口,不需要验证权限和登录 + /// + 开放接口 = 3, + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Common/Ewide.Core.Common.csproj b/Api/Ewide.Core/Ewide.Core.Common/Ewide.Core.Common.csproj index bb47bea..7cc052f 100644 --- a/Api/Ewide.Core/Ewide.Core.Common/Ewide.Core.Common.csproj +++ b/Api/Ewide.Core/Ewide.Core.Common/Ewide.Core.Common.csproj @@ -59,15 +59,18 @@ + + + + + - - - + @@ -77,6 +80,10 @@ {b5b46bad-81e3-4df0-83ef-75148236f7ce} Ewide.Core.Data + + {590704FF-28C5-4536-B587-AC213858CC42} + Ewide.Core.DTO + {31C3CA3D-14A1-453A-866D-76D4C74A9BDC} Ewide.Core.Model diff --git a/Api/Ewide.Core/Ewide.Core.Common/Menu/MenuHelper.cs b/Api/Ewide.Core/Ewide.Core.Common/Menu/MenuHelper.cs new file mode 100644 index 0000000..d0d52f4 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Common/Menu/MenuHelper.cs @@ -0,0 +1,56 @@ +using DapperExtensions; +using Ewide.Core.Data; +using Ewide.Core.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Common +{ + public class MenuHelper + { + public List GetMenu() + { + using (var db = new DapperHelper()) + { + var roles = AuthorizedHelper.CurrentUserRoles; + var menuIDList = db.Conn.GetList(new { RoleID = roles }).Select(p => p.MenuID).ToList(); + var menuList = db.Conn.GetList(new { ID = menuIDList, Enabled = true }) + .Select(p => new MenuStructure + { + ID = p.ID, + ParentID = p.ParentID, + Type = p.Type, + Name = p.Name, + Icon = p.Icon, + Color = p.Color, + Sort = p.Sort, + Children = null + }) + .ToList(); + return GetMenuStructure(menuList); + } + } + + private List GetMenuStructure(List list, string parentID = "") + { + List menu; + if (String.IsNullOrEmpty(parentID)) + { + menu = list.Where(p => String.IsNullOrEmpty(p.ParentID)).OrderBy(p => p.Sort).ToList(); + } + else + { + menu = list.Where(p => parentID == p.ParentID).OrderBy(p => p.Sort).ToList(); + } + + foreach (var m in menu) + { + m.Children = GetMenuStructure(list, m.ID); + } + return menu; + } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Common/Menu/MenuStructure.cs b/Api/Ewide.Core/Ewide.Core.Common/Menu/MenuStructure.cs new file mode 100644 index 0000000..1653763 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Common/Menu/MenuStructure.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Common +{ + public class MenuStructure + { + public string ID { get; set; } + public string ParentID { get; set; } + public int Type { get; set; } + public string Name { get; set; } + public string Component { get; set; } + public string Icon { get; set; } + public string Color { get; set; } + public int Sort { get; set; } + public List Children { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.DTO/CreateUserDTO.cs b/Api/Ewide.Core/Ewide.Core.DTO/CreateUserDTO.cs new file mode 100644 index 0000000..bb4cf92 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.DTO/CreateUserDTO.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.DTO +{ + public class CreateUserDTO + { + [DisplayName("登录帐号")] + [Required(AllowEmptyStrings = false, ErrorMessage = "登录帐号不可为空")] + public string Account { get; set; } + + [DisplayName("登录密码")] + [Required(AllowEmptyStrings = false, ErrorMessage = "登录密码不可为空")] + public string Password { get; set; } + + [DisplayName("用户名")] + [Required(AllowEmptyStrings = false, ErrorMessage = "用户名不可为空")] + public string Name { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.DTO/Ewide.Core.DTO.csproj b/Api/Ewide.Core/Ewide.Core.DTO/Ewide.Core.DTO.csproj index 7d6d427..31fd58d 100644 --- a/Api/Ewide.Core/Ewide.Core.DTO/Ewide.Core.DTO.csproj +++ b/Api/Ewide.Core/Ewide.Core.DTO/Ewide.Core.DTO.csproj @@ -42,7 +42,9 @@ + + diff --git a/Api/Ewide.Core/Ewide.Core.DTO/LoginDTO.cs b/Api/Ewide.Core/Ewide.Core.DTO/LoginDTO.cs index c5ee089..4beafa5 100644 --- a/Api/Ewide.Core/Ewide.Core.DTO/LoginDTO.cs +++ b/Api/Ewide.Core/Ewide.Core.DTO/LoginDTO.cs @@ -11,7 +11,6 @@ namespace Ewide.Core.DTO public class LoginDTO { [DisplayName("登录帐号")] - [Phone(ErrorMessage = "a a")] [Required(AllowEmptyStrings = false, ErrorMessage = "登录帐号不可为空")] public string Account { get; set; } diff --git a/Api/Ewide.Core/Ewide.Core.DTO/LoginRDTO.cs b/Api/Ewide.Core/Ewide.Core.DTO/LoginRDTO.cs new file mode 100644 index 0000000..aa1202f --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.DTO/LoginRDTO.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.DTO +{ + public class LoginRDTO + { + public string Account { get; set; } + public string Name { get; set; } + public int Type { get; set; } + public int Sex { get; set; } + public string Avatar { get; set; } + public string Code { get; set; } + public string Phone { get; set; } + public string Desc { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Appliaction.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Appliaction.cs new file mode 100644 index 0000000..b4e6d33 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Appliaction.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Appliaction + { + public string ID { get; set; } + public string Name { get; set; } + public string Url { get; set; } + public int AuthorityType { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Dictionary.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Dictionary.cs new file mode 100644 index 0000000..bacc5a6 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Dictionary.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Dictionary + { + public string ID { get; set; } + public string ParentID { get; set; } + public string Name { get; set; } + public string Code { get; set; } + public string Value { get; set; } + public string Values { get; set; } + public string Desc { get; set; } + public bool IsSystem { get; set; } + public int Sort { get; set; } + public DateTime CreateTime { get; set; } + public string CreateUserID { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Function.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Function.cs new file mode 100644 index 0000000..8921613 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Function.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Function + { + public string ID { get; set; } + public string MenuID { get; set; } + public string AppliactionID { get; set; } + public string Name { get; set; } + public string Text { get; set; } + public string Icon { get; set; } + public string Theme { get; set; } + public DateTime CreateTime { get; set; } + public string CreateUserID { get; set; } + + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Menu.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Menu.cs new file mode 100644 index 0000000..fc11de6 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Menu.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Menu + { + public string ID { get; set; } + public string ParentID { get; set; } + public int Type { get; set; } + public string Name { get; set; } + public string Component { get; set; } + public string Icon { get; set; } + public string Color { get; set; } + public bool IsSystem { get; set; } + public bool Enabled { get; set; } + public int Sort { get; set; } + public DateTime CreateTime { get; set; } + public string CreateUserID { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Menu_Appliaction.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Menu_Appliaction.cs new file mode 100644 index 0000000..1cd13e4 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Menu_Appliaction.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Menu_Appliaction + { + public string ID { get; set; } + public string MenuID { get; set; } + public int RelationType { get; set; } + public string RelationID { get; set; } + + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Role.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Role.cs new file mode 100644 index 0000000..21f42a7 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Role.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Role + { + public string ID { get; set; } + public string Name { get; set; } + public string Desc { get; set; } + public DateTime CreateTime { get; set; } + public string CreateUserID { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Role_Menu.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Role_Menu.cs new file mode 100644 index 0000000..4a61f75 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Role_Menu.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Role_Menu + { + public string ID { get; set; } + public string MenuID { get; set; } + public string ParentMenuID { get; set; } + public string RoleID { get; set; } + public DateTime CreateTime { get; set; } + public string CreateUserID { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_Role_Menu_Appliaction.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_Role_Menu_Appliaction.cs new file mode 100644 index 0000000..c0f3a01 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_Role_Menu_Appliaction.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_Role_Menu_Appliaction + { + public string ID { get; set; } + public string RoleMenuID { get; set; } + public string MenuAppliactionID { get; set; } + + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_System.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_System.cs new file mode 100644 index 0000000..28b680f --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_System.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_System + { + public string ID { get; set; } + public int Status { get; set; } + public DateTime CreateTime { get; set; } + public string AdminID { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_User.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_User.cs index ecc5884..834b44c 100644 --- a/Api/Ewide.Core/Ewide.Core.Model/EC_User.cs +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_User.cs @@ -6,9 +6,21 @@ using System.Threading.Tasks; namespace Ewide.Core.Model { + [Serializable] public class EC_User { public string ID { get; set; } public string Account { get; set; } + public string Password { get; set; } + public string Name { get; set; } + public int Type { get; set; } + public int Sex { get; set; } + public string Avatar { get; set; } + public string Code { get; set; } + public string Phone { get; set; } + public string Desc { get; set; } + public bool Enabled { get; set; } + public DateTime CreateTime { get; set; } + public string CreateUserID { get; set; } } } diff --git a/Api/Ewide.Core/Ewide.Core.Model/EC_User_Role.cs b/Api/Ewide.Core/Ewide.Core.Model/EC_User_Role.cs new file mode 100644 index 0000000..a2648e0 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Model/EC_User_Role.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Model +{ + [Serializable] + public class EC_User_Role + { + public string ID { get; set; } + public string UserID { get; set; } + public string RoleID { get; set; } + public DateTime CreateTime { get; set; } + public string CreateUserID { get; set; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Model/Ewide.Core.Model.csproj b/Api/Ewide.Core/Ewide.Core.Model/Ewide.Core.Model.csproj index 817b345..88643eb 100644 --- a/Api/Ewide.Core/Ewide.Core.Model/Ewide.Core.Model.csproj +++ b/Api/Ewide.Core/Ewide.Core.Model/Ewide.Core.Model.csproj @@ -54,7 +54,17 @@ + + + + + + + + + + diff --git a/Api/Ewide.Core/Ewide.Core.Service/Base/BaseService.cs b/Api/Ewide.Core/Ewide.Core.Service/Base/BaseService.cs new file mode 100644 index 0000000..6696a89 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Service/Base/BaseService.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Service +{ + public class BaseService + { + /// + /// 设置和读取异常信息。 + /// + protected string _ErrorMessage = String.Empty; + + /// + /// 读取异常信息。 + /// + public string ErrorMessage + { + get + { + return _ErrorMessage; + } + } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Service/Base/GateService.cs b/Api/Ewide.Core/Ewide.Core.Service/Base/GateService.cs new file mode 100644 index 0000000..a481c8d --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Service/Base/GateService.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DapperExtensions; +using Ewide.Core.Model; +using Ewide.Core.Common; + +namespace Ewide.Core.Service +{ + public class GateService : BaseService + { + public void Login(string account, string password) + { + + } + + public string CreateAccount(string account, string password, string name) + { + using (var db = new Data.DapperTransactionHelper()) + { + var user = new EC_User + { + ID = Guid.NewGuid().ToString(), + Account = account, + Password = AuthorizedHelper.GetPasswordMD5(password), + Name = name, + Type = 1, + Sex = 1, + Enabled = true, + CreateTime = DateTime.Now + }; + db.Conn.Insert(user); + + try + { + db.Complete(); + return user.ID; + } + catch (Exception ex) + { + _ErrorMessage = ex.Message; + db.RollBack(); + return null; + } + } + } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.Service/Ewide.Core.Service.csproj b/Api/Ewide.Core/Ewide.Core.Service/Ewide.Core.Service.csproj index 874d932..0d190e0 100644 --- a/Api/Ewide.Core/Ewide.Core.Service/Ewide.Core.Service.csproj +++ b/Api/Ewide.Core/Ewide.Core.Service/Ewide.Core.Service.csproj @@ -42,6 +42,8 @@ + + diff --git a/Api/Ewide.Core/Ewide.Core.Web/Web.Debug.config b/Api/Ewide.Core/Ewide.Core.Web/Web.Debug.config new file mode 100644 index 0000000..96d0fe1 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Web/Web.Debug.config @@ -0,0 +1,32 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Api/Ewide.Core/Ewide.Core.Web/Web.config b/Api/Ewide.Core/Ewide.Core.Web/Web.config new file mode 100644 index 0000000..d396e57 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.Web/Web.config @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/App_Start/WebApiConfig.cs b/Api/Ewide.Core/Ewide.Core.WebApi/App_Start/WebApiConfig.cs index a71cf13..b6e1e7b 100644 --- a/Api/Ewide.Core/Ewide.Core.WebApi/App_Start/WebApiConfig.cs +++ b/Api/Ewide.Core/Ewide.Core.WebApi/App_Start/WebApiConfig.cs @@ -32,8 +32,16 @@ namespace Ewide.Core.WebApi config.Services.Replace(typeof(IHttpActionInvoker), new HttpWebApiControllerActionInvoker(config)); #endregion + // 接口权限 config.Filters.Add(new ApiAuthorizeAttribute()); + + // 接口参数验证 config.Filters.Add(new ValidateArgumentsFilter()); + +#if DEBUG + // 允许跨域 + config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*")); +#endif } } } diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/BetestController.cs b/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/BetestController.cs deleted file mode 100644 index 301526d..0000000 --- a/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/BetestController.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Web.Http; - -namespace Ewide.Core.WebApi.Areas.Base.Controllers -{ - [ValidateArgumentsFilter] - public class BetestController : BaseController - { - /// - /// 获取所有接口及其注释 - /// - /// - [Route("test22")] - [HttpPost] - public IHttpActionResult Test1() - { - var apis = Configuration.Services.GetApiExplorer().ApiDescriptions; - - var result = apis - .Select(p => new - { - p.RelativePath, - p.Documentation - }); - return DisplayJSON(result); - } - - [HttpPost] - public IHttpActionResult Test2() - { - return DisplayJSON(""); - } - } -} diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/GateController.cs b/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/GateController.cs index bd1cb05..108450f 100644 --- a/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/GateController.cs +++ b/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/GateController.cs @@ -1,4 +1,6 @@ -using Ewide.Core.DTO; +using Ewide.Core.Common; +using Ewide.Core.DTO; +using Ewide.Core.Service; using System; using System.Collections.Generic; using System.Linq; @@ -15,11 +17,24 @@ namespace Ewide.Core.WebApi.Areas.Base.Controllers /// /// /// - [ApiAuthorize(VerifyAuthorization = false)] + [AllowAnonymous] [HttpPost] public IHttpActionResult Login(LoginDTO dto) { - return DisplayJSON(dto); + var info = new LoginHelper().Login(dto.Account, dto.Password, out string token, out string message); + if(info == null) + { + return DisplayErrorJSON(message); + } + else + { + return DisplaySuccessJSON(new + { + Token = token, + Message = message, + Info = info + }); + } } [ValidateArgumentsFilter(AllowNull = true)] @@ -28,5 +43,18 @@ namespace Ewide.Core.WebApi.Areas.Base.Controllers { return DisplayJSON(dto); } + + /// + /// 测试创建帐号 + /// + /// + /// + [HttpPost] + public IHttpActionResult CreateAccount(CreateUserDTO dto) + { + var service = new GateService(); + var result = service.CreateAccount(dto.Account, dto.Password, dto.Name); + return DisplayJSON(String.IsNullOrEmpty(service.ErrorMessage) ? result : service.ErrorMessage); + } } } diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/MenuController.cs b/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/MenuController.cs new file mode 100644 index 0000000..06892a0 --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/MenuController.cs @@ -0,0 +1,19 @@ +using Ewide.Core.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; + +namespace Ewide.Core.WebApi.Areas.Base.Controllers +{ + public class MenuController : BaseController + { + [HttpPost] + public IHttpActionResult Get() + { + return DisplaySuccessJSON(new MenuHelper().GetMenu()); + } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/SpareController.cs b/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/SpareController.cs new file mode 100644 index 0000000..7a88c4c --- /dev/null +++ b/Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Controllers/SpareController.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; + +namespace Ewide.Core.WebApi.Areas.Base.Controllers +{ + public class SpareController : BaseController + { + /// + /// 获取所有接口及其注释 + /// + /// + [HttpPost] + public IHttpActionResult GetApis() + { + var apis = Configuration.Services.GetApiExplorer().ApiDescriptions; + + var result = apis + .ToLookup(p => p.ActionDescriptor.ControllerDescriptor) + .Select(p => new + { + Group = p.Key.ControllerName.ToLower(), + Apis = p.Select(q => new + { + RelativePath = "/" + q.RelativePath.ToLower(), + q.Documentation + }) + }); + + return DisplayJSON(result); + } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/ApiAuthorizeAttribute.cs b/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/ApiAuthorizeAttribute.cs index 08ac24f..718abd7 100644 --- a/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/ApiAuthorizeAttribute.cs +++ b/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/ApiAuthorizeAttribute.cs @@ -13,26 +13,16 @@ namespace Ewide.Core.WebApi [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] public class ApiAuthorizeAttribute : AuthorizeAttribute { - /// - /// 是否验证权限 - /// - public bool VerifyAuthorization { get; set; } = true; - public override void OnAuthorization(HttpActionContext actionContext) { - if (!VerifyAuthorization) - { - base.IsAuthorized(actionContext); - return; - } - + var path = actionContext.Request.RequestUri.AbsolutePath; // 验证token var authorization = actionContext.Request.Headers.Authorization; if (authorization != null && !String.IsNullOrEmpty(authorization.Parameter)) { var token = authorization.Parameter; var userID = AuthorizedHelper.GetUserID(token); - if (!String.IsNullOrEmpty(userID)) + if (ApiAuthorized.Authorized(path, userID)) { base.IsAuthorized(actionContext); return; @@ -42,9 +32,10 @@ namespace Ewide.Core.WebApi { var attributes = actionContext.ActionDescriptor.GetCustomAttributes().OfType(); bool isAnonymous = attributes.Any(a => a is AllowAnonymousAttribute); - if (isAnonymous) + if (isAnonymous || ApiAuthorized.Authorized(path)) { - base.OnAuthorization(actionContext); + base.IsAuthorized(actionContext); + return; } else { diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/BaseController.cs b/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/BaseController.cs index 99cdbef..480293c 100644 --- a/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/BaseController.cs +++ b/Api/Ewide.Core/Ewide.Core.WebApi/Controllers/Code/BaseController.cs @@ -58,13 +58,49 @@ namespace Ewide.Core.WebApi return _DisplayJSON(BaseDisplayJSON.Ok(message)); } + protected IHttpActionResult DisplaySuccessJSON(object result) + { + return DisplayJSON(new + { + Success = true, + Data = result + }); + } + + protected IHttpActionResult DisplaySuccessJSON(string message) + { + return DisplayJSON(new + { + Success = true, + Message = message + }); + } + + protected IHttpActionResult DisplayErrorJSON(object result) + { + return DisplayJSON(new + { + Success = false, + Data = result + }); + } + + protected IHttpActionResult DisplayErrorJSON(string message) + { + return DisplayJSON(new + { + Success = false, + Message = message + }); + } + protected IHttpActionResult DisplayDataJSON(object data, int total) { - return _DisplayJSON(BaseDisplayJSON.Ok(new + return DisplayJSON(new { Data = data, Total = total - })); + }); } } } \ No newline at end of file diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Ewide.Core.WebApi.csproj b/Api/Ewide.Core/Ewide.Core.WebApi/Ewide.Core.WebApi.csproj index 8815fe3..45aa52a 100644 --- a/Api/Ewide.Core/Ewide.Core.WebApi/Ewide.Core.WebApi.csproj +++ b/Api/Ewide.Core/Ewide.Core.WebApi/Ewide.Core.WebApi.csproj @@ -52,6 +52,12 @@ + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll + + + ..\packages\Microsoft.AspNet.Cors.5.2.4\lib\net45\System.Web.Cors.dll + @@ -59,6 +65,12 @@ + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.4\lib\net45\System.Web.Http.dll + + + ..\packages\Microsoft.AspNet.WebApi.Cors.5.2.4\lib\net45\System.Web.Http.Cors.dll + @@ -72,18 +84,12 @@ - - ..\packages\Microsoft.AspNet.WebApi.Client.5.2.4\lib\net45\System.Net.Http.Formatting.dll - True ..\packages\Microsoft.AspNet.WebPages.3.2.4\lib\net45\System.Web.Helpers.dll - - ..\packages\Microsoft.AspNet.WebApi.Core.5.2.4\lib\net45\System.Web.Http.dll - ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.4\lib\net45\System.Web.Http.WebHost.dll @@ -134,7 +140,8 @@ - + + @@ -206,7 +213,9 @@ - + + Designer + Web.config diff --git a/Api/Ewide.Core/Ewide.Core.WebApi/Web.config b/Api/Ewide.Core/Ewide.Core.WebApi/Web.config index a0fc5f7..66c101c 100644 --- a/Api/Ewide.Core/Ewide.Core.WebApi/Web.config +++ b/Api/Ewide.Core/Ewide.Core.WebApi/Web.config @@ -5,13 +5,17 @@ --> - + + + + +