BaseClass.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using MySystem.Models.Bs;
  4. using System.Data;
  5. namespace MySystem
  6. {
  7. /// <summary>
  8. /// 基础功能方法
  9. /// </summary>
  10. public class BaseClass
  11. {
  12. public static Models.Bs.WebCMSEntities bsdb = new Models.Bs.WebCMSEntities();
  13. #region 菜单权限json
  14. /// <summary>
  15. /// 菜单权限json
  16. /// </summary>
  17. /// <returns></returns>
  18. public static string GetRightJson()
  19. {
  20. string result = "[";
  21. List<RightDic> list = bsdb.RightDic.ToList();
  22. List<RightDic> Level1 = list.Where(m => m.RightLevel == 1).ToList();
  23. foreach (RightDic sub1 in Level1)
  24. {
  25. result += "{\"title\": \"" + sub1.Title + "\",\"name\": \"" + sub1.Name + "\",\"id\": \"" + sub1.Id + "\", \"spread\": false, \"children\": [";
  26. List<RightDic> Level2 = list.Where(m => m.RightLevel == 2 && m.Id.StartsWith(sub1.Id)).ToList();
  27. if (Level2.Count > 0)
  28. {
  29. foreach (RightDic sub2 in Level2)
  30. {
  31. result += "{\"title\": \"" + sub2.Title + "\",\"name\": \"" + sub2.Name + "\",\"id\": \"" + sub2.Id + "\", \"spread\": false, \"children\": [";
  32. List<RightDic> Level3 = list.Where(m => m.RightLevel == 3 && m.Id.StartsWith(sub2.Id)).ToList();
  33. if (Level3.Count > 0)
  34. {
  35. foreach (RightDic sub3 in Level3)
  36. {
  37. result += "{\"title\": \"" + sub3.Title + "\",\"name\": \"" + sub3.Name + "\",\"id\": \"" + sub3.Id + "\", \"spread\": false, \"children\": [";
  38. string rightString = ForOperateRight(sub3.Id);
  39. if (!string.IsNullOrEmpty(rightString))
  40. {
  41. result += rightString;
  42. }
  43. // else
  44. // {
  45. // result += "{title: '基本权限', id: '" + sub3.Id + "_base'}";
  46. // }
  47. result += "]},";
  48. }
  49. result = result.TrimEnd(',');
  50. }
  51. else
  52. {
  53. string rightString = ForOperateRight(sub2.Id);
  54. if (!string.IsNullOrEmpty(rightString))
  55. {
  56. result += rightString;
  57. }
  58. // else
  59. // {
  60. // result += "{title: '基本权限', id: '" + sub2.Id + "_base'}";
  61. // }
  62. }
  63. result += "]},";
  64. }
  65. result = result.TrimEnd(',');
  66. }
  67. else
  68. {
  69. result += ForOperateRight(sub1.Id);
  70. string rightString = ForOperateRight(sub1.Id);
  71. if (!string.IsNullOrEmpty(rightString))
  72. {
  73. result += rightString;
  74. }
  75. // else
  76. // {
  77. // result += "{title: '基本权限', id: '" + sub1.Id + "_base'}";
  78. // }
  79. }
  80. result += "]},";
  81. }
  82. result = result.TrimEnd(',');
  83. result += "]";
  84. return result;
  85. }
  86. //读取当前菜单有哪些权限
  87. public static string ForOperateRight(string Id)
  88. {
  89. string result = "";
  90. List<MenuRight> rights = bsdb.MenuRight.Where(m => m.MenuId.StartsWith(Id)).OrderBy(m => m.MenuId).ToList();
  91. foreach (MenuRight right in rights)
  92. {
  93. result += "{\"title\": \"" + right.Name + "\",\"id\": \"" + right.MenuId + "\"},";
  94. }
  95. RightDic rightDic = bsdb.RightDic.FirstOrDefault(m => m.Id == Id) ?? new RightDic();
  96. if (!string.IsNullOrEmpty(rightDic.OtherRight))
  97. {
  98. string[] OtherRightList = rightDic.OtherRight.Split('\n');
  99. foreach (string SubOtherRight in OtherRightList)
  100. {
  101. string[] SubOtherRightData = SubOtherRight.Split('_');
  102. result += "{\"title\": \"" + SubOtherRightData[1] + "\",\"id\": \"" + Id + "_" + SubOtherRightData[0] + "\"},";
  103. }
  104. }
  105. result = result.TrimEnd(',');
  106. return result;
  107. }
  108. #endregion
  109. }
  110. }