const hotTagData = require('../../data/hotTagData.js') const hotBarData = require('../../data/hotBarData.js') const util = require('../../utils/util.js') Page({ options: { styleIsolation: 'apply-shared' }, // 页面的初始数据 data: { toggleEnable: true, searchEnable: false, searchText: '', hotTagData: [], hotBarData: [], searchResult: [], searchHistory: [] }, onLoad: function () { wx.getStorage({ key: 'searchHistory', success: function (res) { this.setData({ searchHistory: res.data }) }.bind(this) }) const db = wx.cloud.database() db.collection('search').orderBy('time', 'desc').limit(20).get({ success: function (res) { var count = {}, hotTag = [] for (let j = 0; j < res.data.length; j++) { if (count[res.data[j].key] == undefined) { count[res.data[j].key] = 0 } count[res.data[j].key]++ } for (let key in count) { hotTag.push({ tag: key }) } hotTag.sort(function (a, b) { return count[b] - count[a] }) hotTag.splice(10) this.setData({ hotTagData: hotTag }) }.bind(this) }) this.setData({ hotBarData: hotBarData.hotBarData }) }, update: function (e) { this.setData({ searchText: e.detail.value, searchResult: [] }) }, focus: function () { if (!this.data.toggleEnable || this.data.searchEnable) return this.data.toggleEnable = false this.setData({ searchEnable: true }) this.animate('.cancel-button', [{ opacity: 0 }, { opacity: 1 }, ], 150) this.animate('.search-block', [{ opacity: 0 }, { opacity: 1 }, ], 150) setTimeout(function () { this.setData({ toggleEnable: true }) }.bind(this), 200) }, blur: function () { if (this.data.searchText !== '') return this.cancel() }, search: function () { var history = this.data.searchHistory if (history === undefined) history = [] for (let i = 0; i < history.length; i++) { if (history[i] == this.data.searchText) { history.splice(i, 1) break } } if (history.length > 9) { history.splice(9, history.length - 9) } history = [this.data.searchText].concat(history) this.setData({ searchHistory: history }) wx.setStorage({ key: 'searchHistory', data: history }) wx.showLoading({ title: '搜索中' }) wx.cloud.callFunction({ name: 'listMessages', data: { keyword: this.data.searchText, page_token: 0, page_size: 20 } }).then(res => { wx.hideLoading() if (res.result.status !== 'OK') { wx.showToast({ title: res.result.errMsg, icon: 'none' }) return } for (let i = 0; i < res.result.list.length; i++) { res.result.list[i] = util.dbToMsg(res.result.list[i]) } this.setData({ searchResult: res.result.list }) }) }, cancel: function () { if (!this.data.toggleEnable || !this.data.searchEnable) return this.data.toggleEnable = false this.setData({ searchText: '', }) this.animate('.cancel-button', [{ opacity: 1 }, { opacity: 0 }, ], 150) this.animate('.search-block', [{ opacity: 1 }, { opacity: 0 }, ], 150) setTimeout(function () { this.setData({ toggleEnable: true, searchEnable: false, searchResult: [] }) }.bind(this), 200) }, searchTag: function (e) { this.setData({ searchText: e.target.dataset.searchTag }) this.focus() this.search() }, searchAgain: function (e) { this.setData({ searchText: e.target.dataset.history }) this.focus() this.search() }, removeHistory: function (e) { var history = this.data.searchHistory if (history === undefined) history = [] for (let i = 0; i < history.length; i++) { if (history[i] == e.target.dataset.history) { history.splice(i, 1) break } } this.setData({ searchHistory: history }) wx.setStorage({ key: 'searchHistory', data: history }) }, viewActivity: function (e) { wx.navigateTo({ url: '/pages/activity/activity?id=' + e.target.dataset.activityId }) } });