// 医院选择:列表可搜索(名称 / 主体名 / 全拼 / 拼音首字母);列表里没有的走「其他医院」手填 const SEARCH_PLACEHOLDER = '搜索医院名称或拼音' // 关键词去掉空白、转小写:中文原样匹配,拼音统一小写比较 const normalizeKeyword = (value) => String(value || '').trim().toLowerCase().replace(/\s+/g, '') function filterHospitals(options, keyword) { const kw = normalizeKeyword(keyword) if (!kw) return options || [] return (options || []).filter((item) => [ item.name, item.entityName, item.pinyin, item.pinyinFL, ...(Array.isArray(item.nicknames) ? item.nicknames : []), ].some((field) => normalizeKeyword(field).includes(kw))) } Component({ properties: { label: { type: String, value: '就诊医院' }, value: { type: String, value: '' }, options: { type: Array, value: [], observer(newValue) { this.refreshFiltered(newValue, this.data.keyword) }, }, placeholder: { type: String, value: '请选择就诊医院' }, disabled: { type: Boolean, value: false }, compact: { type: Boolean, value: false }, }, data: { open: false, keyword: '', filtered: [], panelHeight: 0, keyboardHeight: 0, manualEntry: false, manualName: '', searchPlaceholder: SEARCH_PLACEHOLDER, }, lifetimes: { attached() { this.refreshFiltered(this.data.options, '') }, }, methods: { refreshFiltered(options = this.data.options, keyword = this.data.keyword) { const filtered = filterHospitals(options, keyword) this.setData({ filtered }) }, onOpen() { if (this.data.disabled) return if (this.data.open) { this.onClose() return } this.windowHeight = wx.getWindowInfo ? wx.getWindowInfo().windowHeight : wx.getSystemInfoSync().windowHeight this.setData({ open: true, keyword: '', manualEntry: false, keyboardHeight: 0, panelHeight: Math.floor(this.windowHeight * 0.82) }) this.refreshFiltered(this.data.options, '') this.triggerEvent('open') }, onClose() { this.setData({ open: false, keyword: '', keyboardHeight: 0, manualEntry: false }) wx.hideKeyboard() this.triggerEvent('close') }, onKeyboardHeightChange(e) { if (!this.data.open) return const keyboardHeight = Math.max(0, Number(e.detail.height) || 0) const available = Math.max(0, this.windowHeight - keyboardHeight) this.setData({ keyboardHeight, panelHeight: Math.floor(Math.min(this.windowHeight * 0.82, available)) }) }, // 遮罩占位:catchtouchmove 阻止穿透滚动页面 noop() {}, onKeywordInput(e) { const keyword = e.detail.value this.setData({ keyword }) this.refreshFiltered(this.data.options, keyword) }, onSelect(e) { if (this.data.disabled) return const index = Number(e.currentTarget.dataset.index) const hospital = this.data.filtered[index] if (!hospital) return this.onClose() this.triggerEvent('change', { name: hospital.name, manual: false, hospital, }) }, // 「其他医院」:展开手填输入框;有搜索词时带入搜索词,否则带入当前已填名称 onStartManual() { const keyword = String(this.data.keyword || '').trim() const current = String(this.data.value || '').trim() this.setData({ manualEntry: true, manualName: keyword || current }) }, onManualNameInput(e) { this.setData({ manualName: e.detail.value }) }, onCancelManual() { this.setData({ manualEntry: false, manualName: '' }) this.onKeyboardHeightChange({ detail: { height: 0 } }) wx.hideKeyboard() }, onConfirmManual() { if (this.data.disabled) return const name = String(this.data.manualName || '').trim() if (!name) return this.onClose() this.triggerEvent('change', { name, manual: true, hospital: null }) }, }, })