DefaultFunctionsCreator.cs 18 KB

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