addAddress.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="add-address-wrapper">
  3. <view class="form-box mb10">
  4. <u1-form :model="form" ref="addressForm" label-width="150" :label-style="labelStyle">
  5. <u1-form-item label="姓名">
  6. <u1-input placeholder="请输入姓名" v-model="form.name" />
  7. </u1-form-item>
  8. <u1-form-item label="手机号码" prop="phone">
  9. <u1-input placeholder="请输入手机号码" v-model="form.phone" type="number" />
  10. </u1-form-item>
  11. <u1-form-item label="所在地区">
  12. <picker mode="region" :value="form.area" @change="bindAreaChange">
  13. <view v-if="!form.province" class="color-9">省市区县、乡镇等</view>
  14. <view v-else class="color-23">{{areaDisplay}}</view>
  15. </picker>
  16. </u1-form-item>
  17. <u1-form-item label="详细地址">
  18. <u1-input placeholder="街道号、楼牌号等" v-model="form.address" />
  19. </u1-form-item>
  20. </u1-form>
  21. </view>
  22. <view class="form-box display-flex-between set-default">
  23. <text>设置为默认地址</text>
  24. <u-switch v-model="form.type" :activeValue="1" :inactiveValue="0" size="20" active-color="#F39800"
  25. inactive-color="#DCDCDC"></u-switch>
  26. </view>
  27. <view class="common-btn" @click="save">保存并使用</view>
  28. <!-- <u1-button type="primary" shape="circle" @click="save">保存</u1-button> -->
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. labelStyle: {},
  36. pageOptions: {},
  37. form: { type: 0, phone: '', province: '', city: '', area: '' },
  38. rules: {
  39. phone: [{
  40. required: true,
  41. message: '请输入手机号',
  42. trigger: ['change', 'blur']
  43. },
  44. {
  45. // 自定义验证函数,见上说明
  46. validator: (rule, value, callback) => {
  47. // 上面有说,返回true表示校验通过,返回false表示不通过
  48. // this.$u.test.mobile()就是返回true或者false的
  49. return this.$u.test.mobile(value);
  50. },
  51. message: '手机号码不正确',
  52. // 触发器可以同时用blur和change
  53. trigger: ['change', 'blur']
  54. }
  55. ]
  56. }
  57. };
  58. },
  59. computed: {
  60. areaDisplay() {
  61. const { province = '', city = '', area = '' } = this.form
  62. return province + city + area
  63. },
  64. },
  65. onLoad(options) {
  66. this.form = Object.assign({}, this.form, options)
  67. this.pageOptions = options
  68. if (options.code) {
  69. this.getDetail()
  70. }
  71. },
  72. onReady() {
  73. this.$refs.addressForm.setRules(this.rules);
  74. },
  75. methods: {
  76. getDetail() {
  77. this.$api.getAddressDetail({ code: this.pageOptions.code }).then(res => {
  78. this.form = Object.assign({}, this.form, res.data)
  79. })
  80. },
  81. save() {
  82. const currentApi = this.pageOptions.code ? this.$api.updateAddress : this.$api.addAddress
  83. this.$refs.addressForm.validate().then(res => {
  84. if (res) {
  85. currentApi(this.form).then(res => {
  86. if (res.code == 200) {
  87. uni.showToast({
  88. title: '保存成功'
  89. })
  90. // uni.reLaunch({
  91. // url: '../addressMg/addressMg'
  92. // })
  93. uni.navigateBack()
  94. } else {
  95. uni.showToast({
  96. title: '添加失败'
  97. })
  98. }
  99. }).catch(() => {
  100. })
  101. }
  102. }).catch(errors => {
  103. console.log(111111)
  104. })
  105. },
  106. bindAreaChange(data) {
  107. const { code, value } = data.detail
  108. this.form.province = value[0]
  109. this.form.city = value[1]
  110. this.form.area = value[2]
  111. // this.areaDisplay = value.join('.')
  112. console.log(8999, data, this.form)
  113. }
  114. }
  115. };
  116. </script>
  117. <style lang="scss" scoped>
  118. .add-address-wrapper {
  119. padding: 30rpx 20rpx;
  120. .form-box {
  121. padding: 0 20rpx;
  122. background-color: #fff;
  123. border-radius: 15rpx;
  124. }
  125. .set-default {
  126. padding: 28rpx 20rpx;
  127. margin-bottom: 54rpx;
  128. }
  129. .no-value {
  130. color: red;
  131. }
  132. .has-value {
  133. color: green;
  134. }
  135. }
  136. </style>