Files
qipai/miniapp/pages/index/index.js
T

56 lines
1.4 KiB
JavaScript

const { request } = require('../../utils/api.js')
Page({
data: {
city: '',
locating: false,
loading: false,
errorMessage: '',
stores: [],
},
onCityInput(event) {
this.setData({ city: event.detail.value })
},
async searchByCity() {
const city = this.data.city.trim()
if (!city) {
this.setData({ errorMessage: '请输入城市名称' })
return
}
await this.loadStores({ city })
},
locateNearby() {
this.setData({ locating: true, errorMessage: '' })
wx.getLocation({
type: 'gcj02',
success: async ({ latitude, longitude }) => {
await this.loadStores({ latitude, longitude })
},
fail: () => {
this.setData({ errorMessage: '定位未授权,请输入城市手工选店' })
},
complete: () => {
this.setData({ locating: false })
},
})
},
async loadStores(filters) {
this.setData({ loading: true, errorMessage: '' })
try {
const query = Object.entries(filters)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&')
const response = await request(`/stores?${query}`)
this.setData({ stores: response.data || [] })
} catch (error) {
this.setData({ errorMessage: error.message || '门店加载失败' })
} finally {
this.setData({ loading: false })
}
},
})