import { isRemoteSelectType } from '@/common/remoteSelect'; /** * 表单页远程下拉搜索。 * 使用方需实现 fetchRemoteSelectOptions(type, keyword),返回 { value, label }[]。 * * 注意:不要用 `_` / `$` 前缀的 data 字段名——Vue2 不会代理到 this, * `++this._xxx` 会得到 NaN,导致列表不更新、loading 不关。 */ export default { data() { return { selectSearching: false, selectSearchSeq: 0, }; }, computed: { isRemoteSelect() { return isRemoteSelectType(this.selectType); }, }, methods: { isRemoteSelectType, openSelectPicker(type, cachedList) { this.selectType = type; this.selectList = Array.isArray(cachedList) ? cachedList.slice() : []; this.selectShow = true; }, onSelectSearch(keyword) { if (!isRemoteSelectType(this.selectType)) return; if (typeof this.fetchRemoteSelectOptions !== 'function') return; const seq = ++this.selectSearchSeq; this.selectSearching = true; const finish = () => { if (seq === this.selectSearchSeq) this.selectSearching = false; }; Promise.resolve() .then(() => this.fetchRemoteSelectOptions(this.selectType, keyword)) .then((list) => { if (seq !== this.selectSearchSeq) return; this.selectList = Array.isArray(list) ? list.slice() : []; }) .catch((e) => { if (seq !== this.selectSearchSeq) return; if (typeof this.showError === 'function') { this.showError((e && e.message) || '搜索失败'); } else { uni.showToast({ title: (e && e.message) || '搜索失败', icon: 'none' }); } }) .then(finish, finish); }, }, };