Write.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <script setup lang="tsx">
  2. import { watch, ref, onMounted } from 'vue'
  3. import { productUnit } from '@/api/goods/types'
  4. import {
  5. getProductUnitDetail,
  6. addProductUnit,
  7. putProductUnit,
  8. getUnitList
  9. } from '@/api/goods'
  10. import {
  11. ElForm,
  12. ElFormItem,
  13. ElInput,
  14. ElMessage,
  15. ElRadioGroup,
  16. ElRadio,
  17. ElSelect,
  18. ElOption
  19. } from 'element-plus'
  20. import type { FormInstance } from 'element-plus'
  21. import { Dialog } from '@/components/Dialog'
  22. import { useI18n } from '@/hooks/web/useI18n'
  23. const { t } = useI18n()
  24. const props = defineProps({
  25. id: {
  26. type: Number,
  27. default: 0
  28. },
  29. title: {
  30. type: String,
  31. default: ''
  32. }
  33. })
  34. const modelValue = defineModel<boolean>()
  35. const ruleFormRef = ref<FormInstance>()
  36. const emit = defineEmits(['submit'])
  37. const submit = async () => {
  38. if (ruleFormRef.value) {
  39. const valid = await ruleFormRef.value.validate((valid, fields) => {
  40. // console.log(fields, 'fields')
  41. if (!valid) {
  42. console.log('error submit!', fields)
  43. }
  44. })
  45. if (valid) {
  46. loadingData.value = true
  47. try {
  48. let re: any = {}
  49. if (form.value.id > 0) {
  50. re = await putProductUnit(form.value)
  51. } else if (form.value.id == 0) {
  52. re = await addProductUnit(form.value)
  53. }
  54. if (re.status == 200) {
  55. ElMessage({
  56. showClose: true,
  57. message: '添加成功',
  58. type: 'success'
  59. })
  60. }
  61. getUnitLists()
  62. form.value = {
  63. name: '',
  64. id: 0,
  65. type: 'area',
  66. pid: 0,
  67. sort: 0,
  68. is_show: 1,
  69. path: '',
  70. is_weigh: 0
  71. }
  72. emit('submit')
  73. } catch (error) {
  74. console.log(error)
  75. } finally {
  76. loadingData.value = false
  77. }
  78. }
  79. }
  80. }
  81. const loadingData = ref(false)
  82. watch(
  83. () => props.id,
  84. async (value) => {
  85. console.log('zhixin');
  86. if (value * 1 === 0) return
  87. loadingData.value = true
  88. const res = await getProductUnitDetail(value)
  89. form.value = res.data
  90. loadingData.value = false
  91. },
  92. {
  93. deep: true,
  94. immediate: true
  95. }
  96. )
  97. const form = ref<productUnit>({
  98. name: '',
  99. id: 0,
  100. type: 'area',
  101. pid: 0,
  102. sort: 0,
  103. is_show: 1,
  104. path: '',
  105. is_weigh: 0
  106. })
  107. const options = ref<any[]>([])
  108. const getUnitLists = async () => {
  109. // getUnitList()
  110. let res = await getUnitList({
  111. page: 1,
  112. limit: 100,
  113. type: 'area',
  114. is_show: 1,
  115. pid: 0
  116. })
  117. options.value = [{ value: 0, label: '顶级' }].concat(getOp(res.data.list))
  118. }
  119. const getOp = (arr: any[]) => {
  120. let arr1 = arr.map((item) => {
  121. let obj = {
  122. value: item.id,
  123. label: item.name
  124. }
  125. return obj
  126. })
  127. return arr1
  128. }
  129. onMounted(() => {
  130. getUnitLists()
  131. })
  132. </script>
  133. <template>
  134. <Dialog v-model="modelValue" :title="title" width="600px">
  135. <el-form v-loading="loadingData" ref="ruleFormRef" :model="form" label-width="auto">
  136. <el-form-item
  137. prop="name"
  138. label="区域名称"
  139. :rules="[{ required: true, message: '区域名称', trigger: 'blur' }]"
  140. >
  141. <el-input v-model="form.name" />
  142. </el-form-item>
  143. <!-- <el-cascader :options="options" :props="props1" clearable /> -->
  144. <el-form-item prop="pid" label="上级区域">
  145. <el-select v-model="form.pid" class="m-2" placeholder="Select" size="large">
  146. <el-option
  147. v-for="item in options"
  148. :key="item.value"
  149. :label="item.label"
  150. :value="item.value"
  151. />
  152. </el-select>
  153. </el-form-item>
  154. <el-form-item prop="sort" label="排序">
  155. <el-input v-model="form.sort" />
  156. </el-form-item>
  157. <el-form-item prop="is_show" label="是否显示">
  158. <el-radio-group v-model="form.is_show" class="ml-4">
  159. <el-radio :label="1" size="large">是</el-radio>
  160. <el-radio :label="0" size="large">否</el-radio>
  161. </el-radio-group>
  162. </el-form-item>
  163. </el-form>
  164. <template #footer>
  165. <BaseButton type="primary" :loading="loadingData" @click="submit">
  166. {{ t('exampleDemo.save') }}
  167. </BaseButton>
  168. <BaseButton @click="modelValue = false">{{ t('dialogDemo.close') }}</BaseButton>
  169. </template>
  170. </Dialog>
  171. </template>