DefaultFunctionsCreator.cs 20 KB

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