index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <app-modal class="main-address-edit" direction="right" height="100%" v-model:show="showModal">
  3. <app-view class="g-form" style="">
  4. <template #header>
  5. <app-navbar title="新增收货地址" />
  6. </template>
  7. <Form ref="formRef" class="g-form__container" @submit="formSubmit">
  8. <CellGroup inset>
  9. <Field v-model="formData.ReceiverName" name="ReceiverName" label="收货人" placeholder="必填"
  10. :rules="formRules.ReceiverName" />
  11. <Field v-model="formData.PhoneNum" type="tel" name="PhoneNum" label="联系电话" placeholder="必填"
  12. :rules="formRules.PhoneNum" />
  13. <Field label="收货地区" placeholder="请选择" is-link>
  14. <template #input>
  15. <app-region v-model="formData.DistrictID" />
  16. </template>
  17. </Field>
  18. <Field v-model="formData.Address" name="refernum" label="详细地址" placeholder="详细地址" />
  19. </CellGroup>
  20. </Form>
  21. <template #footer>
  22. <div class="g-form__footer">
  23. <Button type="primary" @click="formRef?.submit" round block>确定</Button>
  24. </div>
  25. </template>
  26. </app-view>
  27. </app-modal>
  28. </template>
  29. <script lang="ts" setup>
  30. import { reactive, shallowRef, PropType } from 'vue'
  31. import { CellGroup, Button, Field, Form, FormInstance, Toast, FieldRule } from 'vant'
  32. import { fullloading, dialog } from '@/utils/vant'
  33. import { validateRules } from '@/constants/regex'
  34. import { useNavigation } from '@/hooks/navigation'
  35. import { useAddressForm } from '@/business/user'
  36. import AppModal from '@/components/base/modal/index.vue'
  37. import AppRegion from '@mobile/components/base/region/index.vue'
  38. const props = defineProps({
  39. selectedRow: {
  40. type: Object as PropType<Model.UserReceiveInfoRsp>,
  41. default: () => ({})
  42. }
  43. })
  44. const { router } = useNavigation()
  45. const { formData, addOrUpdate } = useAddressForm(props.selectedRow)
  46. const formRef = shallowRef<FormInstance>()
  47. const showModal = shallowRef(true)
  48. // 表单验证规则
  49. const formRules: { [key in keyof Proto.UserReceiveInfoReq]?: FieldRule[] } = {
  50. ReceiverName: [{
  51. required: true,
  52. message: '请输入收货人',
  53. }],
  54. }
  55. // 表单提交
  56. const formSubmit = () => {
  57. fullloading((hideLoading) => {
  58. addOrUpdate().then(() => {
  59. hideLoading()
  60. Toast.fail('保存成功')
  61. }).catch((err) => {
  62. Toast.fail(err)
  63. })
  64. })
  65. }
  66. // 关闭弹窗
  67. const closed = () => {
  68. showModal.value = false
  69. }
  70. // 暴露组件属性给父组件调用
  71. defineExpose({
  72. closed,
  73. })
  74. </script>