perms.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <el-cascader
  3. :options="options"
  4. :props="{ multiple: true }"
  5. separator=":"
  6. clearable
  7. filterable
  8. v-model="newValue"
  9. @change="onChange"
  10. ></el-cascader>
  11. </template>
  12. <script>
  13. export default {
  14. name: "cl-menu-perms",
  15. props: {
  16. value: [String, Number, Array]
  17. },
  18. data() {
  19. return {
  20. options: [],
  21. newValue: []
  22. };
  23. },
  24. watch: {
  25. value() {
  26. this.parse();
  27. }
  28. },
  29. created() {
  30. let options = [];
  31. let list = [];
  32. const flat = (obj) => {
  33. for (let i in obj) {
  34. let { permission } = obj[i];
  35. if (permission) {
  36. list = [...list, Object.values(permission)].flat();
  37. } else {
  38. flat(obj[i]);
  39. }
  40. }
  41. };
  42. flat(this.$service);
  43. list.filter((e) => e.includes(":"))
  44. .map((e) => e.split(":"))
  45. .forEach((arr) => {
  46. const col = (i, d) => {
  47. let key = arr[i];
  48. let index = d.findIndex((e) => e.label == key);
  49. if (index >= 0) {
  50. col(i + 1, d[index].children);
  51. } else {
  52. let isLast = i == arr.length - 1;
  53. d.push({
  54. label: key,
  55. value: key,
  56. children: isLast ? null : []
  57. });
  58. if (!isLast) {
  59. col(i + 1, d[d.length - 1].children || []);
  60. }
  61. }
  62. };
  63. col(0, options);
  64. });
  65. this.options = options;
  66. },
  67. mounted() {
  68. this.parse();
  69. },
  70. methods: {
  71. parse() {
  72. this.newValue = this.value ? this.value.split(",").map((e) => e.split(":")) : [];
  73. },
  74. onChange(row) {
  75. this.$emit("input", row.map((e) => e.join(":")).join(","));
  76. }
  77. }
  78. };
  79. </script>