authFilter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import {USER_AUTH, SYS_BUTTON_AUTH} from "@/store/mutation-types"
  2. export function disabledAuthFilter(code, formData) {
  3. if (nodeDisabledAuth(code, formData)) {
  4. return true;
  5. } else {
  6. return globalDisabledAuth(code);
  7. }
  8. }
  9. function nodeDisabledAuth(code, formData) {
  10. // console.log("页面权限禁用--NODE--开始");
  11. var permissionList = [];
  12. try {
  13. var obj = formData;
  14. //console.log("页面权限禁用--NODE--开始",obj);
  15. if (obj) {
  16. let bpmList = obj.permissionList;
  17. for (var bpm of bpmList) {
  18. if (bpm.type == '2') {
  19. permissionList.push(bpm);
  20. }
  21. }
  22. }
  23. } catch (e) {
  24. //console.log("页面权限异常----", e);
  25. }
  26. if (permissionList === null || permissionList === "" || permissionList === undefined || permissionList.length <= 0) {
  27. return false;
  28. }
  29. let permissions = [];
  30. for (var item of permissionList) {
  31. if (item.type == '2') {
  32. permissions.push(item.action);
  33. }
  34. }
  35. //console.log("页面权限----"+code);
  36. if (!permissions.includes(code)) {
  37. return false;
  38. } else {
  39. for (var item2 of permissionList) {
  40. if (code === item2.action) {
  41. // console.log("页面权限禁用--NODE--生效");
  42. return true;
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. function globalDisabledAuth(code) {
  49. // console.log("页面禁用权限--Global--开始");
  50. var permissionList = [];
  51. var allPermissionList = [];
  52. //let authList = Vue.ls.get(USER_AUTH);
  53. let authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || "[]");
  54. for (var auth of authList) {
  55. if (auth.type == '2') {
  56. permissionList.push(auth);
  57. }
  58. }
  59. //console.log("页面禁用权限--Global--",sessionStorage.getItem(SYS_BUTTON_AUTH));
  60. let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || "[]");
  61. for (var gauth of allAuthList) {
  62. if (gauth.type == '2') {
  63. allPermissionList.push(gauth);
  64. }
  65. }
  66. //设置全局配置是否有命中
  67. var gFlag = false;//禁用命中
  68. var invalidFlag = false;//无效命中
  69. if (allPermissionList != null && allPermissionList != "" && allPermissionList != undefined && allPermissionList.length > 0) {
  70. for (var itemG of allPermissionList) {
  71. if (code === itemG.action) {
  72. if (itemG.status == '0') {
  73. invalidFlag = true;
  74. break;
  75. } else {
  76. gFlag = true;
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. if (invalidFlag) {
  83. return false;
  84. }
  85. if (permissionList === null || permissionList === "" || permissionList === undefined || permissionList.length <= 0) {
  86. return gFlag;
  87. }
  88. let permissions = [];
  89. for (var item of permissionList) {
  90. if (item.type == '2') {
  91. permissions.push(item.action);
  92. }
  93. }
  94. //console.log("页面禁用权限----"+code);
  95. if (!permissions.includes(code)) {
  96. return gFlag;
  97. } else {
  98. for (var item2 of permissionList) {
  99. if (code === item2.action) {
  100. console.log("页面权限解除禁用--Global--生效");
  101. gFlag = false;
  102. }
  103. }
  104. return gFlag;
  105. }
  106. }
  107. export function colAuthFilter(columns, pre) {
  108. var authList = getNoAuthCols(pre);
  109. const cols = columns.filter(item => {
  110. if (hasColoum(item, authList)) {
  111. return true
  112. }
  113. return false
  114. })
  115. return cols
  116. }
  117. function hasColoum(item, authList) {
  118. if (authList.includes(item.dataIndex)) {
  119. return false
  120. }
  121. return true
  122. }
  123. //权限无效时不做控制,有效时控制,只能控制 显示不显示
  124. //根据授权码前缀获取未授权的列信息
  125. function getNoAuthCols(pre) {
  126. var permissionList = [];
  127. var allPermissionList = [];
  128. //let authList = Vue.ls.get(USER_AUTH);
  129. let authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || "[]");
  130. for (var auth of authList) {
  131. //显示策略,有效状态
  132. if (auth.type == '1' && startWith(auth.action, pre)) {
  133. permissionList.push(substrPre(auth.action, pre));
  134. }
  135. }
  136. //console.log("页面禁用权限--Global--",sessionStorage.getItem(SYS_BUTTON_AUTH));
  137. let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || "[]");
  138. for (var gauth of allAuthList) {
  139. //显示策略,有效状态
  140. if (gauth.type == '1' && gauth.status == '1' && startWith(gauth.action, pre)) {
  141. allPermissionList.push(substrPre(gauth.action, pre));
  142. }
  143. }
  144. const cols = allPermissionList.filter(item => {
  145. if (permissionList.includes(item)) {
  146. return false;
  147. }
  148. return true;
  149. })
  150. return cols;
  151. }
  152. function startWith(str, pre) {
  153. if (pre == null || pre == "" || str == null || str == "" || str.length == 0 || pre.length > str.length)
  154. return false;
  155. if (str.substr(0, pre.length) == pre)
  156. return true;
  157. else
  158. return false;
  159. }
  160. function substrPre(str, pre) {
  161. return str.substr(pre.length);
  162. }