80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
const { request } = require('../../utils/api.js')
|
|
|
|
Page({
|
|
data: {
|
|
city: '',
|
|
locating: false,
|
|
loading: false,
|
|
errorMessage: '',
|
|
stores: [],
|
|
},
|
|
|
|
onLoad(options) {
|
|
if (options.scene) {
|
|
this.resolveScene(decodeURIComponent(options.scene), options.source === 'nfc' ? 'NFC' : 'QRCODE')
|
|
}
|
|
},
|
|
|
|
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 })
|
|
}
|
|
},
|
|
|
|
async resolveScene(code, sourceType) {
|
|
this.setData({ loading: true, errorMessage: '' })
|
|
try {
|
|
const response = await request('/scenes/resolve', {
|
|
method: 'POST',
|
|
data: { code, sourceType },
|
|
})
|
|
const target = response.data
|
|
const query = [`storeId=${encodeURIComponent(target.storeId)}`]
|
|
if (target.roomId) query.push(`roomId=${encodeURIComponent(target.roomId)}`)
|
|
wx.navigateTo({ url: `${target.page}?${query.join('&')}` })
|
|
} catch (error) {
|
|
this.setData({ errorMessage: error.message || '场景码已失效' })
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
})
|