yingsa/miniprogram/pages/store/index.js

165 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const app = getApp()
Page({
data: {
stores: [],
currentStoreId: null,
currentStoreName: '',
currentStoreAddress: '',
loading: false
},
onLoad() {
this.initCurrentStore()
this.fetchStores()
},
onShow() {
// 每次显示页面时刷新当前门店信息
this.initCurrentStore()
},
async onPullDownRefresh() {
try {
await this.fetchStores()
} finally {
wx.stopPullDownRefresh()
}
},
// 初始化当前门店信息
initCurrentStore() {
const currentStore = app.globalData.currentStore
if (currentStore) {
this.setData({
currentStoreId: currentStore.storeId,
currentStoreName: currentStore.storeName || '',
currentStoreAddress: currentStore.storeAddress || ''
})
}
},
formatDistance(meters) {
if (!meters) return ''
if (meters < 1000) {
return Math.round(meters) + 'm'
}
return (meters / 1000).toFixed(1) + 'km'
},
async fetchStores() {
this.setData({ loading: true })
try {
// 如果已登录,尝试获取附近门店
if (app.globalData.token) {
wx.getLocation({
type: 'gcj02',
success: async (loc) => {
try {
const res = await app.request('/api/store/nearby', {
latitude: loc.latitude,
longitude: loc.longitude,
radius: 50000
})
this.setData({
stores: res.data || [],
loading: false
})
// 更新当前门店的完整信息(地址等)
this.updateCurrentStoreInfo(res.data || [])
} catch (e) {
// 如果失败fallback 到门店列表
this.fetchStoreList()
}
},
fail: () => {
this.fetchStoreList()
}
})
} else {
// 未登录,直接获取门店列表
this.fetchStoreList()
}
} catch (e) {
console.error('获取门店列表失败:', e)
this.setData({ loading: false })
}
},
async fetchStoreList() {
try {
const res = await app.request('/api/store/list')
const stores = res.data.list || []
this.setData({
stores: stores,
loading: false
})
// 更新当前门店的完整信息
this.updateCurrentStoreInfo(stores)
} catch (e) {
console.error('获取门店列表失败:', e)
this.setData({ loading: false })
}
},
// 从门店列表中更新当前门店的完整信息
updateCurrentStoreInfo(stores) {
const { currentStoreId } = this.data
if (!currentStoreId || !stores.length) return
const currentStore = stores.find(s => s.id === currentStoreId)
if (currentStore) {
this.setData({
currentStoreName: currentStore.name,
currentStoreAddress: currentStore.address
})
// 同时更新全局数据
app.globalData.currentStore = {
storeId: currentStore.id,
storeName: currentStore.name,
storeAddress: currentStore.address
}
}
},
async selectStore(e) {
const store = e.currentTarget.dataset.store
wx.showLoading({ title: '切换中...' })
this.setData({
currentStoreId: store.id,
currentStoreName: store.name,
currentStoreAddress: store.address
})
// 更新全局门店
app.globalData.currentStore = {
storeId: store.id,
storeName: store.name,
storeAddress: store.address
}
// 清空旧的天梯用户信息
app.globalData.ladderUser = null
// 获取该门店的天梯用户信息
try {
await app.getLadderUser(store.id)
} catch (e) {
console.error('获取天梯信息失败:', e)
}
// 标记需要刷新数据
app.globalData.storeChanged = true
wx.hideLoading()
wx.showToast({ title: '切换成功', icon: 'success' })
setTimeout(() => {
wx.navigateBack()
}, 800)
}
})