| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- var hotTagData = require("../../data/hotTagData.js")
- var hotBarData = require("../../data/hotBarData.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("searchData").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()
- },
- // 计算时间差
- handleDate: function(date) {
- var now = new Date().getTime()
- var diffValue = now - date.getTime()
- if (diffValue < 0) {
- console.log("时间不同步")
- return "刚刚"
- }
- var result = ""
- var minute = 1000 * 60
- var hour = minute * 60
- var day = hour * 24
- var minC = diffValue / minute
- var hourC = diffValue / hour
- var dayC = diffValue / day
- if (parseInt(dayC) > 30) {
- result += date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate()
- } else if (parseInt(dayC) > 1) {
- result += parseInt(dayC) + "天前"
- } else if (parseInt(dayC) == 1) {
- result += "昨天"
- } else if (hourC >= 1) {
- result += parseInt(hourC) + "小时前"
- } else if (minC >= 5) {
- result += parseInt(minC) + "分钟前"
- } else {
- result += "刚刚"
- }
- return result
- },
- 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: "搜索中",
- })
- const db = wx.cloud.database()
- const _ = db.command
- db.collection("searchData").add({
- data: {
- key: this.data.searchText,
- time: new Date()
- },
- success: function () {
- var reg = new RegExp(this.data.searchText, "i")
- db.collection("mainData").where(
- _.or([
- {title: reg},
- {subTitle: reg},
- {details: reg}
- ])
- ).orderBy("time", "desc").limit(20).get({
- success: function (res) {
- for (let i = 0; i < res.data.length; i++) {
- res.data[i].time = this.handleDate(res.data[i].time)
- }
- this.setData({
- searchResult: res.data
- })
- wx.hideLoading()
- }.bind(this)
- })
- }.bind(this)
- })
- },
- 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
- })
- }
- });
|