PrivilegeSet.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <div class="privilege-set">
  3. <table class="table">
  4. <colgroup>
  5. <col width="120" />
  6. <col width="120" />
  7. <col width="140" />
  8. <col width="60" />
  9. <col width="210" />
  10. </colgroup>
  11. <tbody>
  12. <tr>
  13. <th v-for="(item, index) in tableHead" :key="index">{{ item }}</th>
  14. </tr>
  15. <tr v-for="row in tableData" :key="row.id">
  16. <td v-for="(col, cindex) in row.columns" :key="cindex">
  17. <div v-if="col && col.type === 'page'">{{ col.name }}</div>
  18. <div v-else-if="col && col.type === 'page-checkbox'">
  19. <el-checkbox
  20. v-model="row.enable"
  21. :disabled="row.disabled"
  22. @change="(enable) => pageSelectChange(row, enable)"
  23. ></el-checkbox>
  24. </div>
  25. <div
  26. v-else-if="
  27. row.isPage && col && col.type === 'page-data-permission'
  28. "
  29. >
  30. <el-select
  31. v-model="row.dataPermissionType"
  32. class="width-200"
  33. placeholder="请选择"
  34. :disabled="row.disabled"
  35. >
  36. <el-option
  37. v-for="(val, key) in DATA_PRIVILEGE_TYPE"
  38. :key="key"
  39. :value="key"
  40. :label="val"
  41. ></el-option>
  42. </el-select>
  43. </div>
  44. <div v-else-if="col && col.type">
  45. <div
  46. class="cell-check-list"
  47. v-for="item in col.datas"
  48. :key="item.field"
  49. >
  50. <el-checkbox
  51. v-model="item.enable"
  52. :disabled="item.disabled"
  53. @change="(enable) => typeSelectChange(row, enable)"
  54. >{{ item.name }}</el-checkbox
  55. >
  56. </div>
  57. </div>
  58. <div v-else></div>
  59. </td>
  60. </tr>
  61. </tbody>
  62. </table>
  63. </div>
  64. </template>
  65. <script>
  66. import { DATA_PRIVILEGE_TYPE } from "../../../constants/enumerate";
  67. export default {
  68. name: "privilege-set",
  69. props: {
  70. menus: {
  71. type: Array,
  72. default() {
  73. return [];
  74. },
  75. },
  76. showPermission: {
  77. type: Boolean,
  78. default: true,
  79. },
  80. disabledPermissionUrls: {
  81. type: Array,
  82. default() {
  83. return ["TaskApplyManage", "TaskReviewManage"];
  84. },
  85. },
  86. },
  87. data() {
  88. return {
  89. maxDeep: 0,
  90. tableData: [],
  91. tableHead: [],
  92. DATA_PRIVILEGE_TYPE,
  93. };
  94. },
  95. created() {
  96. this.initData();
  97. },
  98. methods: {
  99. initData() {
  100. this.maxDeep = this.getNavsDeep();
  101. this.tableHead = this.buildTableHead();
  102. // this.buildTableData();
  103. },
  104. getNavsDeep() {
  105. let maxDeep = 0;
  106. const getDeep = (navs, deep) => {
  107. ++deep;
  108. navs.forEach((nav) => {
  109. if (maxDeep < deep) maxDeep = deep;
  110. if (nav.children && nav.children.length) getDeep(nav.children, deep);
  111. });
  112. };
  113. getDeep(this.menus, maxDeep);
  114. return maxDeep;
  115. },
  116. buildTableHead() {
  117. let headers = [];
  118. let codes = ["一", "二", "三", "四", "五", "六", "七", "八"];
  119. for (let index = 0; index < this.maxDeep; index++) {
  120. headers.push(`${codes[index]}级页面`);
  121. }
  122. headers = [...headers, "页面"];
  123. if (this.showPermission) headers.push("数据权限");
  124. headers = [...headers, "查询条件", "功能按钮", "列表展示", "操作列"];
  125. return headers;
  126. },
  127. buildTableData(
  128. privilegeIds = [],
  129. dataPermissionInfo = [],
  130. disabledIds = []
  131. ) {
  132. let tableData = [];
  133. const privColumnCount = this.showPermission ? 6 : 5;
  134. let tableColumnCount = this.maxDeep + privColumnCount;
  135. const pageSetTypes = ["conditions", "buttons", "lists", "links"];
  136. let datePermissionMap = {};
  137. dataPermissionInfo.forEach((item) => {
  138. datePermissionMap[item.privilegeId] = item.dataPermissionType;
  139. });
  140. const buildData = (navs, deep) => {
  141. ++deep;
  142. navs.forEach((nav) => {
  143. const isDisabledPermissionUrl = this.disabledPermissionUrls.includes(
  144. nav.url
  145. );
  146. let columns = new Array(tableColumnCount);
  147. columns[deep - 1] = { type: "page", name: nav.name };
  148. columns[this.maxDeep] = {
  149. type: "page-checkbox",
  150. };
  151. if (this.showPermission && !isDisabledPermissionUrl) {
  152. columns[this.maxDeep + 1] = {
  153. type: "page-data-permission",
  154. };
  155. }
  156. const isPage = pageSetTypes.some(
  157. (type) => nav[type] && nav[type].length
  158. );
  159. const offsetPageSetInd = this.showPermission ? 2 : 1;
  160. if (isPage) {
  161. pageSetTypes.forEach((type, index) => {
  162. const datas = !nav[type]
  163. ? []
  164. : nav[type].map((elem) => {
  165. let data = { ...elem };
  166. data.enable = privilegeIds.includes(elem.id);
  167. data.disabled = disabledIds.includes(elem.id);
  168. return data;
  169. });
  170. columns[this.maxDeep + index + offsetPageSetInd] = {
  171. type,
  172. datas,
  173. };
  174. });
  175. }
  176. tableData.push({
  177. id: nav.id,
  178. name: nav.name,
  179. url: nav.url,
  180. enable: privilegeIds.includes(nav.id),
  181. disabled: disabledIds.includes(nav.id),
  182. dataPermissionType:
  183. isPage && this.showPermission && !isDisabledPermissionUrl
  184. ? datePermissionMap[nav.id] || "SELF"
  185. : null,
  186. type: nav.type,
  187. parentId: nav.parentId,
  188. isPage,
  189. columns,
  190. });
  191. if (nav.children && nav.children.length)
  192. buildData(nav.children, deep);
  193. });
  194. };
  195. buildData(this.menus, 0);
  196. this.tableData = tableData;
  197. },
  198. resetdataPermissionType(val) {
  199. this.tableData.forEach((item) => {
  200. if (
  201. item.isPage &&
  202. !item.disabled &&
  203. this.showPermission &&
  204. !this.disabledPermissionUrls.includes(item.url)
  205. )
  206. item.dataPermissionType = val;
  207. });
  208. },
  209. getSelectedPrivileges() {
  210. let privilegeIds = [];
  211. let dataPermissionInfo = [];
  212. this.tableData
  213. .filter((row) => row.enable)
  214. .forEach((row) => {
  215. privilegeIds.push(row.id);
  216. dataPermissionInfo.push({
  217. privilegeId: row.id,
  218. dataPermissionType: row.dataPermissionType,
  219. });
  220. row.columns.forEach((column) => {
  221. if (!column.datas || !column.datas.length) return;
  222. column.datas.forEach((item) => {
  223. if (item.enable) privilegeIds.push(item.id);
  224. });
  225. });
  226. });
  227. return { privilegeIds, dataPermissionInfo };
  228. },
  229. // set change
  230. pageSelectChange(row, enable) {
  231. this.changRowColumnEnable(row, enable);
  232. this.changeParentNodeSelected(row.parentId, enable);
  233. this.changeChildrenNodeSelected(row.id, enable);
  234. },
  235. typeSelectChange(row, enable) {
  236. if (!row.enable && enable) {
  237. row.enable = enable;
  238. this.changeParentNodeSelected(row.parentId, enable);
  239. this.changeChildrenNodeSelected(row.id, enable);
  240. }
  241. },
  242. changRowColumnEnable(row, enable) {
  243. if (!row.isPage) return;
  244. row.columns.forEach((column) => {
  245. if (!column.datas || !column.datas.length) return;
  246. column.datas.forEach((item) => {
  247. item.enable = enable;
  248. });
  249. });
  250. },
  251. changeParentNodeSelected(parentId, enable) {
  252. if (!parentId) return;
  253. let curParentId = parentId;
  254. if (enable) {
  255. while (curParentId) {
  256. let curParentNode = this.tableData.find(
  257. (row) => row.id === curParentId
  258. );
  259. curParentNode.enable = enable;
  260. curParentId = curParentNode.parentId;
  261. this.changRowColumnEnable(curParentNode, enable);
  262. }
  263. } else {
  264. while (curParentId) {
  265. let curParentNode = this.tableData.find(
  266. (row) => row.id === curParentId
  267. );
  268. let childrenHasOneSelected = this.tableData
  269. .filter((row) => row.parentId === curParentId)
  270. .some((row) => row.enable);
  271. curParentNode.enable = childrenHasOneSelected;
  272. curParentId = curParentNode.parentId;
  273. this.changRowColumnEnable(curParentNode, enable);
  274. }
  275. }
  276. },
  277. changeChildrenNodeSelected(id, enable) {
  278. if (!id) return;
  279. let curIds = [id];
  280. while (curIds.length) {
  281. const validNodes = this.tableData.filter((row) =>
  282. curIds.includes(row.parentId)
  283. );
  284. validNodes.forEach((row) => {
  285. row.enable = enable;
  286. this.changRowColumnEnable(row, enable);
  287. });
  288. curIds = validNodes.map((row) => row.id);
  289. }
  290. },
  291. },
  292. };
  293. </script>