yingsa/miniprogram/pages/store/index.js
2026-01-20 08:50:50 +08:00

72 lines
1.6 KiB
JavaScript

const app = getApp()
Page({
data: {
stores: [],
currentStoreId: null
},
onLoad() {
this.fetchStores()
if (app.globalData.currentStore) {
this.setData({ currentStoreId: app.globalData.currentStore.storeId })
}
},
formatDistance(meters) {
if (meters < 1000) {
return Math.round(meters) + 'm'
}
return (meters / 1000).toFixed(1) + 'km'
},
async fetchStores() {
try {
// 尝试获取位置
wx.getLocation({
type: 'gcj02',
success: async (loc) => {
const res = await app.request('/api/store/nearby', {
latitude: loc.latitude,
longitude: loc.longitude,
radius: 50000
})
this.setData({ stores: res.data || [] })
},
fail: async () => {
// 无法获取位置,获取全部门店
const res = await app.request('/api/store/list')
this.setData({ stores: res.data.list || [] })
}
})
} catch (e) {
console.error('获取门店列表失败:', e)
}
},
async selectStore(e) {
const store = e.currentTarget.dataset.store
this.setData({ currentStoreId: store.id })
// 更新全局门店
app.globalData.currentStore = {
storeId: store.id,
storeName: store.name
}
// 获取该门店的天梯用户信息
try {
await app.getLadderUser(store.id)
} catch (e) {
console.error('获取天梯信息失败:', e)
}
wx.showToast({ title: '切换成功', icon: 'success' })
setTimeout(() => {
wx.navigateBack()
}, 1000)
}
})