import { getFirmProfile, listLawyers } from '../../api/open'; import type { Lawyer } from '../../types/card'; const ALL_OFFICES = '所有办公机构'; const ALL_AREAS = '全部专业领域'; let searchDebounceTimer: number | null = null; Page({ data: { keyword: '', selectedOffice: ALL_OFFICES, selectedArea: ALL_AREAS, officeOptions: [ALL_OFFICES], areaOptions: [ALL_AREAS], filteredLawyers: [] as Lawyer[], loading: false, }, onLoad() { this.initializePage(); }, async initializePage() { this.setData({ loading: true }); try { const firm = await getFirmProfile(); this.setData({ officeOptions: [ALL_OFFICES, ...firm.officeList], areaOptions: [ALL_AREAS, ...firm.practiceAreas], }); await this.loadLawyers(); } catch (error) { const message = error instanceof Error ? error.message : '加载列表失败'; wx.showToast({ title: message, icon: 'none' }); } finally { this.setData({ loading: false }); } }, onUnload() { if (searchDebounceTimer !== null) { clearTimeout(searchDebounceTimer); searchDebounceTimer = null; } }, onSearchInput(event: WechatMiniprogram.CustomEvent<{ value: string }>) { const keyword = event.detail.value || ''; this.setData({ keyword }); if (searchDebounceTimer !== null) { clearTimeout(searchDebounceTimer); } searchDebounceTimer = setTimeout(() => { this.loadLawyers(); }, 250) as unknown as number; }, onSearchConfirm() { if (searchDebounceTimer !== null) { clearTimeout(searchDebounceTimer); searchDebounceTimer = null; } this.loadLawyers(); }, handleOfficeChange(event: WechatMiniprogram.CustomEvent<{ value: string }>) { this.setData({ selectedOffice: event.detail.value }); this.loadLawyers(); }, handleAreaChange(event: WechatMiniprogram.CustomEvent<{ value: string }>) { this.setData({ selectedArea: event.detail.value }); this.loadLawyers(); }, async loadLawyers() { this.setData({ loading: true }); try { const filteredLawyers = await listLawyers({ keyword: this.data.keyword.trim() || undefined, office: this.data.selectedOffice === ALL_OFFICES ? undefined : this.data.selectedOffice, practiceArea: this.data.selectedArea === ALL_AREAS ? undefined : this.data.selectedArea, }); this.setData({ filteredLawyers }); } catch (error) { const message = error instanceof Error ? error.message : '加载律师列表失败'; wx.showToast({ title: message, icon: 'none' }); } finally { this.setData({ loading: false }); } }, handleLawyerSelect(event: WechatMiniprogram.CustomEvent<{ id: string }>) { const lawyerId = event.detail.id; if (!lawyerId) { return; } wx.navigateTo({ url: `/pages/lawyer-detail/index?id=${lawyerId}`, }); }, });