Files
soul-yongping/miniprogram/pages/address-list/address-list.js
2026-02-02 18:27:48 +08:00

81 lines
2.1 KiB
JavaScript

const app = getApp()
Page({
data: {
statusBarHeight: 44,
navBarHeight: 88,
user: null,
list: [],
loading: true
},
onLoad() {
const statusBarHeight = app.globalData.statusBarHeight || 44
const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44)
this.setData({ statusBarHeight, navBarHeight })
this.syncUser()
this.loadList()
},
onShow() {
this.loadList()
},
syncUser() {
const user = app.globalData.userInfo || null
this.setData({ user })
},
loadList() {
const user = app.globalData.userInfo
if (!user || !user.id) {
this.setData({ list: [], loading: false })
return
}
this.setData({ loading: true })
app.request('/api/user/addresses?userId=' + encodeURIComponent(user.id))
.then(res => {
const list = (res && res.list) ? res.list : []
this.setData({ list, loading: false })
})
.catch(() => this.setData({ loading: false }))
},
goBack() {
wx.navigateBack({ fail: () => wx.switchTab({ url: '/pages/my/my' }) })
},
goAdd() {
wx.navigateTo({ url: '/pages/address-edit/address-edit' })
},
goEdit(e) {
const id = e.currentTarget.dataset.id
if (id) wx.navigateTo({ url: '/pages/address-edit/address-edit?id=' + encodeURIComponent(id) })
},
deleteAddr(e) {
const id = e.currentTarget.dataset.id
if (!id) return
const that = this
wx.showModal({
title: '提示',
content: '确定要删除该收货地址吗?',
success(res) {
if (!res.confirm) return
app.request('/api/user/addresses/' + encodeURIComponent(id), { method: 'DELETE' })
.then(data => {
if (data && data.success) {
const list = that.data.list.filter(item => item.id !== id)
that.setData({ list })
wx.showToast({ title: '已删除', icon: 'success' })
} else {
wx.showToast({ title: (data && data.message) ? data.message : '删除失败', icon: 'none' })
}
})
.catch(() => wx.showToast({ title: '删除失败', icon: 'none' }))
}
})
}
})