- Added a new method to fetch the default store for users without a login, allowing them to browse store rankings. - Updated the wxLogin function to streamline the login process for users with existing phone numbers, enabling direct token retrieval. - Refactored various page components to utilize the new getDefaultStore method for better user experience when accessing store information. - Enhanced error handling and data synchronization for user and store information across multiple pages.
182 lines
4.5 KiB
JavaScript
182 lines
4.5 KiB
JavaScript
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() {
|
||
let currentStore = app.globalData.currentStore
|
||
if (!currentStore) {
|
||
const lastStore = wx.getStorageSync('last_store')
|
||
if (lastStore && lastStore.storeId) {
|
||
currentStore = {
|
||
storeId: lastStore.storeId,
|
||
storeName: lastStore.storeName || '',
|
||
storeAddress: lastStore.storeAddress || ''
|
||
}
|
||
app.globalData.currentStore = 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
|
||
})
|
||
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
|
||
}
|
||
|
||
// 持久化上次选择的门店,下次启动优先使用
|
||
wx.setStorageSync('last_store', {
|
||
storeId: store.id,
|
||
storeName: store.name,
|
||
storeAddress: store.address
|
||
})
|
||
|
||
app.globalData.ladderUser = null
|
||
|
||
if (app.globalData.token) {
|
||
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)
|
||
}
|
||
})
|