popup.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="register" @ok="confirm" :title="title" minHeight="500">
  3. <BasicForm @register="registerForm" :model="model" />
  4. <!-- <MenuTree /> -->
  5. <div class="tree-wrap">
  6. <p class="tree-label">权限</p>
  7. <a-tree
  8. v-if="allowRule.length"
  9. checkable
  10. :tree-data="allowRule"
  11. showLine
  12. defaultExpandAll
  13. :replace-fields="replaceFields"
  14. v-model:checkedKeys="rules"
  15. />
  16. </div>
  17. </BasicModal>
  18. </template>
  19. <script lang="ts">
  20. import { Tree } from 'ant-design-vue';
  21. import { defineComponent, PropType, reactive, ref, toRefs } from 'vue';
  22. import { BasicModal, useModalInner } from '/@/components/Modal';
  23. import { BasicForm, useForm } from '/@/components/Form/index';
  24. import { schemas } from './data';
  25. import { getAllowRule } from '/@/api/sys/user';
  26. interface PopupData {
  27. title: string;
  28. treeData: object[];
  29. }
  30. interface AllowRule {
  31. id: number;
  32. }
  33. interface ReactData {
  34. id: number | string;
  35. allowRule: AllowRule[];
  36. initRules: number[];
  37. rules: number[];
  38. treeData: object[];
  39. }
  40. export default defineComponent({
  41. components: { BasicModal, BasicForm, [Tree.name]: Tree },
  42. props: {
  43. popupData: {
  44. type: Object as PropType<PopupData>,
  45. default: () => {},
  46. },
  47. },
  48. emits: ['saveData'],
  49. setup(props, { emit }) {
  50. const reactData = reactive<ReactData>({
  51. id: 0,
  52. allowRule: [],
  53. initRules: [],
  54. rules: [],
  55. treeData: [],
  56. });
  57. const popupData = props.popupData as PopupData;
  58. const modelRef = ref({});
  59. const replaceFields = {
  60. key: 'id',
  61. };
  62. // watch(reactData, () => {
  63. // console.log('rules', reactData.rules);
  64. // });
  65. const [
  66. registerForm,
  67. { getFieldsValue, updateSchema, setFieldsValue, resetFields /* validate*/ },
  68. ] = useForm({
  69. labelWidth: 100,
  70. schemas,
  71. showActionButtonGroup: false,
  72. actionColOptions: { span: 24 },
  73. });
  74. const [register, { closeModal }] = useModalInner((data) => {
  75. console.log(data);
  76. console.log('data=========================');
  77. resetFields();
  78. // if (unref(isUpdate)) {
  79. // setFieldsValue(data);
  80. // }
  81. if (data.id) {
  82. reactData.id = data.id;
  83. } else {
  84. reactData.id = 0;
  85. reactData.rules = [];
  86. }
  87. getRuleTree(data.pid);
  88. if (data.rules) {
  89. reactData.initRules = data.rules.split(',');
  90. reactData.initRules = reactData.initRules.map((item) => parseInt(item));
  91. }
  92. setFieldsValue(data);
  93. formatTreeData(popupData.treeData); // 禁止选取自己或自己下级为父id
  94. console.log(`popupData.treeData`, popupData.treeData);
  95. updateSchema({
  96. field: 'pid',
  97. componentProps: {
  98. onChange: (e: ChangeEvent) => {
  99. console.log('===========onChange1====ddd===');
  100. console.log(e);
  101. getRuleTree(e);
  102. },
  103. treeData: popupData.treeData,
  104. },
  105. });
  106. });
  107. function formatChildren(child) {
  108. child.map((item) => {
  109. item.selectable = false;
  110. if (item.children) {
  111. formatChildren(child);
  112. }
  113. });
  114. }
  115. // 添加不可选
  116. function formatTreeData(tree) {
  117. tree.map((item) => {
  118. if (item.children) {
  119. formatTreeData(item.children);
  120. }
  121. if (item.id === reactData.id) {
  122. item.selectable = false;
  123. if (item.children) {
  124. formatChildren(item.children);
  125. }
  126. } else {
  127. item.selectable = true;
  128. }
  129. });
  130. }
  131. function setReactDataId(allowRule) {
  132. allowRule.map((item) => {
  133. if (reactData.initRules.includes(item.id)) {
  134. reactData.rules.push(item.id);
  135. }
  136. if (item.children) {
  137. setReactDataId(item.children);
  138. }
  139. });
  140. }
  141. async function getRuleTree(id) {
  142. console.log(`id`, id);
  143. if (!id) {
  144. id = 1;
  145. }
  146. await getAllowRule({ id }).then((res) => {
  147. reactData.allowRule = res.tree as AllowRule[];
  148. reactData.rules = [];
  149. setReactDataId(reactData.allowRule);
  150. });
  151. }
  152. function confirm() {
  153. console.log('确定');
  154. const data = getFieldsValue();
  155. data.rules = [...reactData.rules];
  156. if (reactData.id) {
  157. data.id = reactData.id;
  158. }
  159. console.log(`pid`, data);
  160. console.log(`data`, data);
  161. const childerData = { closeModal, data };
  162. emit('saveData', childerData);
  163. }
  164. return {
  165. register,
  166. schemas,
  167. registerForm,
  168. replaceFields,
  169. model: modelRef,
  170. confirm,
  171. ...toRefs(popupData),
  172. ...toRefs(reactData),
  173. };
  174. },
  175. });
  176. </script>
  177. <style lang="less">
  178. .ant-form-item-label {
  179. text-align: center !important;
  180. }
  181. .tree-wrap {
  182. display: flex;
  183. }
  184. .tree-label {
  185. width: 20.6%;
  186. margin-top: 8px;
  187. margin-bottom: 1em;
  188. text-align: center;
  189. }
  190. @media (max-width: 639px) {
  191. .ant-form-item-label {
  192. line-height: 2.5715 !important;
  193. text-align: center !important;
  194. }
  195. .tree-label {
  196. width: 24%;
  197. margin-top: 8px;
  198. margin-bottom: 1em;
  199. text-align: center;
  200. }
  201. }
  202. </style>