138 lines
3.2 KiB
JavaScript
138 lines
3.2 KiB
JavaScript
/**
|
|
* 收货地址列表页
|
|
* 参考 Next.js: app/view/my/addresses/page.tsx
|
|
*/
|
|
|
|
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 44,
|
|
isLoggedIn: false,
|
|
addressList: [],
|
|
loading: true
|
|
},
|
|
|
|
onLoad() {
|
|
wx.showShareMenu({ withShareTimeline: true })
|
|
this.setData({
|
|
statusBarHeight: app.globalData.statusBarHeight || 44
|
|
})
|
|
this.checkLogin()
|
|
},
|
|
|
|
onShow() {
|
|
if (this.data.isLoggedIn) {
|
|
this.loadAddresses()
|
|
}
|
|
},
|
|
|
|
// 检查登录状态
|
|
checkLogin() {
|
|
const isLoggedIn = app.globalData.isLoggedIn
|
|
const userId = app.globalData.userInfo?.id
|
|
|
|
if (!isLoggedIn || !userId) {
|
|
wx.showModal({
|
|
title: '需要登录',
|
|
content: '请先登录后再管理收货地址',
|
|
confirmText: '去登录',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
wx.switchTab({ url: '/pages/my/my' })
|
|
} else {
|
|
getApp().goBackOrToHome()
|
|
}
|
|
}
|
|
})
|
|
return
|
|
}
|
|
|
|
this.setData({ isLoggedIn: true })
|
|
this.loadAddresses()
|
|
},
|
|
|
|
// 加载地址列表
|
|
async loadAddresses() {
|
|
const userId = app.globalData.userInfo?.id
|
|
if (!userId) return
|
|
|
|
this.setData({ loading: true })
|
|
|
|
try {
|
|
const res = await app.request(`/api/miniprogram/user/addresses?userId=${userId}`)
|
|
if (res.success && res.list) {
|
|
this.setData({
|
|
addressList: res.list,
|
|
loading: false
|
|
})
|
|
} else {
|
|
this.setData({ addressList: [], loading: false })
|
|
}
|
|
} catch (e) {
|
|
console.error('加载地址列表失败:', e)
|
|
this.setData({ loading: false })
|
|
wx.showToast({ title: '加载失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
// 编辑地址
|
|
editAddress(e) {
|
|
const id = e.currentTarget.dataset.id
|
|
wx.navigateTo({ url: `/pages/addresses/edit?id=${id}` })
|
|
},
|
|
|
|
// 删除地址
|
|
deleteAddress(e) {
|
|
const id = e.currentTarget.dataset.id
|
|
|
|
wx.showModal({
|
|
title: '确认删除',
|
|
content: '确定要删除该收货地址吗?',
|
|
confirmColor: '#FF3B30',
|
|
success: async (res) => {
|
|
if (res.confirm) {
|
|
try {
|
|
const result = await app.request(`/api/miniprogram/user/addresses/${id}`, {
|
|
method: 'DELETE'
|
|
})
|
|
|
|
if (result.success) {
|
|
wx.showToast({ title: '删除成功', icon: 'success' })
|
|
this.loadAddresses()
|
|
} else {
|
|
wx.showToast({ title: result.message || '删除失败', icon: 'none' })
|
|
}
|
|
} catch (e) {
|
|
console.error('删除地址失败:', e)
|
|
wx.showToast({ title: '删除失败', icon: 'none' })
|
|
}
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
// 新增地址
|
|
addAddress() {
|
|
wx.navigateTo({ url: '/pages/addresses/edit' })
|
|
},
|
|
|
|
// 返回
|
|
goBack() {
|
|
getApp().goBackOrToHome()
|
|
},
|
|
|
|
onShareAppMessage() {
|
|
const ref = app.getMyReferralCode()
|
|
return {
|
|
title: 'Soul创业派对 - 地址管理',
|
|
path: ref ? `/pages/addresses/addresses?ref=${ref}` : '/pages/addresses/addresses'
|
|
}
|
|
},
|
|
|
|
onShareTimeline() {
|
|
const ref = app.getMyReferralCode()
|
|
return { title: 'Soul创业派对 - 地址管理', query: ref ? `ref=${ref}` : '' }
|
|
}
|
|
})
|