You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

add.vue 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <a-modal v-model:visible="openAddModel" :title="title" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
  3. @cancel="cancelModal" width="50%">
  4. <a-form :model="createForm" :label-col="{span: 4}" labelAlign="right" style="margin-top: 20px;">
  5. <a-row gutter="20">
  6. <a-col span="24">
  7. <a-form-item required label="权限名称" name="name">
  8. <a-input v-model:value="createForm.name" placeholder="请输入权限名称" />
  9. </a-form-item>
  10. </a-col>
  11. <a-col span="24">
  12. <a-form-item label="路径" name="path">
  13. <a-input v-model:value="createForm.path" placeholder="请输入路径" />
  14. </a-form-item>
  15. </a-col>
  16. <a-col span="24">
  17. <a-form-item label="动作" name="action">
  18. <a-input v-model:value="createForm.action" placeholder="请输入动作" />
  19. </a-form-item>
  20. </a-col>
  21. <a-col span="24">
  22. <a-form-item label="模型" name="module">
  23. <a-input v-model:value="createForm.module" placeholder="请输入模型" />
  24. </a-form-item>
  25. </a-col>
  26. <a-col span="24">
  27. <a-form-item required label="权限类型" name="p_type">
  28. <a-radio-group v-model:value="createForm.p_type" @change="pTypeChange">
  29. <a-radio :value="1">权限</a-radio>
  30. <a-radio :value="2">菜单</a-radio>
  31. </a-radio-group>
  32. </a-form-item>
  33. </a-col>
  34. <a-col span="24" v-if="addOtherForm.isAddLevel">
  35. <a-form-item label="是否是顶级菜单" name="parent_id" >
  36. <a-radio-group v-model:value="addOtherForm.parent_id" @change="parentIdChange">
  37. <a-radio :value="-1">是</a-radio>
  38. <a-radio :value="0">否</a-radio>
  39. </a-radio-group>
  40. </a-form-item>
  41. </a-col>
  42. <a-col span="24" v-if="!addOtherForm.isTopLevel">
  43. <a-form-item label="上级菜单id" name="parent_id" >
  44. <a-cascader v-model:value="cascaderParentId" @change="parentChange"
  45. :options="listOptions" :fieldNames="listFieldNames" :changeOnSelect="true"
  46. placeholder="请选择所属的上级菜单" style="width: 100%" />
  47. </a-form-item>
  48. </a-col>
  49. <a-col span="24">
  50. <a-form-item label="描述" name="description">
  51. <a-input v-model:value="createForm.description" placeholder="请输入描述" />
  52. </a-form-item>
  53. </a-col>
  54. </a-row>
  55. </a-form>
  56. </a-modal>
  57. </template>
  58. <script setup lang="ts">
  59. import { ref, onMounted, computed, defineProps, watch, defineEmits } from 'vue';
  60. import { addPermission, updatePermission, getPermissionListWithchilds } from '@/apis/models';
  61. import { dataForm, otherDataForm, reset } from '@/views/permission/list/add/data.ts';
  62. import { useCommon } from '@/hooks/useCommon';
  63. import { filterChildPermissionsByType } from '@/utils/dataHelper.ts';
  64. let { store, openAddModel, hideModal, message, } = useCommon();
  65. const emit = defineEmits();
  66. let props = defineProps(['edit_record','level_record']);
  67. let title = ref<String>('新增权限/菜单');
  68. let listOptions = ref([]);
  69. let cascaderParentId = ref([]);
  70. const listFieldNames = ref({ label: 'name', value: 'id', children: 'childs' });
  71. let createForm = ref<companyDepartmentType.addFormType>(dataForm)
  72. let addOtherForm = ref<companyDepartmentType.addOtherFormType>(otherDataForm)
  73. onMounted(() => {
  74. getPermissionListWithchilds({page:1, pagesize: 999}).then(res => {
  75. listOptions.value = filterChildPermissionsByType(res.data.permissions)
  76. })
  77. })
  78. const pTypeChange = (val) => {
  79. if(val.target.value == 1) {
  80. addOtherForm.value.isAddLevel = false;
  81. } else {
  82. addOtherForm.value.isAddLevel = true;
  83. }
  84. addOtherForm.value.isAddLevel == false
  85. addOtherForm.value.parent_id = addOtherForm.value.parent_id != 0 ? addOtherForm.value.parent_id : 0;
  86. createForm.value.parent_id = createForm.value.parent_id != 0 ? createForm.value.parent_id : 0;
  87. cascaderParentId.value = []
  88. }
  89. const parentIdChange = (val) => {
  90. cascaderParentId.value = []
  91. createForm.value.parent_id = val.target.value;
  92. val.target.value == 0 ? addOtherForm.value.isTopLevel = false : addOtherForm.value.isTopLevel = true
  93. }
  94. const parentChange = (val) => {
  95. createForm.value.parent_id = val.length > 0 ? val[val.length - 1] : 0;
  96. }
  97. const sumbitForm = () => {
  98. if (createForm.value.name && createForm.value.p_type) {
  99. if (!createForm.value.id) {
  100. addPermission(createForm.value).then(res => {
  101. getPermissionListWithchilds({page:1, pagesize: 999}).then(res => {
  102. listOptions.value = filterChildPermissionsByType(res.data.permissions)
  103. })
  104. message.success('新增菜单/权限成功');
  105. hideModal();
  106. resetForm();
  107. emit('successAdd');
  108. }).catch(err => {
  109. })
  110. } else {
  111. updatePermission(createForm.value).then(res => {
  112. getPermissionListWithchilds({page:1, pagesize: 999}).then(res => {
  113. listOptions.value = filterChildPermissionsByType(res.data.permissions)
  114. })
  115. message.success('修改菜单/权限成功');
  116. hideModal();
  117. resetForm();
  118. emit('successAdd');
  119. }).catch(err => {
  120. })
  121. }
  122. } else {
  123. message.warning('请补充完整信息');
  124. }
  125. }
  126. const resetForm = () => {
  127. createForm.value = reset().dataForm;
  128. addOtherForm.value = reset().otherDataForm;
  129. cascaderParentId.value = []
  130. }
  131. const cancelModal = () => {
  132. emit('closeAdd');
  133. resetForm();
  134. hideModal();
  135. }
  136. openAddModel = computed(() => {
  137. return store.state.openAddModel;
  138. })
  139. watch(() => [props.edit_record, props.level_record], (newVal) => {
  140. console.log(newVal)
  141. if (newVal[0]) {
  142. title.value = "编辑权限/菜单";
  143. console.log(newVal)
  144. createForm.value = {
  145. id: newVal[0].id,
  146. name: newVal[0].name,
  147. path: newVal[0].path,
  148. action: newVal[0].action,
  149. module: newVal[0].module,
  150. p_type: newVal[0].p_type,
  151. parent_id: newVal[0].parent_id,
  152. description: newVal[0].description,
  153. }
  154. if(newVal[0].parent_id == -1) {
  155. cascaderParentId.value.push(newVal[0].id);
  156. addOtherForm.value.isAddLevel = true;
  157. addOtherForm.value.isTopLevel = true;
  158. addOtherForm.value.parent_id = -1;
  159. } else {
  160. cascaderParentId.value.push(newVal[0].parent_id);
  161. cascaderParentId.value.push(newVal[0].id);
  162. addOtherForm.value.isAddLevel = true
  163. addOtherForm.value.isTopLevel = false
  164. addOtherForm.value.parent_id = 0;
  165. }
  166. } else if(newVal[1]){
  167. title.value = "添加下级权限/菜单";
  168. createForm.value.p_type = newVal[1].p_type
  169. cascaderParentId.value = [];
  170. if(newVal[1].parent_id == -1) {
  171. cascaderParentId.value.push(newVal[1].id);
  172. addOtherForm.value.isAddLevel = true;
  173. addOtherForm.value.isTopLevel = true;
  174. addOtherForm.value.parent_id = -1;
  175. } else {
  176. cascaderParentId.value.push(newVal[1].parent_id);
  177. cascaderParentId.value.push(newVal[1].id);
  178. addOtherForm.value.isAddLevel = true
  179. addOtherForm.value.isTopLevel = false
  180. addOtherForm.value.parent_id = 0;
  181. }
  182. createForm.value.parent_id = newVal[1].id
  183. } else {
  184. title.value = "编辑权限/菜单";
  185. }
  186. })
  187. </script>
  188. <style>
  189. </style>