productCassificationModel.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!-- 产品分类-->
  2. <template>
  3. <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" width="550px" @ok="handleSubmit" :maskClosable="false">
  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, list } from '../api/productCassification.api';
  13. import { defHttp } from '/@/utils/http/axios';
  14. import { useMessage } from '/@/hooks/web/useMessage';
  15. const { createConfirm } = useMessage();
  16. const emit = defineEmits(['register', 'success']); //定义emit
  17. const isUpdate = ref(true); //判断编辑还是新增
  18. const rowId = ref(''); //获取主键
  19. let classOption = ref([]);
  20. //获取表单数据
  21. const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
  22. // 重置表单
  23. await resetFields();
  24. setModalProps({ confirmLoading: false });
  25. isUpdate.value = !!data?.isUpdate;
  26. getOptions(isUpdate, data.record);
  27. if (unref(isUpdate)) {
  28. rowId.value = data.record.id;
  29. //表单赋值
  30. await setFieldsValue({
  31. ...data.record,
  32. });
  33. }
  34. });
  35. function getOptions(isUpdate, data) {
  36. var params = { pageSize: -1 };
  37. defHttp
  38. .get({ url: 'baseCode/baseProductClass/list', params }, { isTransformResponse: false })
  39. .then((res) => {
  40. if (res.success) {
  41. var option = [];
  42. res.result.records.forEach((element) => {
  43. var obj = {
  44. label: element.name,
  45. value: element.id,
  46. };
  47. option.push(obj);
  48. });
  49. classOption.value = [];
  50. if (unref(isUpdate)) {
  51. //编辑
  52. var row = JSON.parse(JSON.stringify(data));
  53. option.map((item) => {
  54. if (item.label != row.parentId) {
  55. classOption.value.push(item);
  56. }
  57. });
  58. } else {
  59. classOption.value = option;
  60. }
  61. }
  62. })
  63. .finally(() => {
  64. // loading.value = false;
  65. });
  66. }
  67. //获取title
  68. const getTitle = computed(() => {
  69. return !unref(isUpdate) ? '新增产品分类(add)' : '编辑产品分类(edit)';
  70. });
  71. //自定义表单字段
  72. const formSchemas: FormSchema[] = [
  73. {
  74. //标题名称
  75. label: '上级分类(parent)',
  76. //字段
  77. field: 'parentId',
  78. //组件 支持组件详见 components/Form/src/types/index.ts 中的 ComponentType
  79. component: 'JSearchSelect',
  80. componentProps: {
  81. dict: 'base_product_class,name,id,del_flag=0',
  82. // options: classOption,
  83. stringToNumber: true,
  84. },
  85. },
  86. {
  87. label: '编码(code)',
  88. field: 'code',
  89. component: 'Input',
  90. dynamicDisabled: true,
  91. // required: true,
  92. componentProps: {
  93. AutoComplete: 'off',
  94. placeholder: '自动生成',
  95. },
  96. },
  97. {
  98. label: '名称(name)',
  99. field: 'name',
  100. component: 'Input',
  101. required: true,
  102. componentProps: ({ formModel }) => ({
  103. AutoComplete: 'off',
  104. value: formModel.name,
  105. onInput: (e: Event) => {
  106. const input = e.target as HTMLInputElement;
  107. formModel.name = input.value.toUpperCase();
  108. },
  109. }),
  110. },
  111. {
  112. label: '毛利率(gross margin)',
  113. field: 'grossMargin',
  114. component: 'Input',
  115. componentProps: {
  116. AutoComplete: 'off',
  117. addonAfter: '%',
  118. },
  119. },
  120. {
  121. label: '税率(tax rate)',
  122. field: 'taxRate',
  123. component: 'Input',
  124. componentProps: {
  125. AutoComplete: 'off',
  126. addonAfter: '%',
  127. },
  128. },
  129. {
  130. label: '序号(sort number)',
  131. field: 'sortNumber',
  132. component: 'Input',
  133. // dynamicDisabled:true,
  134. componentProps: {
  135. AutoComplete: 'off',
  136. },
  137. },
  138. {
  139. label: '',
  140. field: 'id',
  141. component: 'Input',
  142. show: false,
  143. },
  144. ];
  145. /**
  146. * BasicForm绑定注册;
  147. * useForm 是整个框架的核心用于表单渲染,里边封装了很多公共方法;
  148. * 支持(schemas: 渲染表单列,autoSubmitOnEnter:回车提交,submitButtonOptions:自定义按钮文本和图标等方法);
  149. * 平台通过此封装,简化了代码,支持自定义扩展;
  150. */
  151. const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  152. //注册表单列
  153. schemas: formSchemas,
  154. //回车提交
  155. autoSubmitOnEnter: true,
  156. //不显示重置按钮
  157. showResetButton: false,
  158. showSubmitButton: false,
  159. labelCol: { style: { width: '150px' } },
  160. labelAlign: 'right',
  161. //查询列占比 24代表一行 取值范围 0-24
  162. });
  163. /**
  164. * 点击提交按钮的value值
  165. * @param values
  166. */
  167. //表单提交事件
  168. async function handleSubmit() {
  169. try {
  170. let values = await validate();
  171. setModalProps({ confirmLoading: true });
  172. var obj = await list({ pageSize: -1, name: values.name });
  173. if (!unref(isUpdate)) {
  174. if (obj.records.length == undefined || obj.records.length == 0) {
  175. handleOkNext(values, isUpdate.value);
  176. } else {
  177. createConfirm({
  178. iconType: 'warning',
  179. title: '请确认',
  180. content: '名称重复,是否继续执行',
  181. okText: '确认',
  182. cancelText: '取消',
  183. onOk: async () => {
  184. handleOkNext(values, isUpdate.value);
  185. },
  186. });
  187. }
  188. } else {
  189. handleOkNext(values, isUpdate.value);
  190. }
  191. } finally {
  192. setModalProps({ confirmLoading: false });
  193. }
  194. }
  195. async function handleOkNext(values, isUpdate) {
  196. await saveOrUpdateDict(values, isUpdate);
  197. //关闭弹窗
  198. closeModal();
  199. //刷新列表
  200. emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
  201. }
  202. </script>