AddressModal.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <a-modal
  3. v-model:open="visible"
  4. :title="t('information.editAddressLinkText')"
  5. :width="600"
  6. :ok-text="t('common.comfirm')"
  7. :cancel-text="t('common.cancel')"
  8. @ok="handleSubmit"
  9. >
  10. <a-form
  11. ref="addrRef"
  12. :model="form"
  13. :label-col="{ style: { width: '120px' } }"
  14. :wrapper-col="{ span: 18 }"
  15. >
  16. <a-form-item :label="t('information.editAddress.country')" name="country">
  17. <a-select size="large" v-model:value="form.country">
  18. <a-select-option value="CN">{{ t('common.china') }}</a-select-option>
  19. </a-select>
  20. </a-form-item>
  21. <a-form-item
  22. :label="t('information.editAddress.area')"
  23. name="area"
  24. :rules="[{ required: true, message: t('information.editAddress.selectAreaRequired') }]"
  25. >
  26. <a-cascader
  27. size="large"
  28. v-model:value="form.area"
  29. :options="regionData"
  30. :placeholder="t('information.editAddress.areaPlaceholder')"
  31. style="width:100%"
  32. />
  33. </a-form-item>
  34. <a-form-item
  35. :label="t('information.editAddress.addressDetail')"
  36. name="shipAddress"
  37. :rules="formRules.shipAddress"
  38. >
  39. <a-input
  40. size="large"
  41. v-model:value="form.shipAddress"
  42. show-count
  43. maxlength="50"
  44. :placeholder="t('information.editAddress.addressRequired')"
  45. />
  46. </a-form-item>
  47. <a-form-item
  48. :label="t('information.editAddress.receiver')"
  49. name="shipName"
  50. :rules="formRules.shipName"
  51. >
  52. <a-input
  53. size="large"
  54. v-model:value="form.shipName"
  55. show-count
  56. maxlength="10"
  57. :placeholder="t('information.editAddress.namePlaceholder')"
  58. />
  59. </a-form-item>
  60. <a-form-item
  61. :label="t('information.editAddress.receiverPhone')"
  62. name="shipMobile"
  63. :rules="formRules.shipMobile"
  64. >
  65. <a-input
  66. size="large"
  67. v-model:value="form.shipMobile"
  68. show-count
  69. maxlength="11"
  70. :placeholder="t('login.phonePlaceholder')"
  71. />
  72. </a-form-item>
  73. </a-form>
  74. </a-modal>
  75. </template>
  76. <script setup>
  77. import { ref, watch, computed } from 'vue'
  78. import { useI18n } from 'vue-i18n'
  79. import { useCartStore, useUserStore } from "@/stores";
  80. import { regionData } from 'element-china-area-data'
  81. import { updateAddress } from "@/api/login/index.js";
  82. const { t } = useI18n();
  83. const cart = useCartStore();
  84. const receiverInfo = computed(() => cart.greceiverInfo);
  85. const formRules = computed(() => ({
  86. shipAddress: [
  87. { required: true, message: t('information.editAddress.addressRequired'), trigger: 'blur' },
  88. { max: 50, message: t('information.editAddress.maxChars50'), trigger: 'blur' },
  89. ],
  90. shipName: [
  91. { required: true, message: t('information.editAddress.receiverRequired'), trigger: 'blur' },
  92. { max: 10, message: t('information.editAddress.maxChars10'), trigger: 'blur' },
  93. ],
  94. shipMobile: [
  95. { required: true, message: t('information.editAddress.phoneRequired'), trigger: 'blur' },
  96. { len: 11, message: t('information.editAddress.phoneLen11'), trigger: 'blur' },
  97. ],
  98. }));
  99. const props = defineProps({
  100. open: {
  101. type: Boolean,
  102. default: false
  103. }
  104. })
  105. const emit = defineEmits(['update:open', 'confirm'])
  106. const addrRef = ref(null)
  107. const form = ref(receiverInfo.value || {})
  108. watch(
  109. () => receiverInfo.value,
  110. (val) => {
  111. form.value = { ...val, area: val?.shipAreaPath?.split(',') || [] }
  112. console.log('receiverInfo.value', val, receiverInfo.value);
  113. },
  114. { deep: true, immediate: true }
  115. )
  116. watch(
  117. () => props.open,
  118. (val) => {
  119. if (!val) {
  120. addrRef.value?.clearValidate()
  121. }
  122. }
  123. )
  124. const visible = computed({
  125. get() {
  126. return props.open
  127. },
  128. set(val) {
  129. emit('update:open', val)
  130. }
  131. })
  132. const handleSubmit = async () => {
  133. await addrRef.value.validate()
  134. await updateAddress({
  135. ...form.value,
  136. city: form.value?.area?.[1],
  137. province: form.value?.area?.[0],
  138. shipAreaPath: form.value?.area?.join(','),
  139. });
  140. console.log('form', form);
  141. cart.getReceiver();
  142. emit('confirm', { ...form.value })
  143. }
  144. </script>