index.vue 7.1 KB

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