index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <view>
  3. <u-navbar title="我的地址" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
  4. <view class="addres-list">
  5. <view class="address-item" v-for="(item, index) in list" :key="index">
  6. <view class="contacts">
  7. <text class="name">{{ item.receiver }}</text>
  8. <text class="phone">{{ item.mobile }}</text>
  9. </view>
  10. <view class="address">
  11. <text class="region">{{ `${ item.province }-${ item.city }-${ item.area }` }}</text>
  12. <text class="detail">{{ item.addr }}</text>
  13. </view>
  14. <view class="line"></view>
  15. <view class="item-option">
  16. <view class="_left">
  17. <label class="item-radio" @click.stop="handleSetDefault(item.addrId)">
  18. <radio class="radio" color="#fa2209" :checked="item.addrId == defaultId"></radio>
  19. <text class="text">{{ item.addrId == defaultId ? '默认' : '选择' }}</text>
  20. </label>
  21. </view>
  22. <view class="_right">
  23. <view class="events">
  24. <view class="event-item" @click="handleUpdate(item.addrId)">
  25. <text class="iconfont icon-edit"></text>
  26. <text class="title">编辑</text>
  27. </view>
  28. <view class="event-item" @click="handleRemove(item.addrId)">
  29. <text class="iconfont icon-delete"></text>
  30. <text class="title">删除</text>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <u-empty text="亲,暂无收货地址" marginTop="100" mode="data" v-if="!list.length" />
  38. <!-- 底部操作按钮 -->
  39. <view class="footer-fixed">
  40. <view class="btn-wrapper">
  41. <button type="default" @click="handleCreate">添加地址</button>
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. import $http from '@/utils/request.js'
  48. export default {
  49. data() {
  50. return {
  51. //当前页面参数
  52. options: {},
  53. // 正在加载
  54. isLoading: true,
  55. // 收货地址列表
  56. list: [],
  57. // 默认收货地址
  58. defaultId: null
  59. }
  60. },
  61. onLoad(options) {
  62. // 当前页面参数
  63. this.options = options
  64. },
  65. onShow() {
  66. // 获取页面数据
  67. this.getPageData()
  68. },
  69. methods: {
  70. // 获取页面数据
  71. getPageData() {
  72. const app = this
  73. app.isLoading = true
  74. Promise.all([app.getDefaultId(), app.getAddressList()])
  75. .then(() => {
  76. // 列表排序把默认收货地址放到最前
  77. app.onReorder()
  78. })
  79. },
  80. // 获取收货地址列表
  81. getAddressList() {
  82. $http.post('/api/v1/mp/user/addr/list', {}).then(res => {
  83. if (res.code == 0) {
  84. this.list = res.data
  85. }
  86. })
  87. },
  88. // 获取默认的收货地址
  89. getDefaultId() {
  90. $http.post('/api/v1/mp/user/addr/queryDefault', {}).then(res => {
  91. if (res.code == 0) {
  92. this.defaultId = res.data.addrId
  93. }
  94. })
  95. },
  96. // 列表排序把默认收货地址放到最前
  97. onReorder() {
  98. const app = this
  99. app.list.sort(item => {
  100. return item.addrId == app.defaultId ? -1 : 1
  101. })
  102. },
  103. // 添加地址
  104. handleCreate() {
  105. uni.navigateTo({
  106. url: "/pages/address/create"
  107. })
  108. },
  109. // 编辑地址
  110. handleUpdate(addressId) {
  111. uni.navigateTo({
  112. url: `/pages/address/create?addrId=${ addressId }`
  113. })
  114. },
  115. // 删除地址
  116. handleRemove(addressId) {
  117. const _this = this
  118. uni.showModal({
  119. title: "提示",
  120. content: "您确定要删除当前收货地址吗?",
  121. success(res) {
  122. if (res.confirm) {
  123. $http.post('/api/v1/mp/user/addr/remove', {
  124. addrId: addressId
  125. }).then(res=>{
  126. if(res.code == 0){
  127. _this.getPageData()
  128. uni.$u.toast('删除成功');
  129. }
  130. })
  131. }
  132. }
  133. });
  134. },
  135. // 设置默认地址
  136. handleSetDefault(addressId) {
  137. $http.post('/api/v1/mp/user/addr/setDefault', {
  138. addrId: addressId
  139. }).then(res => {
  140. this.getPageData()
  141. })
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. .addres-list {
  148. padding-bottom: calc(constant(safe-area-inset-bottom) + 120rpx);
  149. padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
  150. }
  151. .address-item {
  152. margin: 20rpx auto 20rpx auto;
  153. padding: 30rpx 40rpx;
  154. width: 94%;
  155. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  156. border-radius: 16rpx;
  157. background: #fff;
  158. }
  159. .contacts {
  160. font-size: 30rpx;
  161. margin-bottom: 16rpx;
  162. .name {
  163. margin-right: 16rpx;
  164. }
  165. }
  166. .address {
  167. font-size: 28rpx;
  168. .region {
  169. margin-right: 10rpx;
  170. }
  171. }
  172. .line {
  173. margin: 20rpx 0;
  174. border-bottom: 1rpx solid #f3f3f3;
  175. }
  176. .item-option {
  177. display: flex;
  178. justify-content: space-between;
  179. height: 48rpx;
  180. // 单选框
  181. .item-radio {
  182. font-size: 28rpx;
  183. .radio {
  184. vertical-align: middle;
  185. transform: scale(0.76)
  186. }
  187. .text {
  188. vertical-align: middle;
  189. }
  190. }
  191. // 操作
  192. .events {
  193. display: flex;
  194. align-items: center;
  195. line-height: 48rpx;
  196. .event-item {
  197. font-size: 28rpx;
  198. margin-right: 26rpx;
  199. color: #4c4c4c;
  200. &:last-child {
  201. margin-right: 0;
  202. }
  203. .title {
  204. margin-left: 8rpx;
  205. }
  206. }
  207. }
  208. }
  209. // 底部操作栏
  210. .footer-fixed {
  211. position: fixed;
  212. bottom: var(--window-bottom);
  213. left: 0;
  214. right: 0;
  215. min-height: 120rpx;
  216. z-index: 11;
  217. box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
  218. background: #fff;
  219. // 设置ios刘海屏底部横线安全区域
  220. padding-bottom: constant(safe-area-inset-bottom);
  221. padding-bottom: env(safe-area-inset-bottom);
  222. .btn-wrapper {
  223. height: 120rpx;
  224. display: flex;
  225. align-items: center;
  226. padding: 0 40rpx;
  227. }
  228. /deep/ button {
  229. width: 100%;
  230. line-height: 76rpx;
  231. font-size: 28rpx;
  232. height: 76rpx;
  233. color: #fff;
  234. background-color: $uni-bg-color;
  235. border: none;
  236. border-radius: 100rpx;
  237. }
  238. }
  239. </style>