Address.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <view>
  3. <add-component v-for="(item, index) in addInfoArr" :key="index"
  4. :ref="el => { if (el) addComponentRef[index] = el }"></add-component>
  5. <view class="Wrapper-Btn">
  6. <up-button @click="handleQux" type="error" :plain="true" :hairline="true" text="取消"
  7. :customStyle="hadlClickEdit"></up-button>
  8. <up-button type="error" text="确定" @click="handlOk" :customStyle="hadlClickEdit"></up-button>
  9. </view>
  10. </view>
  11. </template>
  12. <script setup>
  13. import { ref, reactive } from 'vue'
  14. import { useraDdressData } from '@/api/userSettings.js'
  15. import pickerAddress from '@/pages_home/components/pickerAddress/pickerAddress.vue' // 地区选择器组件
  16. import addComponent from '@/pages_home/components/setupUserCopy/Add.vue'
  17. const addComponentRef = ref([])
  18. const addInfoArr = ref([{}])
  19. function handleAdd() {
  20. if (addInfoArr.value.length < 4) addInfoArr.value.push({})
  21. }
  22. const handlOk = async () => {
  23. // 验证所有表单是否填写完整
  24. let isValid = true
  25. // 遍历所有地址组件,验证每个表单
  26. for (let i = 0; i < addComponentRef.value.length; i++) {
  27. const formRef = addComponentRef.value[i].$refs.formRef
  28. if (formRef) {
  29. try {
  30. // 触发表单验证,返回验证结果
  31. await formRef.validate().catch(error => {
  32. console.error('验证错误:', error)
  33. isValid = false
  34. })
  35. } catch (error) {
  36. console.error('验证出错:', error)
  37. isValid = false
  38. }
  39. }
  40. }
  41. // 只有全部验证通过才调用接口
  42. if (isValid) {
  43. const params = []
  44. addComponentRef.value.forEach((item) => {
  45. params.push(item.modelForm)
  46. })
  47. // 提交数据
  48. let success = true
  49. for (let i = 0; i < params.length; i++) {
  50. try {
  51. const res = await useraDdressData(params[i])
  52. if (res.code != 200) {
  53. success = false
  54. }
  55. } catch (error) {
  56. console.error('提交数据失败:', error)
  57. success = false
  58. }
  59. }
  60. if (success) {
  61. uni.showToast({
  62. title: '新增成功',
  63. icon: 'success',
  64. duration: 1500,
  65. mask: true,
  66. })
  67. // 成功后延迟返回上一页
  68. setTimeout(() => {
  69. uni.navigateBack({
  70. delta: 1
  71. })
  72. }, 1500)
  73. } else {
  74. uni.showToast({
  75. title: '新增失败',
  76. icon: 'none'
  77. })
  78. }
  79. } else {
  80. uni.showToast({
  81. title: '请填写完整信息',
  82. icon: 'none'
  83. })
  84. }
  85. }
  86. const handleQux = () => {
  87. uni.navigateBack({
  88. delta: 2,
  89. })
  90. }
  91. const hadlClickEdit = {
  92. width: '240rpx',
  93. marginTop: '30rpx',
  94. }
  95. </script>
  96. <style scoped lang="scss">
  97. .address-inp {
  98. color: #929292;
  99. }
  100. .Wrapper-Btn {
  101. display: flex;
  102. justify-content: center;
  103. align-items: center;
  104. }
  105. </style>