Files
Mycontent/miniprogram/pages/address-edit/address-edit.js
2026-02-02 18:27:48 +08:00

118 lines
3.7 KiB
JavaScript

const app = getApp()
Page({
data: {
statusBarHeight: 44,
navBarHeight: 88,
id: '',
isEdit: false,
name: '',
phone: '',
province: '',
city: '',
district: '',
detail: '',
isDefault: false,
loading: false,
saving: false
},
onLoad(options) {
const statusBarHeight = app.globalData.statusBarHeight || 44
const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44)
const id = (options && options.id) ? decodeURIComponent(options.id) : ''
this.setData({ statusBarHeight, navBarHeight, id, isEdit: !!id })
if (id) this.loadAddress(id)
},
loadAddress(id) {
this.setData({ loading: true })
app.request('/api/user/addresses/' + encodeURIComponent(id))
.then(res => {
const item = res && res.item ? res.item : null
if (!item) {
this.setData({ loading: false })
return
}
this.setData({
loading: false,
name: item.name || '',
phone: item.phone || '',
province: item.province || '',
city: item.city || '',
district: item.district || '',
detail: item.detail || '',
isDefault: !!item.isDefault
})
})
.catch(() => this.setData({ loading: false }))
},
goBack() {
wx.navigateBack({ fail: () => wx.switchTab({ url: '/pages/my/my' }) })
},
onNameInput(e) { this.setData({ name: (e.detail && e.detail.value) || '' }) },
onPhoneInput(e) { this.setData({ phone: (e.detail && e.detail.value) || '' }) },
onProvinceInput(e) { this.setData({ province: (e.detail && e.detail.value) || '' }) },
onCityInput(e) { this.setData({ city: (e.detail && e.detail.value) || '' }) },
onDistrictInput(e) { this.setData({ district: (e.detail && e.detail.value) || '' }) },
onDetailInput(e) { this.setData({ detail: (e.detail && e.detail.value) || '' }) },
onDefaultChange(e) { this.setData({ isDefault: !!e.detail.value }) },
submit() {
const { id, isEdit, name, phone, province, city, district, detail, isDefault } = this.data
const user = app.globalData.userInfo
if (!user || !user.id) {
wx.showToast({ title: '请先登录', icon: 'none' })
return
}
if (!name || !name.trim()) {
wx.showToast({ title: '请输入姓名', icon: 'none' })
return
}
if (!phone || !phone.trim()) {
wx.showToast({ title: '请输入手机号', icon: 'none' })
return
}
if (!/^1[3-9]\d{9}$/.test(phone.trim())) {
wx.showToast({ title: '请输入正确的手机号', icon: 'none' })
return
}
if (!detail || !detail.trim()) {
wx.showToast({ title: '请输入详细地址', icon: 'none' })
return
}
this.setData({ saving: true })
const body = {
userId: user.id,
name: name.trim(),
phone: phone.trim(),
province: (province || '').trim(),
city: (city || '').trim(),
district: (district || '').trim(),
detail: detail.trim(),
isDefault: !!isDefault
}
if (isEdit && id) {
app.request('/api/user/addresses/' + encodeURIComponent(id), {
method: 'PUT',
data: body
}).then(() => {
this.setData({ saving: false })
wx.showToast({ title: '保存成功', icon: 'success' })
setTimeout(() => wx.navigateBack(), 1500)
}).catch(() => this.setData({ saving: false }))
} else {
app.request('/api/user/addresses', {
method: 'POST',
data: body
}).then(() => {
this.setData({ saving: false })
wx.showToast({ title: '添加成功', icon: 'success' })
setTimeout(() => wx.navigateBack(), 1500)
}).catch(() => this.setData({ saving: false }))
}
}
})