editAddress.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view class="content">
  3. <u-form :model="form" ref="uForm" :label-width="140" :error-type="['toast']">
  4. <u-form-item label="姓名" required prop="receiverName">
  5. <u-input placeholder="请输入姓名" :maxlength="30" v-model="form.receiverName" />
  6. </u-form-item>
  7. <u-form-item label="电话" required prop="receiverPhone">
  8. <u-input placeholder="请输入电话" :maxlength="11" type="number" v-model="form.receiverPhone" />
  9. </u-form-item>
  10. <u-form-item label="省市区" required prop="receiveAreasName">
  11. <u-input v-model="form.receiveAreasName" placeholder="请选择省市区" @click="show = true" type="select" />
  12. <u-picker v-model="show" @confirm="chooseArea" mode="region"></u-picker>
  13. </u-form-item>
  14. <u-form-item label="详细地址" required prop="receiveAddress">
  15. <u-input type="textarea" placeholder="请输入收货详细地址(最多500个字符)" :maxlength="500" v-model="form.receiveAddress" />
  16. </u-form-item>
  17. </u-form>
  18. <view class="buttons">
  19. <u-button type="primary" @click="submit">提交</u-button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. saveAddress
  26. } from '@/api/receiveAddress.js'
  27. export default {
  28. data() {
  29. return {
  30. show: false,
  31. form: {
  32. receiverName: '',
  33. receiverPhone: '',
  34. receiveAreasName: '', // 地址名称
  35. receiveAddress: '' // 详细地址
  36. },
  37. receiveAreas: '', //地址编码
  38. areaName: '', //地址
  39. rules: {
  40. }
  41. };
  42. },
  43. onLoad(option) {
  44. this.form = this.$store.state.vuex_OrderAddress
  45. if (this.form.receiveAreasName) {
  46. this.receiveAreas = this.form.receiveAreas
  47. this.areaName = this.form.receiveAreasName
  48. this.form.receiveAreasName = this.form.receiveAreasName.replace(/\,/g,'-')
  49. uni.setNavigationBarTitle({
  50. title: '修改地址'
  51. })
  52. } else {
  53. uni.setNavigationBarTitle({
  54. title: '新增地址'
  55. })
  56. }
  57. },
  58. onUnload() {
  59. this.$u.vuex("vuex_OrderAddress",{})
  60. },
  61. methods: {
  62. submit() {
  63. this.$refs.uForm.validate(valid => {
  64. if (valid) {
  65. console.log('验证通过');
  66. if (this.form.receiverName ==='') {
  67. uni.showToast({
  68. title: '请输入姓名',
  69. icon: 'none'
  70. })
  71. return false
  72. }
  73. if (this.form.receiverPhone ==='') {
  74. uni.showToast({
  75. title: '请输入电话',
  76. icon: 'none'
  77. })
  78. return false
  79. }
  80. if (this.form.receiveAreasName ==='') {
  81. uni.showToast({
  82. title: '请选择省市区',
  83. icon: 'none'
  84. })
  85. return false
  86. }
  87. if (this.form.receiveAddress ==='') {
  88. uni.showToast({
  89. title: '请输入详细地址',
  90. icon: 'none'
  91. })
  92. return false
  93. }
  94. let params = {
  95. receiverName: this.form.receiverName,
  96. receiverPhone: this.form.receiverPhone,
  97. receiveAreasName: this.areaName, // 地址名称
  98. receiveAreas: this.receiveAreas,
  99. receiveAddress: this.form.receiveAddress // 详细地址
  100. }
  101. if(this.form.id) {
  102. params.id = this.form.id
  103. }
  104. saveAddress(params).then(res=>{
  105. if(res.status == 200) {
  106. uni.showToast({
  107. title: res.message,
  108. icon: 'none'
  109. })
  110. setTimeout(()=>{
  111. uni.navigateBack()
  112. },300)
  113. } else {
  114. uni.showToast({
  115. title: res.message,
  116. icon: 'none'
  117. })
  118. }
  119. })
  120. } else {
  121. console.log('验证失败');
  122. }
  123. });
  124. },
  125. // 选择地址
  126. chooseArea(e){
  127. console.log(e)
  128. this.form.receiveAreasName = e.province.label + '-' + e.city.label + '-' + e.area.label
  129. this.areaName = e.province.label + ',' + e.city.label + ',' + e.area.label
  130. this.receiveAreas = e.province.value + ',' + e.city.value + ',' + e.area.value
  131. }
  132. },
  133. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  134. onReady() {
  135. this.$refs.uForm.setRules(this.rules);
  136. }
  137. };
  138. </script>
  139. <style lang="scss">
  140. .content{
  141. padding: 0 50upx;
  142. background: #FFFFFF;
  143. width: 100%;
  144. .buttons{
  145. padding: 80upx 100upx;
  146. }
  147. }
  148. </style>