Files
soul-yongping/miniprogram/pages/addresses/edit.js

216 lines
5.0 KiB
JavaScript

/**
* 地址编辑页(新增/编辑)
* 参考 Next.js: app/view/my/addresses/[id]/page.tsx
*/
const app = getApp()
Page({
data: {
statusBarHeight: 44,
isEdit: false, // 是否为编辑模式
addressId: null,
// 表单数据
name: '',
phone: '',
province: '',
city: '',
district: '',
detail: '',
isDefault: false,
// 地区选择器
region: [],
saving: false
},
onLoad(options) {
wx.showShareMenu({ withShareTimeline: true })
this.setData({
statusBarHeight: app.globalData.statusBarHeight || 44
})
// 如果有 id 参数,则为编辑模式
if (options.id) {
this.setData({
isEdit: true,
addressId: options.id
})
this.loadAddress(options.id)
}
},
// 加载地址详情(编辑模式)
async loadAddress(id) {
wx.showLoading({ title: '加载中...', mask: true })
try {
const res = await app.request(`/api/miniprogram/user/addresses/${id}`)
if (res.success && res.data) {
const addr = res.data
this.setData({
name: addr.name || '',
phone: addr.phone || '',
province: addr.province || '',
city: addr.city || '',
district: addr.district || '',
detail: addr.detail || '',
isDefault: addr.isDefault || false,
region: [addr.province, addr.city, addr.district]
})
} else {
wx.showToast({ title: '加载失败', icon: 'none' })
}
} catch (e) {
console.error('加载地址详情失败:', e)
wx.showToast({ title: '加载失败', icon: 'none' })
} finally {
wx.hideLoading()
}
},
// 表单输入
onNameInput(e) {
this.setData({ name: e.detail.value })
},
onPhoneInput(e) {
this.setData({ phone: e.detail.value.replace(/\D/g, '').slice(0, 11) })
},
onDetailInput(e) {
this.setData({ detail: e.detail.value })
},
// 地区选择
onRegionChange(e) {
const region = e.detail.value
this.setData({
region,
province: region[0],
city: region[1],
district: region[2]
})
},
// 切换默认地址
onDefaultChange(e) {
this.setData({ isDefault: e.detail.value })
},
// 表单验证
validateForm() {
const { name, phone, province, city, district, detail } = this.data
if (!name || name.trim().length === 0) {
wx.showToast({ title: '请输入收货人姓名', icon: 'none' })
return false
}
if (!phone || phone.length !== 11) {
wx.showToast({ title: '请输入正确的手机号', icon: 'none' })
return false
}
if (!province || !city || !district) {
wx.showToast({ title: '请选择省市区', icon: 'none' })
return false
}
if (!detail || detail.trim().length === 0) {
wx.showToast({ title: '请输入详细地址', icon: 'none' })
return false
}
return true
},
// 保存地址
async saveAddress() {
if (!this.validateForm()) return
if (this.data.saving) return
this.setData({ saving: true })
wx.showLoading({ title: '保存中...', mask: true })
const { isEdit, addressId, name, phone, province, city, district, detail, isDefault } = this.data
const userId = app.globalData.userInfo?.id
if (!userId) {
wx.hideLoading()
wx.showToast({ title: '请先登录', icon: 'none' })
this.setData({ saving: false })
return
}
const addressData = {
userId,
name,
phone,
province,
city,
district,
detail,
fullAddress: `${province}${city}${district}${detail}`,
isDefault
}
try {
let res
if (isEdit) {
// 编辑模式 - PUT 请求
res = await app.request(`/api/miniprogram/user/addresses/${addressId}`, {
method: 'PUT',
data: addressData
})
} else {
// 新增模式 - POST 请求
res = await app.request('/api/miniprogram/user/addresses', {
method: 'POST',
data: addressData
})
}
if (res.success) {
wx.hideLoading()
wx.showToast({
title: isEdit ? '保存成功' : '添加成功',
icon: 'success'
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
} else {
wx.hideLoading()
wx.showToast({ title: res.message || '保存失败', icon: 'none' })
this.setData({ saving: false })
}
} catch (e) {
console.error('保存地址失败:', e)
wx.hideLoading()
wx.showToast({ title: '保存失败', icon: 'none' })
this.setData({ saving: false })
}
},
// 返回
goBack() {
wx.navigateBack()
},
onShareAppMessage() {
const ref = app.getMyReferralCode()
return {
title: 'Soul创业派对 - 编辑地址',
path: ref ? `/pages/addresses/edit?ref=${ref}` : '/pages/addresses/edit'
}
},
onShareTimeline() {
const ref = app.getMyReferralCode()
return { title: 'Soul创业派对 - 编辑地址', query: ref ? `ref=${ref}` : '' }
}
})