address.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view class="address">
  3. <custom-nav :title="pageTitle"></custom-nav>
  4. <custom-toast ref='toast'></custom-toast>
  5. <view class="content">
  6. <view class="default">
  7. <view class="title">{{ list.length ? '当前选择的收件人信息' : '您还没有添加收货地址'}}</view>
  8. <view class="info">
  9. <view class="name-tel" v-if="list.length">
  10. <text class="name">{{ choosedAddress.name }}</text>
  11. <text class="tel">{{ choosedAddress.tel }}</text>
  12. </view>
  13. <view class="address1">{{ address1 }}</view>
  14. <view class="address2">{{ address2 }}</view>
  15. <button hover-class="hover" class="addAddress" @tap="toaddaddress">+ 新增地址</button>
  16. </view>
  17. </view>
  18. <view class="list" v-if="list.length">
  19. <view class="item" v-for="(item, index) in list" :key="item.id" @tap="tap(index)" :style="{ color: item.id === choosedAddress.id ? '#000000' : '#999999' }">
  20. <text class="icon" :class="item.id === choosedAddress.id ? 'cuIcon-roundcheckfill' : 'cuIcon-roundcheck'"></text>
  21. <view class="name-tel">
  22. <text class="name">{{ item.name }}</text>
  23. <text class="tel">{{ item.tel }}</text>
  24. </view>
  25. <view class="item-address">{{ item.address }}</view>
  26. </view>
  27. </view>
  28. <view class="set" @tap="set" v-if="list.length">{{ pageTitle === '地址选择' ? '选择地址' : '设为默认' }}</view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import { api_getAddress } from '../../api.js' //添加地址接口
  34. import { api_setDefaultAddress } from '../../api.js' //修改默认地址接口
  35. export default {
  36. data() {
  37. return {
  38. pageTitle: '', //导航栏名字
  39. list: [], //地址列表
  40. index: 0 //当前选中地址下标
  41. };
  42. },
  43. computed: {
  44. choosedAddress () { //当前选中地址对象
  45. return this.list[this.index]
  46. },
  47. address1 () {
  48. return this.choosedAddress ? this.choosedAddress.address.split('-')[0] : ''
  49. },
  50. address2 () {
  51. return this.choosedAddress ? this.choosedAddress.address.split('-')[1] : ''
  52. }
  53. },
  54. methods: {
  55. tap (index) { //切换地址
  56. this.index = index
  57. },
  58. toaddaddress () {
  59. uni.navigateTo({
  60. url: '../addressadd/addressadd'
  61. })
  62. },
  63. set () { //点击提交按钮
  64. if (this.pageTitle === '地址管理') { //如果是地址管理模式,就是为了修改默认地址
  65. if (+this.choosedAddress.status) { //检查当前地址是否是默认地址
  66. this.$refs.toast.hover('当前地址已是默认地址')
  67. } else { //如果当前地址不是默认地址
  68. this.$ajax.get(`${api_setDefaultAddress}?id=${this.choosedAddress.id}`).then(([ , res]) => { //发请求
  69. if (res.data.code === 200) {
  70. this.$refs.toast.hover('设置成功')
  71. this.list.forEach(e => e.status = 0) //先遍历所有地址对象将其状态设置为非默认地址
  72. this.list[this.index].status = 1 //将设置成功的下标对应地址对象的为默认地址
  73. } else {
  74. this.$refs.toast.hover('设置失败')
  75. }
  76. })
  77. }
  78. } else if (this.pageTitle === '地址选择') { //如果是地址选择模式,就是为了选择收货地址
  79. getApp().globalData.choosedAddress = this.list[this.index] //将选择的地址对象放在 globalData 里以用来传给商品详情页
  80. uni.$emit('MESSAGE', '地址选择成功', 2345, 'bottom') //提示地址选择成功
  81. uni.navigateBack()
  82. }
  83. },
  84. },
  85. onShow () { //在页面展示时,发请求获取地址列表(为了在添加地址之后实时更新地址列表)
  86. uni.showLoading({ title: '加载中', mask: true })
  87. this.$ajax.get(api_getAddress).then(([ , { data: res}]) => {
  88. this.$hideLoading()
  89. if (res.code === 200) {
  90. this.list = res.data.list
  91. this.list.forEach((e, i) => {
  92. if (e.status === 1) {
  93. this.index = i
  94. }
  95. })
  96. } else {
  97. this.$refs.toast.hover('获取地址失败')
  98. }
  99. })
  100. },
  101. created () { //加载完成后开启监听全局 MESSAGE 事件
  102. uni.$on('MESSAGE', (m, d, p) => { //m 要提示的文字信息 d 文字信息出现的时常 p 文字信息出现的位置
  103. this.$refs.toast.hover(m, d, p)
  104. })
  105. },
  106. onLoad(opt) { //根据进入页面时传的值确定用户从那个页面进入当前页面,并作出不同的展示
  107. if (opt.from === 'detail') {
  108. this.pageTitle = '地址选择'
  109. } else {
  110. this.pageTitle = '地址管理'
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="scss" scoped>
  116. .address {
  117. @include page();
  118. .content {
  119. background: $custom-nav-borderbot-color;
  120. .default {
  121. border-top: 10rpx solid $custom-nav-borderbot-color;
  122. background: #FFFFFF;
  123. .title {
  124. width: auto;
  125. color: #333333;
  126. font-size: 36rpx;
  127. margin: 36rpx 0 0 20rpx;
  128. padding-left: 20rpx;
  129. border-left: 6rpx solid #FA6342;
  130. }
  131. .info {
  132. border: 40rpx solid #FFFFFF;
  133. .name-tel {
  134. font-size: 30rpx;
  135. margin-bottom: 14rpx;
  136. .tel {
  137. font-size: 26rpx;
  138. margin-left: 8rpx;
  139. color: #AAAAAA;
  140. }
  141. }
  142. .address1, .address2 {
  143. font-size: 28rpx;
  144. margin-bottom: 8;
  145. }
  146. .addAddress {
  147. width: 200rpx;
  148. height: 60rpx;
  149. line-height: 60rpx;
  150. text-align: center;
  151. font-size: 30rpx;
  152. color: #F86541;
  153. border: 2rpx solid rgba(250,99,66,1);
  154. border-radius: 30rpx;
  155. padding: 0;
  156. margin: 30rpx 0 0;
  157. background: #FFFFFF;
  158. }
  159. .hover {
  160. background: #F86541;
  161. color: #FFFFFF;
  162. }
  163. }
  164. }
  165. .list {
  166. box-sizing: border-box;
  167. padding: 0 40rpx;
  168. margin-top: 20rpx;
  169. background: #FFFFFF;
  170. .item {
  171. box-sizing: border-box;
  172. height: 150rpx;
  173. border-bottom: 1rpx solid $custom-nav-borderbot-color;
  174. padding: 31rpx 0;
  175. color: #BBBBBB;
  176. .name-tel {
  177. height: 47rpx;
  178. line-height: 47rpx;
  179. font-size: 30rpx;
  180. .tel {
  181. font-size: 26rpx;
  182. margin-left: 8rpx;
  183. }
  184. }
  185. .item-address {
  186. height: 40rpx;
  187. line-height: 40rpx;
  188. text-overflow: ellipsis;
  189. white-space: nowrap;
  190. overflow: hidden;
  191. }
  192. .icon {
  193. float: right;
  194. font-size: 36rpx;
  195. line-height: 87rpx;
  196. &.cuIcon-roundcheck {
  197. color: #BBBBBB!important;
  198. }
  199. &.cuIcon-roundcheckfill {
  200. color: #FA6342!important;
  201. }
  202. }
  203. }
  204. }
  205. .set {
  206. width: 400rpx;
  207. height: 70rpx;
  208. line-height: 70rpx;
  209. text-align: center;
  210. background: rgba(250,99,66,1);
  211. color: #FFFFFF;
  212. margin: 138rpx auto 0;
  213. box-shadow: 0px 2rpx 4rpx 0px rgba(51,51,51,0.15);
  214. border-radius: 35rpx;
  215. }
  216. }
  217. }
  218. </style>