address.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view class="content b-t">
  3. <view class="list" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
  4. <view class="wrapper">
  5. <view class="address-box">
  6. <text class="name">{{ item.real_name }}</text>
  7. <text class="mobile">{{ item.phone }}</text>
  8. </view>
  9. <view class="u-box">
  10. <text class="address">{{ item.province + item.city + item.district }} {{ item.detail }}</text>
  11. </view>
  12. </view>
  13. <view class="buttom">
  14. <view class="default-buttom" @click.stop="defaultUp(item,index)">
  15. <view class="iconfont iconroundcheckfill checkbox" :class="{ checked: item.is_default == 1 }"></view>
  16. <text class="text">设为默认地址</text>
  17. </view>
  18. <view class="operation" v-if="userInfo.address_edit == 1">
  19. <view @click.stop="addAddress('edit', item)">
  20. <text class="iconfont iconedit"></text>
  21. <text class="text">编辑</text>
  22. </view>
  23. <view class="blank"></view>
  24. <!-- <view @click.stop="delAddress(item)">
  25. <text class="iconfont icondelete"></text>
  26. <text class="text">删除</text>
  27. </view> -->
  28. </view>
  29. </view>
  30. </view>
  31. <button class="add-btn" @click="addAddress('add')" v-if="couldAdd">新增地址</button>
  32. </view>
  33. </template>
  34. <script>
  35. import { getAddressList,setAddressDefault,addressDel,getUserInfo } from '@/api/user.js';
  36. export default {
  37. data() {
  38. return {
  39. userInfo: {},
  40. source: 0,
  41. addressList: [],
  42. couldAdd: false
  43. };
  44. },
  45. onLoad(option) {
  46. this.source = option.source||0
  47. this.loadAddress();
  48. this.getUserInfo()
  49. },
  50. methods: {
  51. getUserInfo() {
  52. getUserInfo().then(({data}) => {
  53. console.log(data)
  54. this.userInfo = data
  55. })
  56. },
  57. // 加载地址
  58. loadAddress() {
  59. getAddressList({
  60. page: 1,
  61. limit: 100
  62. }).then(({ data }) => {
  63. this.addressList = data;
  64. if(data.length == 0) {
  65. this.couldAdd = true
  66. }else {
  67. this.couldAdd = false
  68. }
  69. });
  70. },
  71. // 设为默认地址
  72. defaultUp(data,ind) {
  73. this.addressList=this.addressList.map((e) => {
  74. e.is_default=0
  75. return e
  76. })
  77. this.addressList[ind].is_default=1
  78. setAddressDefault({
  79. id: data.id
  80. }).then(({ data }) => {
  81. this.loadAddress();
  82. }).catch((e) => {
  83. console.log(e);
  84. });
  85. },
  86. //删除地址
  87. delAddress(item) {
  88. addressDel({
  89. id:item.id
  90. }).then(({data})=>{
  91. this.$api.msg('删除成功')
  92. })
  93. let s = this.addressList.indexOf(item);
  94. this.addressList.splice(s, 1);
  95. },
  96. //选择地址
  97. checkAddress(item) {
  98. if (this.source == 1) {
  99. //this.$api.prePage()获取上一页实例,在App.vue定义
  100. this.$api.prePage().addressData = item;
  101. uni.navigateBack();
  102. }
  103. },
  104. // 添加地址
  105. addAddress(type, item) {
  106. uni.navigateTo({
  107. url: `/pages/set/addressManage?type=${type}&data=${JSON.stringify(item)}`
  108. });
  109. },
  110. //添加或修改成功之后回调
  111. refreshList() {
  112. // 重新加载地址
  113. this.loadAddress()
  114. this.getUserInfo()
  115. }
  116. }
  117. };
  118. </script>
  119. <style lang="scss">
  120. page {
  121. padding-bottom: 120rpx;
  122. padding-top: 20rpx;
  123. background-color: $page-color-base;
  124. }
  125. .content {
  126. position: relative;
  127. }
  128. .list {
  129. align-items: center;
  130. padding: 20rpx 30rpx;
  131. background: #fff;
  132. margin: 20rpx;
  133. margin-top: 0;
  134. .buttom {
  135. display: flex;
  136. align-items: center;
  137. justify-content: space-between;
  138. padding-top: 10rpx;
  139. .checkbox {
  140. font-size: 44rpx;
  141. line-height: 1;
  142. padding: 4rpx;
  143. color: $font-color-disabled;
  144. background: #fff;
  145. border-radius: 50px;
  146. }
  147. .checkbox.checked {
  148. color: $base-color;
  149. }
  150. .default-buttom {
  151. display: flex;
  152. align-items: center;
  153. }
  154. .operation {
  155. display: flex;
  156. align-items: center;
  157. .blank {
  158. width: 30rpx;
  159. }
  160. }
  161. .text {
  162. padding-left: 10rpx;
  163. font-size: 24rpx;
  164. color: #666666;
  165. }
  166. }
  167. }
  168. .wrapper {
  169. display: flex;
  170. flex-direction: column;
  171. flex: 1;
  172. border-bottom: 1px solid #f0f0f0;
  173. padding-bottom: 20rpx;
  174. }
  175. .address-box {
  176. display: flex;
  177. align-items: center;
  178. justify-content: space-between;
  179. .address {
  180. font-size: $font-base + 2rpx;
  181. color: $font-color-dark;
  182. }
  183. .mobile {
  184. font-size: $font-base;
  185. color: rgba(51, 51, 51, 1);
  186. }
  187. }
  188. .u-box {
  189. font-size: $font-base;
  190. color: $font-color-light;
  191. margin-top: 16rpx;
  192. .name {
  193. margin-right: 30rpx;
  194. }
  195. }
  196. .icon-bianji {
  197. display: flex;
  198. align-items: center;
  199. height: 80rpx;
  200. font-size: 40rpx;
  201. color: $font-color-light;
  202. padding-left: 30rpx;
  203. }
  204. .add-btn {
  205. position: fixed;
  206. left: 30rpx;
  207. right: 30rpx;
  208. bottom: 16rpx;
  209. z-index: 95;
  210. display: flex;
  211. align-items: center;
  212. justify-content: center;
  213. width: 690rpx;
  214. height: 80rpx;
  215. font-size: $font-lg;
  216. color: #fff;
  217. background-color: $base-color;
  218. border-radius: 10rpx;
  219. }
  220. </style>