const { request } = require('../../utils/api.js') Page({ data: { city: '', locating: false, loading: false, errorMessage: '', stores: [], showManagerEntry: false, showCleanerEntry: false, }, onLoad(options) { if (options.scene) { this.resolveScene(decodeURIComponent(options.scene), options.source === 'nfc' ? 'NFC' : 'QRCODE') } }, onShow() { this.loadRoleEntrances() }, async loadRoleEntrances() { if (!wx.getStorageSync('qipai_access_token')) { this.setData({ showManagerEntry: false, showCleanerEntry: false }) return } try { const response = await request('/auth/me') const roles = response.data?.access?.roles || [] this.setData({ showManagerEntry: roles.some((role) => ['STAFF', 'STORE_ADMIN', 'TENANT_ADMIN', 'PLATFORM_ADMIN'].includes(role)), showCleanerEntry: roles.includes('CLEANER'), }) } catch (_) { this.setData({ showManagerEntry: false, showCleanerEntry: false }) } }, 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 }) } }, openStore(event) { const storeId = event.currentTarget.dataset.storeId if (!storeId) return wx.navigateTo({ url: `/pages/store/detail?storeId=${encodeURIComponent(storeId)}` }) }, openOrders() { wx.navigateTo({ url: '/pages/orders/list' }) }, openProfile() { wx.navigateTo({ url: '/pages/profile/index' }) }, openFranchise() { wx.navigateTo({ url: '/pages/franchise/apply' }) }, openManager() { wx.navigateTo({ url: '/pages/manager/dashboard' }) }, openCleaner() { wx.navigateTo({ url: '/pages/cleaner/tasks' }) }, 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 }) } }, })