const authApi = require('../../api/auth') Page({ data: { step: 1, role: 'manager', form: { shopName: '', shopAddress: '', shopId: '' }, registrationTicket: '', applicationId: '', shopOptions: [], shopSelectorItems: [], shopSheetVisible: false, shopKeyword: '', shopLoading: false, shopError: '', selectedShopName: '', loading: false, ticketInvalid: false }, async onLoad(options) { const registrationTicket = options && options.registrationTicket ? options.registrationTicket : '' this.setData({ registrationTicket, applicationId: options && options.applicationId ? options.applicationId : '', ticketInvalid: !registrationTicket }) await this.loadShops() }, async loadShops() { this.setData({ shopLoading: true, shopError: '' }) try { const shops = await authApi.getAvailableRegistrationShops() const shopOptions = (Array.isArray(shops) ? shops : []).map(item => ({ id: item.id, name: item.shopName || item.name, address: item.address || '' })) this.setData({ shopOptions, shopSelectorItems: this.filterShops(shopOptions, this.data.shopKeyword) }) } catch (error) { this.setData({ shopOptions: [], shopSelectorItems: [], shopError: error.message || '门店列表加载失败' }) } finally { this.setData({ shopLoading: false }) } }, onInput(event) { this.setData({ [`form.${event.currentTarget.dataset.field}`]: event.detail.value }) }, selectRole(event) { this.setData({ role: event.currentTarget.dataset.role }) }, continueFromRole() { if (!this.data.ticketInvalid) this.setData({ step: 2 }) }, filterShops(shops, keywordValue) { const keyword = String(keywordValue || '').trim().toLowerCase() return (shops || []) .filter(item => !keyword || [item.name, item.address].some(value => String(value || '').toLowerCase().includes(keyword))) .map(item => ({ ...item, title: item.name || '未命名门店', subtitle: item.address || '暂无地址' })) }, openShopSheet() { this.setData({ shopSheetVisible: true, shopSelectorItems: this.filterShops(this.data.shopOptions, this.data.shopKeyword) }) if (this.data.shopError) this.loadShops() }, closeShopSheet() { this.setData({ shopSheetVisible: false }) }, onShopSearch(event) { const shopKeyword = event.detail.value this.setData({ shopKeyword, shopSelectorItems: this.filterShops(this.data.shopOptions, shopKeyword) }) }, selectShop(event) { const shop = event.detail.item if (shop) this.setData({ selectedShopName: shop.name, 'form.shopId': shop.id, shopSheetVisible: false }) }, goBackStep() { this.setData({ step: 1 }) }, async submit() { const form = this.data.form if (!this.data.registrationTicket) { wx.showToast({ title: '注册票据已失效,请返回重新快捷登录', icon: 'none' }) return } if (this.data.role === 'manager' && !String(form.shopName || '').trim()) { wx.showToast({ title: '请输入店铺名称', icon: 'none' }); return } if (this.data.role === 'staff' && !form.shopId) { wx.showToast({ title: '请选择要加入的店铺', icon: 'none' }); return } this.setData({ loading: true }) try { await authApi.register({ registrationTicket: this.data.registrationTicket, applicationId: this.data.applicationId || undefined, shopName: String(form.shopName || '').trim(), shopAddress: String(form.shopAddress || '').trim(), shopId: form.shopId || undefined, roleType: this.data.role === 'manager' ? 'MANAGER' : 'STAFF', agreementAccepted: true }) this.setData({ step: 3, registrationTicket: '' }) } catch (error) { wx.showToast({ title: error.message || '提交失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, back() { wx.reLaunch({ url: '/pages/login/index' }) } })