address.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <view class="container">
  3. <scroll-view
  4. scroll-y
  5. class="address-list"
  6. refresher-enabled
  7. :refresher-triggered="refreshing"
  8. @refresherrefresh="onRefresh"
  9. >
  10. <view v-if="loading && addressList.length === 0" class="loading-wrap">
  11. <text class="loading-text">加载中...</text>
  12. </view>
  13. <view v-else-if="addressList.length === 0" class="empty-wrap">
  14. <text class="empty-icon">📍</text>
  15. <text class="empty-text">暂无收货地址</text>
  16. </view>
  17. <view v-else>
  18. <view
  19. v-for="item in addressList"
  20. :key="item.id"
  21. class="address-card"
  22. @click="onSelect(item)"
  23. >
  24. <view class="card-top">
  25. <view class="name-phone">
  26. <text class="receiver-name">{{ item.receiverName }}</text>
  27. <text class="receiver-phone">{{ formatPhone(item.phone) }}</text>
  28. <text v-if="item.isDefault === 1" class="default-badge">默认</text>
  29. </view>
  30. </view>
  31. <text class="address-detail">
  32. {{ item.province }}{{ item.city }}{{ item.district }}{{ item.street }}
  33. </text>
  34. <view class="card-actions">
  35. <view class="set-default" v-if="item.isDefault !== 1">
  36. <text class="set-default-btn" @click.stop="onSetDefault(item.id)">设为默认</text>
  37. </view>
  38. <view class="action-right">
  39. <text class="action-btn edit-btn" @click.stop="onEdit(item.id)">✏️ 编辑</text>
  40. <text class="action-btn delete-btn" @click.stop="onDelete(item.id)">🗑 删除</text>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </scroll-view>
  46. <view class="bottom-bar">
  47. <button class="add-btn" @click="onAdd">+ 新增地址</button>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import config from '@/config.js'
  53. export default {
  54. data() {
  55. return {
  56. addressList: [],
  57. loading: false,
  58. refreshing: false,
  59. userId: null,
  60. selectMode: false
  61. }
  62. },
  63. onLoad(options) {
  64. this.userId = uni.getStorageSync('userId')
  65. if (options.selectMode) {
  66. this.selectMode = options.selectMode === '1'
  67. }
  68. this.loadAddressList()
  69. this._onLoadFired = true
  70. },
  71. onShow() {
  72. if (this._onLoadFired) {
  73. this._onLoadFired = false
  74. } else {
  75. this.loadAddressList()
  76. }
  77. },
  78. methods: {
  79. loadAddressList() {
  80. this.loading = true
  81. var that = this
  82. uni.request({
  83. url: config.api('/api/user/address/list'),
  84. method: 'POST',
  85. data: {},
  86. header: {
  87. 'Content-Type': 'application/json',
  88. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  89. },
  90. success: function(res) {
  91. that.loading = false
  92. that.refreshing = false
  93. if (res.data && res.data.code === 200) {
  94. that.addressList = res.data.data || []
  95. }
  96. },
  97. fail: function() {
  98. that.loading = false
  99. that.refreshing = false
  100. }
  101. })
  102. },
  103. onRefresh() {
  104. this.refreshing = true
  105. this.loadAddressList()
  106. },
  107. onSelect(item) {
  108. if (this.selectMode) {
  109. var pages = getCurrentPages()
  110. var prevPage = pages[pages.length - 2]
  111. if (prevPage) {
  112. prevPage.selectedAddress = item
  113. }
  114. uni.navigateBack()
  115. }
  116. },
  117. onEdit(id) {
  118. uni.navigateTo({ url: '/pages/shop/address-edit/address-edit?addressId=' + id })
  119. },
  120. onAdd() {
  121. uni.navigateTo({ url: '/pages/shop/address-edit/address-edit' })
  122. },
  123. onDelete(id) {
  124. var that = this
  125. uni.showModal({
  126. title: '确认删除',
  127. content: '确定删除该地址吗?',
  128. success: function(confirm) {
  129. if (confirm.confirm) {
  130. uni.request({
  131. url: config.api('/api/user/address/delete'),
  132. method: 'POST',
  133. data: { id: id },
  134. header: {
  135. 'Content-Type': 'application/json',
  136. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  137. },
  138. success: function(res) {
  139. if (res.data && res.data.code === 200) {
  140. uni.showToast({ title: '已删除', icon: 'success' })
  141. that.loadAddressList()
  142. } else {
  143. uni.showToast({ title: res.data.message || '删除失败', icon: 'none' })
  144. }
  145. }
  146. })
  147. }
  148. }
  149. })
  150. },
  151. onSetDefault(id) {
  152. var that = this
  153. uni.request({
  154. url: config.api('/api/user/address/setDefault'),
  155. method: 'POST',
  156. data: { id: id },
  157. header: {
  158. 'Content-Type': 'application/json',
  159. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  160. },
  161. success: function(res) {
  162. if (res.data && res.data.code === 200) {
  163. uni.showToast({ title: '已设为默认', icon: 'success' })
  164. that.loadAddressList()
  165. }
  166. }
  167. })
  168. },
  169. formatPhone(phone) {
  170. if (!phone) return ''
  171. if (phone.length === 11) {
  172. return phone.substr(0, 3) + '****' + phone.substr(7)
  173. }
  174. return phone
  175. }
  176. }
  177. }
  178. </script>
  179. <style scoped>
  180. .container {
  181. display: flex;
  182. flex-direction: column;
  183. height: 100vh;
  184. background: #f5f5f5;
  185. }
  186. .address-list {
  187. flex: 1;
  188. height: calc(100vh - 120rpx);
  189. }
  190. .loading-wrap,
  191. .empty-wrap {
  192. display: flex;
  193. flex-direction: column;
  194. align-items: center;
  195. padding-top: 200rpx;
  196. }
  197. .loading-text {
  198. font-size: 26rpx;
  199. color: #999;
  200. }
  201. .empty-icon {
  202. font-size: 100rpx;
  203. margin-bottom: 30rpx;
  204. }
  205. .empty-text {
  206. font-size: 28rpx;
  207. color: #999;
  208. }
  209. .address-card {
  210. background: #fff;
  211. margin: 20rpx;
  212. border-radius: 16rpx;
  213. padding: 24rpx;
  214. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  215. }
  216. .card-top {
  217. margin-bottom: 10rpx;
  218. }
  219. .name-phone {
  220. display: flex;
  221. align-items: center;
  222. }
  223. .receiver-name {
  224. font-size: 30rpx;
  225. font-weight: bold;
  226. color: #333;
  227. margin-right: 20rpx;
  228. }
  229. .receiver-phone {
  230. font-size: 26rpx;
  231. color: #666;
  232. }
  233. .default-badge {
  234. font-size: 20rpx;
  235. color: #F97316;
  236. background: #fff7e6;
  237. padding: 2rpx 12rpx;
  238. border-radius: 20rpx;
  239. margin-left: 12rpx;
  240. }
  241. .address-detail {
  242. display: block;
  243. font-size: 26rpx;
  244. color: #999;
  245. line-height: 1.5;
  246. margin-bottom: 16rpx;
  247. }
  248. .card-actions {
  249. display: flex;
  250. justify-content: space-between;
  251. align-items: center;
  252. border-top: 1rpx solid #f0f0f0;
  253. padding-top: 16rpx;
  254. }
  255. .set-default-btn {
  256. font-size: 24rpx;
  257. color: #1890ff;
  258. padding: 4rpx 12rpx;
  259. }
  260. .action-right {
  261. display: flex;
  262. }
  263. .action-btn {
  264. font-size: 24rpx;
  265. padding: 4rpx 12rpx;
  266. margin-left: 12rpx;
  267. }
  268. .edit-btn {
  269. color: #666;
  270. }
  271. .delete-btn {
  272. color: #ff4d4f;
  273. }
  274. .bottom-bar {
  275. position: fixed;
  276. bottom: 0;
  277. left: 0;
  278. right: 0;
  279. height: 100rpx;
  280. background: #fff;
  281. border-top: 1rpx solid #eee;
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. padding: 0 30rpx;
  286. z-index: 100;
  287. }
  288. .add-btn {
  289. width: 100%;
  290. height: 72rpx;
  291. line-height: 72rpx;
  292. background: #fff;
  293. color: #F97316;
  294. font-size: 28rpx;
  295. font-weight: bold;
  296. border: 2rpx solid #F97316;
  297. border-radius: 36rpx;
  298. text-align: center;
  299. }
  300. .add-btn::after {
  301. border: none;
  302. }
  303. </style>