DefaultFunctionsCreator.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using WeApp.BaseInfo;
  7. using WeApp.Configuration;
  8. using WeApp.EF;
  9. namespace WeApp.SeedData
  10. {
  11. public class DefaultFunctionsCreator
  12. {
  13. private readonly WeAppDbContext _context;
  14. public DefaultFunctionsCreator(WeAppDbContext context)
  15. {
  16. _context = context;
  17. LangStr = "\r\n\r\n";
  18. PermissionStr = "";
  19. LangList = new List<string>();
  20. }
  21. public void Create()
  22. {
  23. _context.Database.ExecuteSqlCommand("TRUNCATE TABLE [dbo].[Sys_Functions]");
  24. CreateFunctions("HTSystem", "后台管理系统", "0", FunctionTypeDefinition.Catalog, "Index", "Home", "", "fas fa-home", 0, "", "", false, "Pages", 0);
  25. Camp();
  26. Basic();
  27. System();
  28. Write();
  29. }
  30. #region Camp
  31. public void Camp()
  32. {
  33. CreateCatalog("TrainMg", "HTSystem", "演练培训实施", 10, "fa fa-cogs");
  34. CreateMenu("CampMg", "TrainMg", "演练培训营管理", 1, "Camp", "Train");
  35. CreateBtn_Curd("CampMg", "工程", "Camp");
  36. CreateBtn_Other("Group", "CampMg","分组管理",4, "iconfont icon-user-group", "btnGroup","Camp/Group");
  37. CreateBtn_Other("Attach", "CampMg", "附件配置", 5, "iconfont icon-paperclip", "btnAttach", "Camp/AttachFile");
  38. CreateBtn_Other("Tag", "CampMg", "标签配置", 6, "iconfont icon-tag", "btnTag", "Camp/BehaviorTag");
  39. CreateBtn_Other("Role", "CampMg", "角色预案", 7, "iconfont icon-process", "btnRole", "Camp/RoleGroup");
  40. CreateBtn_NotShow("RunMg", "CampMg", "运行管理",8, "iconfont icon-training", "","");
  41. CreateBtn_NotShow("DetailMg", "CampMg", "运行详情",9, "iconfont icon-news", "","");
  42. CreateMenu_NotShow("GroupMg", "TrainMg", "演练培训营分组管理", 1,"Group", "Train" );
  43. CreateBtn_Curd("GroupMg", "分组", "CampGroup");
  44. CreateMenu("EvalMg", "TrainMg", "演练培训营演练实施", 2, "Eval", "Train");
  45. CreateMenu("PortraitMg", "TrainMg", "演练培训营画像", 3, "Portrait", "Train");
  46. CreateBtn_Query("PortraitMg", 0);
  47. CreateBtn_Other("Generate", "PortraitMg","生成画像",1);
  48. }
  49. #endregion
  50. #region Basic
  51. public void Basic()
  52. {
  53. CreateCatalog("BasicMg", "HTSystem", "基础信息管理", 20, "fa fa-cogs");
  54. CreateMenu("TrainingRoleMg", "BasicMg", "演练角色管理", 1, "Role", "Basic");
  55. CreateBtn_Curd("TrainingRoleMg", "角色", "TrainingRole");
  56. CreateMenu("GroupRoleMg", "BasicMg", "演练角色组预案管理", 2, "GroupRole", "Basic");
  57. CreateBtn_Curd("GroupRoleMg", "预案", "TrainingRoleGroup");
  58. CreateMenu("PortraitRemarkMg", "BasicMg", "画像预置信息管理", 3, "PortraitRemark", "Basic");
  59. CreateBtn_Curd("PortraitRemarkMg", "信息", "PortraitRemark");
  60. }
  61. #endregion
  62. #region SYSTEM
  63. private void System()
  64. {
  65. CreateCatalog("SystemMg", "HTSystem", "系统信息维护", 100, "fa fa-cogs");
  66. CreateMenu("TenantMg", "SystemMg", "租户信息管理", 0, "SysTenants", "BaseSystem", "host");
  67. CreateBtn_Curd("TenantMg", "租户", "Tenants");
  68. CreateMenu("RoleMg", "SystemMg", "角色信息管理", 1, "SysRoles", "BaseSystem", "", "fa fa-users");
  69. CreateBtn_Curd("RoleMg", "角色", "Roles");
  70. CreateBtn_Other("Auth", "RoleMg", "角色授权", 4, "fa fa-cog", null, "Roles/Auth");
  71. CreateMenu("UserMg", "SystemMg", "用户信息管理", 2, "SysUsers", "BaseSystem", "", "fa fa-user");
  72. CreateBtn_Curd("UserMg", "用户", "Users");
  73. CreateBtn_Other("Auth", "UserMg", "用户授权", 4, "fa fa-cog", null, "Users/Auth");
  74. CreateBtn_Other("ResetPassword", "UserMg", "重置密码", 5, "fa fa-lock", "btnResetPwd", "Users/ResetPassword");
  75. CreateBtn_Other("ResetLock", "UserMg", "解除锁定", 5, "fa fa-unlock", "btnResetLock", "Users/ResetLock");
  76. CreateMenu("StateMg", "SystemMg", "系统字典信息管理", 3, "SysStates", "BaseSystem", "", "fa fa-book");
  77. CreateBtn_Curd("StateMg", "字典", "States");
  78. CreateMenu("SettingMg", "SystemMg", "系统配置信息管理", 4, "SysSettings", "BaseSystem", "", "fa fa-cog");
  79. //CreateBtn_Curd("SettingMg", "配置", "Settings");
  80. CreateBtn_Query("SettingMg", 0);
  81. CreateBtn_Update("配置", "SettingMg", 2, "Settings");
  82. CreateMenu("HelpMg", "SystemMg", "系统帮助信息管理", 5, "SysHelps", "BaseSystem", "", "fa fa-question");
  83. CreateBtn_Curd("HelpMg", "帮助", "Helps");
  84. CreateMenu("FunctionMg", "SystemMg", "功能菜单管理", 6, "SysFunctions", "BaseSystem", "", "fa fa-server");
  85. CreateBtn_Curd("FunctionMg", "菜单", "Functions");
  86. CreateBtn_Other("MoveUp", "FunctionMg", "上移菜单", 4, "fa fa-arrow-up", "btnMoveUp", "Functions/MoveUp");
  87. CreateBtn_Other("MoveDown", "FunctionMg", "下移菜单", 5, "fa fa-arrow-down", "btnMoveDown", "Functions/MoveDown");
  88. CreateBtn_Other("Refresh", "FunctionMg", "刷新菜单", 6, "fa fa-retweet", "btnRefresh", "Functions/Refresh");
  89. CreateMenu("LogMg", "SystemMg", "系统日志管理", 7, "SysLogs", "BaseSystem", "", "fa fa-list-alt");
  90. CreateBtn_Query("LogMg", 0);
  91. CreateMenu("UserHelpInfo", "HTSystem", "系统帮助信息", 101, "Index", "Help", "", "fa fa-question");
  92. CreateMenu("UserSysSetting", "HTSystem", "用户配置", 102, "", "", icon: "fa fa-cogs", isShow: false);
  93. CreateBtn_Other("LoginImage", "UserSysSetting", "配置登陆页背景", 1, "fa fa-cog", "_Admin_btnLoginImage", "Settings/LoginImage", false);
  94. CreateBtn_Other("HomeImage", "UserSysSetting", "配置主页背景", 2, "fa fa-cog", "_Admin_btnHomeImage", "Settings/HomeImage", false);
  95. CreateBtn_Other("RefreshCache", "UserSysSetting", "刷新缓存", 3, "fa fa-recycle", "_Admin_btnCache", "Settings/CacheRefresh", false);
  96. CreateBtn_Other("RefreshLang", "UserSysSetting", "刷新语言包", 4, "fa fa-recycle", "_Admin_btnLang", "Settings/LangRefresh", false);
  97. }
  98. #endregion
  99. #region CREATE
  100. /// <summary>
  101. ///创建增删改查按钮
  102. /// </summary>
  103. /// <param name="funName"></param>
  104. /// <param name="parentNo"></param>
  105. /// <param name="url">{IwbConsts.ApiAppUrl}{url}/Create 如果url与parentNo不同需指定</param>
  106. private void CreateBtn_Curd(string parentNo, string funName, string url = null)
  107. {
  108. CreateBtn_Query(parentNo, 0);
  109. CreateBtn_Create(funName, parentNo, 1, url);
  110. CreateBtn_Update(funName, parentNo, 2, url);
  111. CreateBtn_Delete(funName, parentNo, 3, url);
  112. }
  113. /// <summary>
  114. ///创建不是增删改查的按钮
  115. /// </summary>
  116. 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)
  117. {
  118. script = script ?? $"btn{funNo}";
  119. url = url ?? $"{parentNo}/{funNo}";
  120. url = isApiUrl ? $"{IwbConsts.ApiAppUrl}{url}" : url;
  121. if (isShow)
  122. {
  123. CreateBtn(funNo, parentNo, funName, sort, url, icon, script);
  124. }
  125. else
  126. {
  127. CreateBtn_NotShow(funNo, parentNo, funName, sort, icon, script, url);
  128. }
  129. }
  130. /// <summary>
  131. /// 创建目录
  132. /// </summary>
  133. private void CreateCatalog(string funNo, string parentNo, string funName, int sort,
  134. string icon = "fa fa-indent", string s = "", string c = "", bool auth = true, bool isShow = true)
  135. {
  136. if (isShow)
  137. {
  138. CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Catalog, "", "", "", icon, sort, c, s, auth);
  139. }
  140. else
  141. {
  142. CreateCatalog_NotShow(funNo, parentNo, funName, sort, icon, c, s, auth);
  143. }
  144. }
  145. /// <summary>
  146. /// 创建菜单
  147. /// </summary>
  148. private void CreateMenu(string funNo, string parentNo, string funName, int sort, string action,
  149. string controller, string s = "", string icon = "fa fa-bullseye", string c = "", string url = null,
  150. bool auth = true, bool isShow = true)
  151. {
  152. url = url ?? $"{controller}/{action}";
  153. if (isShow)
  154. {
  155. CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Menu, action, controller, url, icon, sort,
  156. c, s, auth);
  157. }
  158. else
  159. {
  160. CreateMenu_NotShow(funNo, parentNo, funName, sort, action, controller, s: s, icon: icon, c: c, url: url, auth: auth);
  161. }
  162. }
  163. /// <summary>
  164. /// 创建查询按钮
  165. /// </summary>
  166. private void CreateBtn_Query(string parentNo, int sort)
  167. {
  168. CreateBtn_NotShow("Query", parentNo, "查看页面", sort, "fa fa-eye", "btnQuery", "");
  169. }
  170. /// <summary>
  171. /// 创建添加按钮
  172. /// </summary>
  173. private void CreateBtn_Create(string funName, string parentNo, int sort, string url = null)
  174. {
  175. url = url ?? parentNo;
  176. CreateBtn("Create", parentNo, $"添加{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Create", "far fa-plus-square", "_btnCreate");
  177. }
  178. /// <summary>
  179. /// 创建修改按钮
  180. /// </summary>
  181. private void CreateBtn_Update(string funName, string parentNo, int sort, string url = null)
  182. {
  183. url = url ?? parentNo;
  184. CreateBtn("Update", parentNo, $"修改{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Update", "far fa-edit", "btnUpdate");
  185. }
  186. /// <summary>
  187. /// 创建删除按钮
  188. /// </summary>
  189. private void CreateBtn_Delete(string funName, string parentNo, int sort, string url = null)
  190. {
  191. url = url ?? parentNo;
  192. CreateBtn("Delete", parentNo, $"删除{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Delete", "far fa-minus-square", "btnDelete");
  193. }
  194. /// <summary>
  195. /// 创建按钮
  196. /// </summary>
  197. private void CreateBtn(string funNo, string parentNo, string funName, int sort, string url, string icon,
  198. string s, string c = "btn btn-default", string action = "", string controller = "", bool auth = true)
  199. {
  200. CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Button, action, controller, url, icon, sort, c, s, auth);
  201. }
  202. /// <summary>
  203. /// 创建不显示的目录
  204. /// </summary>
  205. private void CreateCatalog_NotShow(string funNo, string parentNo, string funName, int sort, string icon,
  206. string c = "", string s = "", bool auth = true)
  207. {
  208. CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, "", "", "", icon, sort, c, s, auth);
  209. }
  210. /// <summary>
  211. /// 创建不显示的菜单
  212. /// </summary>
  213. private void CreateMenu_NotShow(string funNo, string parentNo, string funName, int sort,
  214. string action, string controller, string s = "", string icon = "fa fa-bullseye",
  215. string c = "", string url = "", bool auth = true)
  216. {
  217. CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, action, controller, url, icon, sort, c, s, auth);
  218. }
  219. /// <summary>
  220. /// 创建不显示的按钮
  221. /// </summary>
  222. 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)
  223. {
  224. CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, action, controller, url, icon, sort, c, s, auth);
  225. }
  226. /// <summary>
  227. /// 创建功能菜单
  228. /// </summary>
  229. private void CreateFunctions(string funNo, string funName, string parentNo, int funType,
  230. string action, string controller, string url, string icon, int sort, string @class, string script, bool auth = true, string path = "", int? depth = null)
  231. {
  232. var fun = _context.SysFunctions.FirstOrDefault(e => e.FunctionNo == funNo && e.ParentNo == parentNo);
  233. if (fun == null)
  234. {
  235. string permissionName = path.Replace(",", ".");
  236. if (string.IsNullOrEmpty(path))
  237. {
  238. var parentFun = _context.SysFunctions.FirstOrDefault(f => f.FunctionNo == parentNo);
  239. if (parentFun != null)
  240. {
  241. //path = parentFun.FunctionPath + "," + funNo;
  242. permissionName = parentFun.PermissionName + "." + funNo;
  243. depth = depth ?? parentFun.Depth + 1;
  244. }
  245. else
  246. {
  247. throw new Exception($"未查询到父菜单【{funName}-No:{funNo}-ParentNo:{parentNo}】");
  248. }
  249. }
  250. url = url ?? controller + "/" + action;
  251. fun = new SysFunction
  252. {
  253. FunctionNo = funNo,
  254. ParentNo = parentNo,
  255. FunctionName = funName,
  256. PermissionName = permissionName,
  257. //FunctionPath = path,
  258. FunctionType = funType,
  259. Controller = controller,
  260. Action = action,
  261. Icon = icon,
  262. Sort = sort,
  263. Depth = depth ?? 0,
  264. Class = @class,
  265. Script = script,
  266. Url = url,
  267. NeedAuth = auth
  268. };
  269. _context.SysFunctions.Add(fun);
  270. _context.SaveChanges();
  271. }
  272. var permName = fun.PermissionName.Replace(".", "");
  273. PermissionStr += $" public const string {permName} = \"{fun.PermissionName}\";\r\n";
  274. if (!LangList.Contains(permName))
  275. {
  276. LangList.Add(permName);
  277. LangStr += $" <text name=\"{permName}\" value=\"{fun.FunctionName}\" />\r\n";
  278. }
  279. }
  280. #endregion
  281. #region WRITE
  282. private List<string> LangList { get; set; }
  283. private string LangStr { get; set; }
  284. private string PermissionStr { get; set; }
  285. private void Write()
  286. {
  287. WritePermission();
  288. WriteLang();
  289. }
  290. private void WritePermission()
  291. {
  292. if (string.IsNullOrEmpty(PermissionStr))
  293. {
  294. return;
  295. }
  296. try
  297. {
  298. string content = "namespace WeApp.Authorization\r\n";
  299. content += "{\r\n";
  300. content += " public static class PermissionNames\r\n";
  301. content += " {\r\n";
  302. content += PermissionStr;
  303. content += " }\r\n";
  304. content += "}\r\n";
  305. var filePath = Path.Combine(GetBasePath(), "WeApp.Core\\Authorization\\PermissionNames.cs");
  306. var fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite);//清空文件内容
  307. fs.Close();
  308. using (FileStream fr = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
  309. {
  310. fr.Seek(0, SeekOrigin.Begin);
  311. byte[] bytes = Encoding.UTF8.GetBytes(content);
  312. fr.Write(bytes, 0, bytes.Length);
  313. fr.Flush();
  314. }
  315. }
  316. catch (Exception e)
  317. {
  318. throw (new Exception("Permission:" + e.Message));
  319. }
  320. }
  321. private void WriteLang()
  322. {
  323. if (string.IsNullOrEmpty(LangStr))
  324. {
  325. return;
  326. }
  327. //LangStr = $"<!--FUNCTION NAME START-->\r\n{LangStr}\r\n<!--FUNCTION NAME End-->\r\n";
  328. LangStr += "\r\n";
  329. try
  330. {
  331. var filePath = Path.Combine(GetBasePath(), "WeApp.Web\\Localization\\SourceFiles\\Iwb-zh-Hans.xml");
  332. string content;
  333. using (FileStream fr = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  334. {
  335. fr.Seek(0, SeekOrigin.Begin);
  336. byte[] bytes = new byte[fr.Length];
  337. fr.Read(bytes, 0, bytes.Length);
  338. content = Encoding.UTF8.GetString(bytes);
  339. //Regex regex = new Regex("<!--FUNCTION NAME START-->(.*)<!--FUNCTION NAME End-->");
  340. //string content2 = regex.Replace(content, LangStr);
  341. string first = "<!--FUNCTION AND MENU NAME START-->";
  342. string second = "<!--FUNCTION AND MENU NAME END-->";
  343. if (!string.IsNullOrEmpty(content) && content.Contains(first) && content.Contains(second))
  344. {
  345. var start = content.IndexOf(first, 0, StringComparison.Ordinal) + first.Length;
  346. var end = content.IndexOf(second, start, StringComparison.Ordinal);
  347. string temp = content.Substring(start, end - start);
  348. content = content.Replace(temp, LangStr);
  349. }
  350. else
  351. {
  352. throw new Exception("Lang:语言配置信息没有生成!");
  353. }
  354. }
  355. if (!string.IsNullOrEmpty(content))
  356. {
  357. var fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite);//清空文件内容
  358. fs.Close();
  359. using (FileStream fr = new FileStream(filePath, FileMode.Append, FileAccess.Write))
  360. {
  361. byte[] newBytes = Encoding.UTF8.GetBytes(content);
  362. fr.Seek(0, SeekOrigin.Begin);
  363. fr.Write(newBytes, 0, newBytes.Length);
  364. fr.Flush();
  365. }
  366. }
  367. }
  368. catch (Exception e)
  369. {
  370. throw (new Exception("Lang:" + e.Message));
  371. }
  372. }
  373. private string GetBasePath()
  374. {
  375. var basePath = AppDomain.CurrentDomain.BaseDirectory;
  376. string path = Path.Combine(basePath.Substring(0, basePath.LastIndexOf("\\We.App", StringComparison.Ordinal)), "We.App\\");
  377. return path;
  378. }
  379. #endregion
  380. }
  381. }