| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using WeApp.BaseInfo;
- using WeApp.Configuration;
- using WeApp.EF;
- namespace WeApp.SeedData
- {
- public class DefaultFunctionsCreator
- {
- private readonly WeAppDbContext _context;
- public DefaultFunctionsCreator(WeAppDbContext context)
- {
- _context = context;
- LangStr = "\r\n\r\n";
- PermissionStr = "";
- LangList = new List<string>();
- }
- public void Create()
- {
- _context.Database.ExecuteSqlCommand("TRUNCATE TABLE [dbo].[Sys_Functions]");
- CreateFunctions("HTSystem", "后台管理系统", "0", FunctionTypeDefinition.Catalog, "Index", "Home", "", "fas fa-home", 0, "", "", false, "Pages", 0);
- Camp();
- Basic();
- System();
- Write();
- }
- #region Camp
- public void Camp()
- {
- CreateCatalog("TrainMg", "HTSystem", "演练培训实施", 10, "fa fa-cogs");
- CreateMenu("CampMg", "TrainMg", "演练培训营管理", 1, "Camp", "Train");
- CreateBtn_Curd("CampMg", "工程", "Camp");
- CreateBtn_Other("Group", "CampMg", "分组管理", 4, "iconfont icon-user-group", "btnGroup", "Camp/Group");
- //CreateBtn_Other("StuHelp", "CampMg", "提示配置", 5, "iconfont icon-help", "btnHelp", "Camp/StudentHelp");
- CreateBtn_Other("Attach", "CampMg", "附件配置/电话", 6, "iconfont icon-paperclip", "btnAttach", "Camp/AttachFile");
- CreateBtn_NotShow("Question", "CampMg", "配置电话提问", 6, "iconfont icon-news", "", "");
- CreateBtn_Other("Tag", "CampMg", "标签配置", 7, "iconfont icon-tag", "btnTag", "Camp/BehaviorTag");
- CreateBtn_Other("Role", "CampMg", "角色预案", 8, "iconfont icon-process", "btnRole", "Camp/RoleGroup");
- CreateBtn_NotShow("RunMg", "CampMg", "运行管理", 9, "iconfont icon-training", "", "");
- CreateBtn_NotShow("DetailMg", "CampMg", "运行详情", 10, "iconfont icon-news", "", "");
- CreateMenu_NotShow("GroupMg", "TrainMg", "演练培训营分组管理", 1, "Group", "Train");
- CreateBtn_Curd("GroupMg", "分组", "CampGroup");
- CreateMenu("EvalMg", "TrainMg", "演练培训营演练实施", 2, "Eval", "Train");
- CreateMenu("PortraitMg", "TrainMg", "演练培训营画像", 3, "Portrait", "Train");
- CreateBtn_Query("PortraitMg", 0);
- CreateBtn_Other("Generate", "PortraitMg", "生成画像", 1);
- }
- #endregion Camp
- #region Basic
- public void
- Basic()
- {
- CreateCatalog("BasicMg", "HTSystem", "基础信息管理", 20, "fa fa-cogs");
- CreateMenu("TrainingRoleMg", "BasicMg", "演练角色管理", 1, "Role", "Basic");
- CreateBtn_Curd("TrainingRoleMg", "角色", "TrainingRole");
- CreateMenu("GroupRoleMg", "BasicMg", "演练角色组预案管理", 2, "GroupRole", "Basic");
- CreateBtn_Curd("GroupRoleMg", "预案", "TrainingRoleGroup");
- CreateMenu("PortraitRemarkMg", "BasicMg", "画像预置信息管理", 3, "PortraitRemark", "Basic");
- CreateBtn_Curd("PortraitRemarkMg", "信息", "PortraitRemark");
- CreateMenu("StudentHelpMg", "BasicMg", "学员提示信息管理", 4, "StudentHelp", "Basic");
- CreateBtn_Curd("StudentHelpMg", "提示", "StudentHelp");
- CreateMenu("PhoneQuestionMg", "BasicMg", "电话提问信息管理", 5, "PhoneQuestion", "Basic");
- CreateBtn_Curd("PhoneQuestionMg", "提问", "PhoneQuestion");
- }
- #endregion Basic
- #region SYSTEM
- private void System()
- {
- CreateCatalog("SystemMg", "HTSystem", "系统信息维护", 100, "fa fa-cogs");
- CreateMenu("TenantMg", "SystemMg", "租户信息管理", 0, "SysTenants", "BaseSystem", "host");
- CreateBtn_Curd("TenantMg", "租户", "Tenants");
- CreateMenu("RoleMg", "SystemMg", "角色信息管理", 1, "SysRoles", "BaseSystem", "", "fa fa-users");
- CreateBtn_Curd("RoleMg", "角色", "Roles");
- CreateBtn_Other("Auth", "RoleMg", "角色授权", 4, "fa fa-cog", null, "Roles/Auth");
- CreateMenu("UserMg", "SystemMg", "用户信息管理", 2, "SysUsers", "BaseSystem", "", "fa fa-user");
- CreateBtn_Curd("UserMg", "用户", "Users");
- CreateBtn_Other("Auth", "UserMg", "用户授权", 4, "fa fa-cog", null, "Users/Auth");
- CreateBtn_Other("ResetPassword", "UserMg", "重置密码", 5, "fa fa-lock", "btnResetPwd", "Users/ResetPassword");
- CreateBtn_Other("ResetLock", "UserMg", "解除锁定", 5, "fa fa-unlock", "btnResetLock", "Users/ResetLock");
- CreateMenu("StateMg", "SystemMg", "系统字典信息管理", 3, "SysStates", "BaseSystem", "", "fa fa-book");
- CreateBtn_Curd("StateMg", "字典", "States");
- CreateMenu("SettingMg", "SystemMg", "系统配置信息管理", 4, "SysSettings", "BaseSystem", "", "fa fa-cog");
- //CreateBtn_Curd("SettingMg", "配置", "Settings");
- CreateBtn_Query("SettingMg", 0);
- CreateBtn_Update("配置", "SettingMg", 2, "Settings");
- CreateMenu("HelpMg", "SystemMg", "系统帮助信息管理", 5, "SysHelps", "BaseSystem", "", "fa fa-question");
- CreateBtn_Curd("HelpMg", "帮助", "Helps");
- CreateMenu("FunctionMg", "SystemMg", "功能菜单管理", 6, "SysFunctions", "BaseSystem", "", "fa fa-server");
- CreateBtn_Curd("FunctionMg", "菜单", "Functions");
- CreateBtn_Other("MoveUp", "FunctionMg", "上移菜单", 4, "fa fa-arrow-up", "btnMoveUp", "Functions/MoveUp");
- CreateBtn_Other("MoveDown", "FunctionMg", "下移菜单", 5, "fa fa-arrow-down", "btnMoveDown", "Functions/MoveDown");
- CreateBtn_Other("Refresh", "FunctionMg", "刷新菜单", 6, "fa fa-retweet", "btnRefresh", "Functions/Refresh");
- CreateMenu("LogMg", "SystemMg", "系统日志管理", 7, "SysLogs", "BaseSystem", "", "fa fa-list-alt");
- CreateBtn_Query("LogMg", 0);
- CreateMenu("UserHelpInfo", "HTSystem", "系统帮助信息", 101, "Index", "Help", "", "fa fa-question");
- CreateMenu("UserSysSetting", "HTSystem", "用户配置", 102, "", "", icon: "fa fa-cogs", isShow: false);
- CreateBtn_Other("LoginImage", "UserSysSetting", "配置登陆页背景", 1, "fa fa-cog", "_Admin_btnLoginImage", "Settings/LoginImage", false);
- CreateBtn_Other("HomeImage", "UserSysSetting", "配置主页背景", 2, "fa fa-cog", "_Admin_btnHomeImage", "Settings/HomeImage", false);
- CreateBtn_Other("RefreshCache", "UserSysSetting", "刷新缓存", 3, "fa fa-recycle", "_Admin_btnCache", "Settings/CacheRefresh", false);
- CreateBtn_Other("RefreshLang", "UserSysSetting", "刷新语言包", 4, "fa fa-recycle", "_Admin_btnLang", "Settings/LangRefresh", false);
- }
- #endregion SYSTEM
- #region CREATE
- /// <summary>
- ///创建增删改查按钮
- /// </summary>
- /// <param name="funName"></param>
- /// <param name="parentNo"></param>
- /// <param name="url">{IwbConsts.ApiAppUrl}{url}/Create 如果url与parentNo不同需指定</param>
- private void CreateBtn_Curd(string parentNo, string funName, string url = null)
- {
- CreateBtn_Query(parentNo, 0);
- CreateBtn_Create(funName, parentNo, 1, url);
- CreateBtn_Update(funName, parentNo, 2, url);
- CreateBtn_Delete(funName, parentNo, 3, url);
- }
- /// <summary>
- ///创建不是增删改查的按钮
- /// </summary>
- private void CreateBtn_Other(string funNo, string parentNo, string funName, int sort, string icon = "far fa-plus-square", string script = null, string url = null, bool isShow = true, bool isApiUrl = true)
- {
- script = script ?? $"btn{funNo}";
- url = url ?? $"{parentNo}/{funNo}";
- url = isApiUrl ? $"{IwbConsts.ApiAppUrl}{url}" : url;
- if (isShow)
- {
- CreateBtn(funNo, parentNo, funName, sort, url, icon, script);
- }
- else
- {
- CreateBtn_NotShow(funNo, parentNo, funName, sort, icon, script, url);
- }
- }
- /// <summary>
- /// 创建目录
- /// </summary>
- private void CreateCatalog(string funNo, string parentNo, string funName, int sort,
- string icon = "fa fa-indent", string s = "", string c = "", bool auth = true, bool isShow = true)
- {
- if (isShow)
- {
- CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Catalog, "", "", "", icon, sort, c, s, auth);
- }
- else
- {
- CreateCatalog_NotShow(funNo, parentNo, funName, sort, icon, c, s, auth);
- }
- }
- /// <summary>
- /// 创建菜单
- /// </summary>
- private void CreateMenu(string funNo, string parentNo, string funName, int sort, string action,
- string controller, string s = "", string icon = "fa fa-bullseye", string c = "", string url = null,
- bool auth = true, bool isShow = true)
- {
- url = url ?? $"{controller}/{action}";
- if (isShow)
- {
- CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Menu, action, controller, url, icon, sort,
- c, s, auth);
- }
- else
- {
- CreateMenu_NotShow(funNo, parentNo, funName, sort, action, controller, s: s, icon: icon, c: c, url: url, auth: auth);
- }
- }
- /// <summary>
- /// 创建查询按钮
- /// </summary>
- private void CreateBtn_Query(string parentNo, int sort)
- {
- CreateBtn_NotShow("Query", parentNo, "查看页面", sort, "fa fa-eye", "btnQuery", "");
- }
- /// <summary>
- /// 创建添加按钮
- /// </summary>
- private void CreateBtn_Create(string funName, string parentNo, int sort, string url = null)
- {
- url = url ?? parentNo;
- CreateBtn("Create", parentNo, $"添加{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Create", "far fa-plus-square", "_btnCreate");
- }
- /// <summary>
- /// 创建修改按钮
- /// </summary>
- private void CreateBtn_Update(string funName, string parentNo, int sort, string url = null)
- {
- url = url ?? parentNo;
- CreateBtn("Update", parentNo, $"修改{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Update", "far fa-edit", "btnUpdate");
- }
- /// <summary>
- /// 创建删除按钮
- /// </summary>
- private void CreateBtn_Delete(string funName, string parentNo, int sort, string url = null)
- {
- url = url ?? parentNo;
- CreateBtn("Delete", parentNo, $"删除{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Delete", "far fa-minus-square", "btnDelete");
- }
- /// <summary>
- /// 创建按钮
- /// </summary>
- private void CreateBtn(string funNo, string parentNo, string funName, int sort, string url, string icon,
- string s, string c = "btn btn-default", string action = "", string controller = "", bool auth = true)
- {
- CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Button, action, controller, url, icon, sort, c, s, auth);
- }
- /// <summary>
- /// 创建不显示的目录
- /// </summary>
- private void CreateCatalog_NotShow(string funNo, string parentNo, string funName, int sort, string icon,
- string c = "", string s = "", bool auth = true)
- {
- CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, "", "", "", icon, sort, c, s, auth);
- }
- /// <summary>
- /// 创建不显示的菜单
- /// </summary>
- private void CreateMenu_NotShow(string funNo, string parentNo, string funName, int sort,
- string action, string controller, string s = "", string icon = "fa fa-bullseye",
- string c = "", string url = "", bool auth = true)
- {
- CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, action, controller, url, icon, sort, c, s, auth);
- }
- /// <summary>
- /// 创建不显示的按钮
- /// </summary>
- private void CreateBtn_NotShow(string funNo, string parentNo, string funName, int sort, string icon, string s, string url, string c = "btn btn-default", string action = "", string controller = "", bool auth = true)
- {
- CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, action, controller, url, icon, sort, c, s, auth);
- }
- /// <summary>
- /// 创建功能菜单
- /// </summary>
- private void CreateFunctions(string funNo, string funName, string parentNo, int funType,
- string action, string controller, string url, string icon, int sort, string @class, string script, bool auth = true, string path = "", int? depth = null)
- {
- var fun = _context.SysFunctions.FirstOrDefault(e => e.FunctionNo == funNo && e.ParentNo == parentNo);
- if (fun == null)
- {
- string permissionName = path.Replace(",", ".");
- if (string.IsNullOrEmpty(path))
- {
- var parentFun = _context.SysFunctions.FirstOrDefault(f => f.FunctionNo == parentNo);
- if (parentFun != null)
- {
- //path = parentFun.FunctionPath + "," + funNo;
- permissionName = parentFun.PermissionName + "." + funNo;
- depth = depth ?? parentFun.Depth + 1;
- }
- else
- {
- throw new Exception($"未查询到父菜单【{funName}-No:{funNo}-ParentNo:{parentNo}】");
- }
- }
- url = url ?? controller + "/" + action;
- fun = new SysFunction
- {
- FunctionNo = funNo,
- ParentNo = parentNo,
- FunctionName = funName,
- PermissionName = permissionName,
- //FunctionPath = path,
- FunctionType = funType,
- Controller = controller,
- Action = action,
- Icon = icon,
- Sort = sort,
- Depth = depth ?? 0,
- Class = @class,
- Script = script,
- Url = url,
- NeedAuth = auth
- };
- _context.SysFunctions.Add(fun);
- _context.SaveChanges();
- }
- var permName = fun.PermissionName.Replace(".", "");
- PermissionStr += $" public const string {permName} = \"{fun.PermissionName}\";\r\n";
- if (!LangList.Contains(permName))
- {
- LangList.Add(permName);
- LangStr += $" <text name=\"{permName}\" value=\"{fun.FunctionName}\" />\r\n";
- }
- }
- #endregion CREATE
- #region WRITE
- private List<string> LangList { get; set; }
- private string LangStr { get; set; }
- private string PermissionStr { get; set; }
- private void Write()
- {
- WritePermission();
- WriteLang();
- }
- private void WritePermission()
- {
- if (string.IsNullOrEmpty(PermissionStr))
- {
- return;
- }
- try
- {
- string content = "namespace WeApp.Authorization\r\n";
- content += "{\r\n";
- content += " public static class PermissionNames\r\n";
- content += " {\r\n";
- content += PermissionStr;
- content += " }\r\n";
- content += "}\r\n";
- var filePath = Path.Combine(GetBasePath(), "WeApp.Core\\Authorization\\PermissionNames.cs");
- var fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite);//清空文件内容
- fs.Close();
- using (FileStream fr = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
- {
- fr.Seek(0, SeekOrigin.Begin);
- byte[] bytes = Encoding.UTF8.GetBytes(content);
- fr.Write(bytes, 0, bytes.Length);
- fr.Flush();
- }
- }
- catch (Exception e)
- {
- throw (new Exception("Permission:" + e.Message));
- }
- }
- private void WriteLang()
- {
- if (string.IsNullOrEmpty(LangStr))
- {
- return;
- }
- //LangStr = $"<!--FUNCTION NAME START-->\r\n{LangStr}\r\n<!--FUNCTION NAME End-->\r\n";
- LangStr += "\r\n";
- try
- {
- var filePath = Path.Combine(GetBasePath(), "WeApp.Web\\Localization\\SourceFiles\\Iwb-zh-Hans.xml");
- string content;
- using (FileStream fr = new FileStream(filePath, FileMode.Open, FileAccess.Read))
- {
- fr.Seek(0, SeekOrigin.Begin);
- byte[] bytes = new byte[fr.Length];
- fr.Read(bytes, 0, bytes.Length);
- content = Encoding.UTF8.GetString(bytes);
- //Regex regex = new Regex("<!--FUNCTION NAME START-->(.*)<!--FUNCTION NAME End-->");
- //string content2 = regex.Replace(content, LangStr);
- string first = "<!--FUNCTION AND MENU NAME START-->";
- string second = "<!--FUNCTION AND MENU NAME END-->";
- if (!string.IsNullOrEmpty(content) && content.Contains(first) && content.Contains(second))
- {
- var start = content.IndexOf(first, 0, StringComparison.Ordinal) + first.Length;
- var end = content.IndexOf(second, start, StringComparison.Ordinal);
- string temp = content.Substring(start, end - start);
- content = content.Replace(temp, LangStr);
- }
- else
- {
- throw new Exception("Lang:语言配置信息没有生成!");
- }
- }
- if (!string.IsNullOrEmpty(content))
- {
- var fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite);//清空文件内容
- fs.Close();
- using (FileStream fr = new FileStream(filePath, FileMode.Append, FileAccess.Write))
- {
- byte[] newBytes = Encoding.UTF8.GetBytes(content);
- fr.Seek(0, SeekOrigin.Begin);
- fr.Write(newBytes, 0, newBytes.Length);
- fr.Flush();
- }
- }
- }
- catch (Exception e)
- {
- throw (new Exception("Lang:" + e.Message));
- }
- }
- private string GetBasePath()
- {
- var basePath = AppDomain.CurrentDomain.BaseDirectory;
- string path = Path.Combine(basePath.Substring(0, basePath.LastIndexOf("\\SourceCode", StringComparison.Ordinal)), "SourceCode\\");
- return path;
- }
- #endregion WRITE
- }
- }
|