address.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view style="padding: 20rpx;">
  3. <view class="box" style="margin-bottom: 10rpx;">
  4. <uni-forms :modelValue="form" :rules="rules" ref="formRef" label-width="160rpx" label-align="right" validateTrigger="blur">
  5. <uni-forms-item label="地址" name="address" required>
  6. <uni-easyinput type="text" v-model="form.address" placeholder="请输入地址" />
  7. </uni-forms-item>
  8. <uni-forms-item label="门牌号" name="doorNo" required>
  9. <uni-easyinput type="text" v-model="form.doorNo" placeholder="请输入门牌号" />
  10. </uni-forms-item>
  11. <uni-forms-item label="联系人" name="userName" required>
  12. <uni-easyinput type="text" v-model="form.userName" placeholder="请输入联系人" />
  13. </uni-forms-item>
  14. <uni-forms-item label="联系电话" name="phone" required>
  15. <uni-easyinput type="text" v-model="form.phone" placeholder="请输入联系电话" />
  16. </uni-forms-item>
  17. <view>
  18. <button type="primary" class="my-button-primary" @click="save">保存并使用</button>
  19. </view>
  20. </uni-forms>
  21. </view>
  22. <view class="box" v-if="addressList.length">
  23. <view v-for="item in addressList" :key="item.id">
  24. <view style="padding: 10rpx 0; border-bottom: 1px solid #eee;" @click="selectAddress(item)">
  25. <view style="font-weight: bold; font-size: 32rpx; margin-bottom: 10rpx;">{{ item.address + item.doorNo }}</view>
  26. <view style="color: #888; margin-bottom: 10rpx;">
  27. <text style="margin-right: 20rpx;">{{ item.userName }}</text>
  28. <text>{{ item.phone }}</text>
  29. </view>
  30. <view style="text-align: right;">
  31. <uni-icons type="compose" size="18" color="#888" style="margin-right: 10rpx;" @click.native.stop="handleEdit(item)"></uni-icons>
  32. <uni-icons type="trash" size="18" color="#888" @click.native.stop="del(item.id)"></uni-icons>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. user: uni.getStorageSync('xm-user'),
  44. addressList: [],
  45. form: {},
  46. rules: {
  47. address: {
  48. rules: [{
  49. required: true,
  50. errorMessage: '请填写地址',
  51. }]
  52. },
  53. doorNo: {
  54. rules: [{
  55. required: true,
  56. errorMessage: '请填写门牌号',
  57. }]
  58. },
  59. userName: {
  60. rules: [{
  61. required: true,
  62. errorMessage: '请填写联系人',
  63. }]
  64. },
  65. phone: {
  66. rules: [{
  67. required: true,
  68. errorMessage: '请填写联系电话',
  69. }]
  70. }
  71. },
  72. addressType: ''
  73. }
  74. },
  75. onLoad(option) {
  76. this.addressType = option.addressType // 地址类型
  77. this.load()
  78. },
  79. methods: {
  80. selectAddress(address) {
  81. let orderStore = uni.getStorageSync('orderStore') || {} // 先获取缓存的数据
  82. if (this.addressType === '取货') {
  83. orderStore.pickAddress = address
  84. } else {
  85. orderStore.recieveAddress = address
  86. }
  87. uni.setStorageSync('orderStore', orderStore) // 再设置缓存的地址信息
  88. uni.redirectTo({
  89. url: '/pages/preOrder/preOrder'
  90. })
  91. },
  92. del(id) {
  93. this.$request.del('/address/delete/' + id).then(res => {
  94. if (res.code === '200') {
  95. uni.showToast({
  96. icon: 'success',
  97. title: '操作成功'
  98. })
  99. this.load()
  100. } else {
  101. uni.showToast({
  102. icon: 'none',
  103. title: res.msg
  104. })
  105. }
  106. })
  107. },
  108. handleEdit(address) {
  109. this.form = JSON.parse(JSON.stringify(address))
  110. },
  111. save() {
  112. this.$refs.formRef.validate().then(res => {
  113. this.form.userId = this.user.id
  114. this.$request.post('/address/add', this.form).then(res => {
  115. if (res.code === '200') {
  116. uni.showToast({
  117. icon: 'success',
  118. title: '操作成功'
  119. })
  120. if(this.addressType){
  121. this.selectAddress(res.data) // 设置地址信息到缓存
  122. }
  123. this.form = {}
  124. this.load()
  125. } else {
  126. uni.showToast({
  127. icon: 'none',
  128. title: res.msg
  129. })
  130. }
  131. })
  132. }).catch(error => {
  133. console.error(error)
  134. })
  135. },
  136. load() {
  137. this.$request.get('/address/selectAll', { userId: this.user.id }).then(res => {
  138. this.addressList = res.data || []
  139. })
  140. }
  141. }
  142. }
  143. </script>
  144. <style>
  145. </style>