微信小店联盟带货小程序

list.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. Page({
  2. data: {
  3. addressList: [], // 地址列表
  4. selectMode: false, // 是否是选择地址模式
  5. },
  6. onLoad(options) {
  7. // 判断是否是从订单页面跳转来选择地址
  8. if (options.select) {
  9. this.setData({
  10. selectMode: true
  11. })
  12. }
  13. },
  14. onShow() {
  15. this.getAddressList()
  16. },
  17. // 获取地址列表
  18. getAddressList() {
  19. // TODO: 调用后端API获取地址列表
  20. // 这里先模拟数据
  21. const mockAddress = [{
  22. id: '1',
  23. name: '张三',
  24. phone: '13800138000',
  25. province: '广东省',
  26. city: '深圳市',
  27. district: '南山区',
  28. detail: '科技园南路XX号XX大厦',
  29. isDefault: true
  30. }, {
  31. id: '2',
  32. name: '李四',
  33. phone: '13900139000',
  34. province: '广东省',
  35. city: '广州市',
  36. district: '天河区',
  37. detail: '天河路XX号XX广场',
  38. isDefault: false
  39. }]
  40. this.setData({
  41. addressList: mockAddress
  42. })
  43. },
  44. // 选择地址(从订单页面进入时)
  45. selectAddress(e) {
  46. if (!this.data.selectMode) return
  47. const id = e.currentTarget.dataset.id
  48. const address = this.data.addressList.find(item => item.id === id)
  49. // 选择地址后返回上一页
  50. const pages = getCurrentPages()
  51. const prevPage = pages[pages.length - 2]
  52. prevPage.setData({
  53. selectedAddress: address
  54. })
  55. wx.navigateBack()
  56. },
  57. // 设为默认地址
  58. setDefault(e) {
  59. const id = e.currentTarget.dataset.id
  60. wx.showLoading({
  61. title: '设置中...'
  62. })
  63. // TODO: 调用后端API设置默认地址
  64. setTimeout(() => {
  65. wx.hideLoading()
  66. // 更新本地数据
  67. const addressList = this.data.addressList.map(item => ({
  68. ...item,
  69. isDefault: item.id === id
  70. }))
  71. this.setData({
  72. addressList
  73. })
  74. wx.showToast({
  75. title: '设置成功'
  76. })
  77. }, 500)
  78. },
  79. // 编辑地址
  80. editAddress(e) {
  81. const id = e.currentTarget.dataset.id
  82. wx.navigateTo({
  83. url: `/pages/address/edit?id=${id}`
  84. })
  85. },
  86. // 删除地址
  87. deleteAddress(e) {
  88. const id = e.currentTarget.dataset.id
  89. wx.showModal({
  90. title: '删除地址',
  91. content: '确认删除此地址?',
  92. success: (res) => {
  93. if (res.confirm) {
  94. // TODO: 调用后端API删除地址
  95. wx.showLoading({
  96. title: '删除中...'
  97. })
  98. setTimeout(() => {
  99. wx.hideLoading()
  100. // 更新本地数据
  101. const addressList = this.data.addressList.filter(item => item.id !== id)
  102. this.setData({
  103. addressList
  104. })
  105. wx.showToast({
  106. title: '删除成功'
  107. })
  108. }, 500)
  109. }
  110. }
  111. })
  112. },
  113. // 新增地址
  114. addAddress() {
  115. wx.navigateTo({
  116. url: '/pages/address/edit'
  117. })
  118. }
  119. })