AddForm.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <Dialog
  3. style="max-width: 100vw; min-width: 40vw;max-height: 70vh;min-height: 45vh"
  4. v-model="dialogVisible"
  5. :title="title"
  6. class="OutwardRegistrationBak"
  7. noPaddingEL="OutwardRegistrationBak"
  8. @close="handleClosed"
  9. >
  10. <el-form ref="formRef" :model="dataForm" :rules="dataRule" :label-width="labelWidth">
  11. <div class="info-wrap">
  12. <el-row align="bottom" justify="start">
  13. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  14. <el-form-item label="餐厅名称" prop="restaurantName">
  15. <el-input
  16. v-model="dataForm.restaurantName"
  17. placeholder="餐厅名称"
  18. show-word-limit
  19. :maxlength="25"
  20. @input="handleFormActivity"
  21. />
  22. </el-form-item>
  23. </el-col>
  24. </el-row>
  25. <el-row align="bottom" justify="start" style="margin-top: 8px">
  26. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  27. <el-form-item label="餐厅描述" prop="restaurantDescription">
  28. <el-input
  29. v-model="dataForm.restaurantDescription"
  30. type="textarea"
  31. placeholder="餐厅描述"
  32. show-word-limit
  33. :maxlength="200"
  34. :rows="4"
  35. @input="handleFormActivity"
  36. />
  37. </el-form-item>
  38. </el-col>
  39. </el-row>
  40. <el-row align="bottom" justify="start" style="margin-top: 8px">
  41. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  42. <el-form-item label="餐厅图片" prop="elderName">
  43. <SelectUpload v-model="dataForm.pictures" fun-name="餐厅图片" />
  44. </el-form-item>
  45. </el-col>
  46. </el-row>
  47. </div>
  48. </el-form>
  49. <template #footer>
  50. <el-button @click="handleClosed">关闭</el-button>
  51. <el-button v-loading="formLoading" type="primary" @click="submitForm">确定</el-button>
  52. </template>
  53. </Dialog>
  54. </template>
  55. <script lang="ts" setup>
  56. import { computed, ref } from 'vue'
  57. import { FormRules } from 'element-plus'
  58. import { useMediaQuery } from '@vueuse/core'
  59. import { useUpload } from '@/components/UploadFile/src/useUpload'
  60. import { restaurantManagementAdd, restaurantManagementEdit } from '@/api/system/foods'
  61. // const { uploadUrl, httpRequest } = useUpload()
  62. const message = useMessage() // 消息弹窗
  63. const { t } = useI18n() // 国际化
  64. const dialogVisible = ref(false) // 弹窗
  65. const title = ref('') // 表单 Ref
  66. const formRef = ref() // 表单 Ref
  67. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  68. let dataForm = ref({
  69. // 表单字段
  70. id: undefined,
  71. restaurantName: '',
  72. restaurantDescription: '',
  73. pictures: [] as any[]
  74. })
  75. // 表单规则
  76. const dataRule = reactive<FormRules>({
  77. restaurantName: [{ required: true, message: '餐厅名称不能为空', trigger: 'blur' }],
  78. restaurantDescription: [{ required: true, message: '餐厅描述不能为空', trigger: 'blur' }]
  79. })
  80. // 计算窗口大小
  81. const currentWidth = useMediaQuery('(max-width: 800px)')
  82. // 计算文字大小
  83. const labelWidth = computed(() => {
  84. return currentWidth.value ? '110px' : '120px'
  85. })
  86. /** 提交表单 */
  87. const emit = defineEmits(['success', 'visible-change', 'form-activity']) // 定义 success 事件,用于操作成功后的回调
  88. /** 打开弹窗 */
  89. const open = async (id?: any) => {
  90. resetForm()
  91. dialogVisible.value = true
  92. emit('visible-change', true)
  93. if (id !== undefined) {
  94. try {
  95. dataForm.value = JSON.parse(JSON.stringify(id))
  96. if (dataForm.value.pictures && dataForm.value.pictures.length > 0) {
  97. for (const idElement of dataForm.value.pictures) {
  98. idElement.fileUrl = idElement.url ? idElement.url : idElement.fileUrl
  99. }
  100. }
  101. } catch (error) {}
  102. title.value = '编辑餐厅'
  103. } else {
  104. title.value = '新增餐厅'
  105. }
  106. }
  107. const handleFormActivity = () => {
  108. emit('form-activity')
  109. }
  110. const applyAiSuggest = (suggest: { restaurantName?: string; restaurantDescription?: string }) => {
  111. if (!dataForm.value.restaurantName && suggest?.restaurantName) {
  112. dataForm.value.restaurantName = suggest.restaurantName as string
  113. }
  114. if (!dataForm.value.restaurantDescription && suggest?.restaurantDescription) {
  115. dataForm.value.restaurantDescription = suggest.restaurantDescription as string
  116. }
  117. emit('form-activity')
  118. }
  119. const getFormData = () => {
  120. return {
  121. restaurantName: dataForm.value.restaurantName,
  122. restaurantDescription: dataForm.value.restaurantDescription
  123. }
  124. }
  125. defineExpose({ open, applyAiSuggest, getFormData }) // 提供 open 方法,用于打开弹窗
  126. const submitForm = async () => {
  127. if (formLoading.value) {
  128. return
  129. }
  130. // 提交请求
  131. formLoading.value = true
  132. try {
  133. // 校验表单
  134. if (!formRef.value) return
  135. const valid = await formRef.value.validate()
  136. if (!valid) return
  137. let dataFormUP = {
  138. ...dataForm.value,
  139. pictures: JSON.stringify(dataForm.value.pictures) //改成字符串
  140. }
  141. if (dataForm.value.id) {
  142. const res = await restaurantManagementEdit(dataFormUP)
  143. if (res) {
  144. message.success(t('common.updateSuccess'))
  145. dialogVisible.value = false
  146. emit('visible-change', false)
  147. emit('success')
  148. }
  149. } else {
  150. const res = await restaurantManagementAdd(dataFormUP)
  151. if (res) {
  152. message.success(t('common.updateSuccess'))
  153. dialogVisible.value = false
  154. emit('visible-change', false)
  155. emit('success')
  156. }
  157. }
  158. } finally {
  159. setTimeout(() => {
  160. formLoading.value = false
  161. }, 500)
  162. }
  163. }
  164. /** 重置表单 */
  165. const resetForm = () => {
  166. dataForm.value = {
  167. id: undefined,
  168. restaurantName: '',
  169. restaurantDescription: '',
  170. pictures: []
  171. }
  172. formRef.value?.resetFields()
  173. }
  174. // 关闭表单
  175. const handleClosed = () => {
  176. dialogVisible.value = false
  177. emit('visible-change', false)
  178. resetForm()
  179. }
  180. </script>
  181. <style lang="scss" scoped>
  182. .OutwardRegistrationBak {
  183. .el-form {
  184. padding: 15px !important;
  185. }
  186. .info-title {
  187. min-width: 75px;
  188. max-width: 180px;
  189. padding: 15px;
  190. font-weight: bolder;
  191. background-color: #f3f6fb;
  192. border-top-right-radius: 50px;
  193. border-bottom-right-radius: 50px;
  194. }
  195. .info-wrap {
  196. margin: 15px 2vw 15px 15px;
  197. }
  198. @media (max-width: 1200) {
  199. :deep(.el-form-item--default .el-form-item__label) {
  200. line-height: 0 !important;
  201. }
  202. }
  203. }
  204. </style>