index.vue 6.3 KB

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