productCassificationModel.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!-- 产品档案 -->
  2. <template>
  3. <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" width="550px" @ok="handleSubmit">
  4. <BasicForm @register="registerForm" @submit="handleSubmit" style="margin-top: 26px" />
  5. </BasicModal>
  6. </template>
  7. <script lang="ts" setup>
  8. import { ref, computed, unref,onMounted } from 'vue';
  9. //引入依赖
  10. import { useForm, BasicForm, FormSchema } from '/@/components/Form';
  11. import { BasicModal, useModalInner } from '/@/components/Modal';
  12. import { saveOrUpdateDict } from '../api/productCassification.api';
  13. import { defHttp } from '/@/utils/http/axios';
  14. const emit = defineEmits(['register', 'success']); //定义emit
  15. const isUpdate = ref(true); //判断编辑还是新增
  16. const rowId = ref('');//获取主键
  17. let classOption = ref([])
  18. //获取表单数据
  19. const [registerModal, { closeModal, setModalProps }] = useModalInner(
  20. async (data) => {
  21. // 重置表单
  22. await resetFields();
  23. setModalProps({ confirmLoading: false});
  24. isUpdate.value = !!data?.isUpdate;
  25. getOptions(isUpdate,data.record)
  26. if (unref(isUpdate)) {
  27. rowId.value = data.record.id;
  28. //表单赋值
  29. await setFieldsValue({
  30. ...data.record,
  31. });
  32. }
  33. }
  34. )
  35. function getOptions(isUpdate,data) {
  36. defHttp
  37. .get({ url: 'baseCode/baseProductClass/list'}, { isTransformResponse: false })
  38. .then((res) => {
  39. if (res.success) {
  40. var option = []
  41. res.result.records.forEach(element => {
  42. var obj = {
  43. label: element.name,
  44. value: element.id
  45. };
  46. option.push(obj)
  47. });
  48. classOption.value = []
  49. if(unref(isUpdate)){ //编辑
  50. var row = JSON.parse(JSON.stringify(data))
  51. option.map(item=>{
  52. if(item.label!=row.parentId){
  53. classOption.value.push(item)
  54. }
  55. })
  56. }else{
  57. classOption.value = option
  58. }
  59. }
  60. })
  61. .finally(() => {
  62. // loading.value = false;
  63. });
  64. }
  65. //获取title
  66. const getTitle = computed(() => {
  67. return (!unref(isUpdate) ? '新增产品分类(add)' : '编辑产品分类(edit)');
  68. })
  69. //自定义表单字段
  70. const formSchemas: FormSchema[] = [
  71. {
  72. //标题名称
  73. label: '上级分类(parent)',
  74. //字段
  75. field: 'parentId',
  76. //组件 支持组件详见 components/Form/src/types/index.ts 中的 ComponentType
  77. component: 'JSelectInput',
  78. componentProps:{
  79. options: classOption,
  80. },
  81. },
  82. {
  83. label: '编码(code)',
  84. field: 'code',
  85. component: 'Input',
  86. // required: true,
  87. componentProps:{
  88. AutoComplete:'off',
  89. placeholder:'请输入,为空则自动生成'
  90. },
  91. },
  92. {
  93. label: '名称(name)',
  94. field: 'name',
  95. component: 'Input',
  96. required: true,
  97. componentProps:{
  98. AutoComplete:'off'
  99. },
  100. },
  101. {
  102. label: '毛利率(gross margin)',
  103. field: 'grossMargin',
  104. component: 'Input',
  105. componentProps:{
  106. AutoComplete:'off',
  107. addonAfter:"%",
  108. },
  109. },
  110. {
  111. label: '税率(tax rate)',
  112. field: 'taxRate',
  113. component: 'Input',
  114. componentProps:{
  115. AutoComplete:'off',
  116. addonAfter:"%",
  117. },
  118. },
  119. {
  120. label: '',
  121. field: 'id',
  122. component: 'Input',
  123. show: false
  124. },
  125. ];
  126. /**
  127. * BasicForm绑定注册;
  128. * useForm 是整个框架的核心用于表单渲染,里边封装了很多公共方法;
  129. * 支持(schemas: 渲染表单列,autoSubmitOnEnter:回车提交,submitButtonOptions:自定义按钮文本和图标等方法);
  130. * 平台通过此封装,简化了代码,支持自定义扩展;
  131. */
  132. const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  133. //注册表单列
  134. schemas: formSchemas,
  135. //回车提交
  136. autoSubmitOnEnter: true,
  137. //不显示重置按钮
  138. showResetButton: false,
  139. showSubmitButton:false,
  140. labelCol: { style: { width: '150px'} },
  141. labelAlign:'right'
  142. //查询列占比 24代表一行 取值范围 0-24
  143. });
  144. /**
  145. * 点击提交按钮的value值
  146. * @param values
  147. */
  148. //表单提交事件
  149. async function handleSubmit() {
  150. try {
  151. let values = await validate();
  152. setModalProps({ confirmLoading: true });
  153. //提交表单
  154. await saveOrUpdateDict(values, isUpdate.value);
  155. //关闭弹窗
  156. closeModal();
  157. //刷新列表
  158. emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
  159. } finally {
  160. setModalProps({ confirmLoading: false });
  161. }
  162. }
  163. </script>