OrderUpdatePriceForm.vue 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <Dialog v-model="dialogVisible" title="订单调价" width="25%">
  3. <el-form ref="formRef" v-loading="formLoading" :model="formData" label-width="100px">
  4. <el-form-item label="应付金额(总)">
  5. <el-input v-model="formData.payPrice" disabled />
  6. </el-form-item>
  7. <el-form-item label="订单调价">
  8. <el-input-number v-model="formData.adjustPrice" :precision="2" :step="0.1" class="w-100%" />
  9. <el-tag class="ml-10px" type="warning">订单调价。 正数,加价;负数,减价</el-tag>
  10. </el-form-item>
  11. <el-form-item label="调价后">
  12. <el-input v-model="formData.newPayPrice" disabled />
  13. </el-form-item>
  14. </el-form>
  15. <template #footer>
  16. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  17. <el-button @click="dialogVisible = false">取 消</el-button>
  18. </template>
  19. </Dialog>
  20. </template>
  21. <script lang="ts" setup>
  22. import * as TradeOrderApi from '@/api/mall/trade/order'
  23. import { convertToInteger, floatToFixed2, formatToFraction } from '@/utils'
  24. import { cloneDeep } from 'lodash-es'
  25. defineOptions({ name: 'OrderUpdatePriceForm' })
  26. const { t } = useI18n() // 国际化
  27. const message = useMessage() // 消息弹窗
  28. const dialogVisible = ref(false) // 弹窗的是否展示
  29. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  30. const formData = ref({
  31. id: undefined, // 订单编号
  32. adjustPrice: 0, // 订单调价
  33. payPrice: '', // 应付金额(总)
  34. newPayPrice: '' // 调价后应付金额(总)
  35. })
  36. watch(
  37. () => formData.value.adjustPrice,
  38. (adjustPrice: number | string) => {
  39. const numMatch = formData.value.payPrice.match(/\d+(\.\d+)?/)
  40. if (numMatch) {
  41. const payPriceNum = parseFloat(numMatch[0])
  42. adjustPrice = typeof adjustPrice === 'string' ? parseFloat(adjustPrice) : adjustPrice
  43. formData.value.newPayPrice = (payPriceNum + adjustPrice).toFixed(2) + '元'
  44. }
  45. }
  46. )
  47. const formRef = ref() // 表单 Ref
  48. /** 打开弹窗 */
  49. const open = async (row: TradeOrderApi.OrderVO) => {
  50. resetForm()
  51. formData.value.id = row.id!
  52. // 设置数据
  53. formData.value.adjustPrice = formatToFraction(row.adjustPrice!)
  54. formData.value.payPrice = floatToFixed2(row.payPrice!) + '元'
  55. formData.value.newPayPrice = formData.value.payPrice
  56. dialogVisible.value = true
  57. }
  58. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  59. /** 提交表单 */
  60. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  61. const submitForm = async () => {
  62. // 提交请求
  63. formLoading.value = true
  64. try {
  65. const data = cloneDeep(unref(formData))
  66. data.adjustPrice = convertToInteger(data.adjustPrice)
  67. delete data.payPrice
  68. delete data.newPayPrice
  69. await TradeOrderApi.updateOrderPrice(data)
  70. message.success(t('common.updateSuccess'))
  71. dialogVisible.value = false
  72. // 发送操作成功的事件
  73. emit('success', true)
  74. } finally {
  75. formLoading.value = false
  76. }
  77. }
  78. /** 重置表单 */
  79. const resetForm = () => {
  80. formData.value = {
  81. id: undefined, // 订单编号
  82. adjustPrice: 0, // 订单调价
  83. payPrice: '', // 应付金额(总)
  84. newPayPrice: '' // 调价后应付金额(总)
  85. }
  86. formRef.value?.resetFields()
  87. }
  88. </script>