123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- Page({
- data: {
- addressList: [], // 地址列表
- selectMode: false, // 是否是选择地址模式
- },
- onLoad(options) {
- // 判断是否是从订单页面跳转来选择地址
- if (options.select) {
- this.setData({
- selectMode: true
- })
- }
- },
- onShow() {
- this.getAddressList()
- },
- // 获取地址列表
- getAddressList() {
- // TODO: 调用后端API获取地址列表
- // 这里先模拟数据
- const mockAddress = [{
- id: '1',
- name: '张三',
- phone: '13800138000',
- province: '广东省',
- city: '深圳市',
- district: '南山区',
- detail: '科技园南路XX号XX大厦',
- isDefault: true
- }, {
- id: '2',
- name: '李四',
- phone: '13900139000',
- province: '广东省',
- city: '广州市',
- district: '天河区',
- detail: '天河路XX号XX广场',
- isDefault: false
- }]
- this.setData({
- addressList: mockAddress
- })
- },
- // 选择地址(从订单页面进入时)
- selectAddress(e) {
- if (!this.data.selectMode) return
-
- const id = e.currentTarget.dataset.id
- const address = this.data.addressList.find(item => item.id === id)
-
- // 选择地址后返回上一页
- const pages = getCurrentPages()
- const prevPage = pages[pages.length - 2]
- prevPage.setData({
- selectedAddress: address
- })
- wx.navigateBack()
- },
- // 设为默认地址
- setDefault(e) {
- const id = e.currentTarget.dataset.id
- wx.showLoading({
- title: '设置中...'
- })
- // TODO: 调用后端API设置默认地址
- setTimeout(() => {
- wx.hideLoading()
- // 更新本地数据
- const addressList = this.data.addressList.map(item => ({
- ...item,
- isDefault: item.id === id
- }))
- this.setData({
- addressList
- })
- wx.showToast({
- title: '设置成功'
- })
- }, 500)
- },
- // 编辑地址
- editAddress(e) {
- const id = e.currentTarget.dataset.id
- wx.navigateTo({
- url: `/pages/address/edit?id=${id}`
- })
- },
- // 删除地址
- deleteAddress(e) {
- const id = e.currentTarget.dataset.id
- wx.showModal({
- title: '删除地址',
- content: '确认删除此地址?',
- success: (res) => {
- if (res.confirm) {
- // TODO: 调用后端API删除地址
- wx.showLoading({
- title: '删除中...'
- })
- setTimeout(() => {
- wx.hideLoading()
- // 更新本地数据
- const addressList = this.data.addressList.filter(item => item.id !== id)
- this.setData({
- addressList
- })
- wx.showToast({
- title: '删除成功'
- })
- }, 500)
- }
- }
- })
- },
- // 新增地址
- addAddress() {
- wx.navigateTo({
- url: '/pages/address/edit'
- })
- }
- })
|