// pages/search/index.js import { fetchEnterprise } from '../../../services/enterprise/fetchEnterprise'; import { useLoading } from '../../../behaviors/loading'; Page({ behaviors: [useLoading], data: { placeholder: '输入关键词搜索企业', searchValue: '', isSearch: false, searchResult: [], historyWords: [], dialog: { title: '确认删除当前历史记录', showCancelButton: true, message: '', }, dialogShow: false, }, deleteType: 0, deleteIndex: '', onLoad(options) {}, onShow() { this.queryHistory(); }, onReachBottom() { this._loadMore(); }, queryHistory() { wx.getStorage({ key: 'historyWords', success: res => { this.setData({ historyWords: res.data, }); }, }); }, //删除某个历史记录 deleteCurr(e) { const { index } = e.currentTarget.dataset; const { dialog } = this.data; this.deleteIndex = index; this.setData({ dialog: { ...dialog, message: '确认删除当前历史记录', deleteType: 0, }, dialogShow: true, }); }, //清空历史记录 handleClearHistory() { const { dialog } = this.data; this.deleteType = 1; this.setData({ dialog: { ...dialog, message: '确认删除所有历史记录', }, dialogShow: true, }); }, confirm() { const { historyWords } = this.data; const { deleteType, deleteIndex } = this; if (deleteType === 0) { historyWords.splice(deleteIndex, 1); this.setData({ historyWords, dialogShow: false, }); } else { this.setData({ historyWords: [], dialogShow: false }); } wx.setStorage({ key: 'historyWords', data: [], }); }, close() { this.setData({ dialogShow: false }); }, //点击历史记录 handleHistoryTap(e) { const { historyWords } = this.data; const { dataset } = e.currentTarget; const _searchValue = historyWords[dataset.index || 0] || ''; if (_searchValue) { this.setData({ searchValue: _searchValue }); this._startLoad(); } }, //点击搜索 handleSubmit() { const { historyWords, searchValue } = this.data; if (historyWords.indexOf(searchValue) == -1) { historyWords.push(searchValue); this.setData({ historyWords }); } wx.setStorage({ key: 'historyWords', data: historyWords, }); this._startLoad(); }, //清除搜索 handleClear() { this.setData({ searchValue: '', searchResult: [], isSearch: false }); }, handleChange() { if (this.data.searchValue == '') { this.setData({ searchResult: [], isSearch: false }); } }, _fetchData(page) { const { searchValue } = this.data; if (searchValue.length === 0) return; this.setData({ isSearch: true }); return fetchEnterprise({ page, data: { searchText: searchValue, userTypeId: 3 }, }).then(res => { this.setData({ searchResult: page == 1 ? res.data : this.data.searchResult.concat(res.data), }); return res.head; }); }, handCellClick(e) { console.log(e); let item = e.detail; // let item = this.data.searchResult[index]; wx.navigateTo({ url: '/pages/enterprise/detail/index', success: result => { result.eventChannel.emit('acceptEnterpriseData', { enterprise: item }); }, }); }, });