search.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. var hotTagData = require("../../data/hotTagData.js")
  2. var hotBarData = require("../../data/hotBarData.js")
  3. Page({
  4. options: {
  5. styleIsolation: "apply-shared"
  6. },
  7. // 页面的初始数据
  8. data: {
  9. toggleEnable: true,
  10. searchEnable: false,
  11. searchText: "",
  12. hotTagData: [],
  13. hotBarData: [],
  14. searchResult: [],
  15. searchHistory: []
  16. },
  17. onLoad: function () {
  18. wx.getStorage({
  19. key: "searchHistory",
  20. success: function (res) {
  21. this.setData({
  22. searchHistory: res.data
  23. })
  24. }.bind(this)
  25. })
  26. const db = wx.cloud.database()
  27. db.collection("searchData").orderBy("time", "desc").limit(20).get({
  28. success: function (res) {
  29. var count = {}, hotTag = []
  30. for (let j = 0; j < res.data.length; j++) {
  31. if (count[res.data[j].key] == undefined) {
  32. count[res.data[j].key] = 0
  33. }
  34. count[res.data[j].key]++
  35. }
  36. for (let key in count) {
  37. hotTag.push({tag: key})
  38. }
  39. hotTag.sort(function (a, b) {
  40. return count[b] - count[a]
  41. })
  42. hotTag.splice(10)
  43. this.setData({
  44. hotTagData: hotTag
  45. })
  46. }.bind(this)
  47. })
  48. this.setData({
  49. hotBarData: hotBarData.hotBarData
  50. })
  51. },
  52. update: function (e) {
  53. this.setData({
  54. searchText: e.detail.value,
  55. searchResult: []
  56. })
  57. },
  58. focus: function () {
  59. if (!this.data.toggleEnable || this.data.searchEnable) return
  60. this.data.toggleEnable = false
  61. this.setData({
  62. searchEnable: true
  63. })
  64. this.animate('.cancel-button', [
  65. { opacity: 0 },
  66. { opacity: 1 },
  67. ], 150)
  68. this.animate('.search-block', [
  69. { opacity: 0 },
  70. { opacity: 1 },
  71. ], 150)
  72. setTimeout(function () {
  73. this.setData({
  74. toggleEnable: true
  75. })
  76. }.bind(this), 200)
  77. },
  78. blur: function () {
  79. if (this.data.searchText != "") return
  80. this.cancel()
  81. },
  82. // 计算时间差
  83. handleDate: function(date) {
  84. var now = new Date().getTime()
  85. var diffValue = now - date.getTime()
  86. if (diffValue < 0) {
  87. console.log("时间不同步")
  88. return "刚刚"
  89. }
  90. var result = ""
  91. var minute = 1000 * 60
  92. var hour = minute * 60
  93. var day = hour * 24
  94. var minC = diffValue / minute
  95. var hourC = diffValue / hour
  96. var dayC = diffValue / day
  97. if (parseInt(dayC) > 30) {
  98. result += date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate()
  99. } else if (parseInt(dayC) > 1) {
  100. result += parseInt(dayC) + "天前"
  101. } else if (parseInt(dayC) == 1) {
  102. result += "昨天"
  103. } else if (hourC >= 1) {
  104. result += parseInt(hourC) + "小时前"
  105. } else if (minC >= 5) {
  106. result += parseInt(minC) + "分钟前"
  107. } else {
  108. result += "刚刚"
  109. }
  110. return result
  111. },
  112. search: function () {
  113. var history = this.data.searchHistory
  114. if (history == undefined) history = []
  115. for (let i = 0; i < history.length; i++) {
  116. if (history[i] == this.data.searchText) {
  117. history.splice(i, 1)
  118. break
  119. }
  120. }
  121. if (history.length > 9) {
  122. history.splice(9, history.length - 9)
  123. }
  124. history = [this.data.searchText].concat(history)
  125. this.setData({
  126. searchHistory: history
  127. })
  128. wx.setStorage({
  129. key: "searchHistory",
  130. data: history
  131. })
  132. wx.showLoading({
  133. title: "搜索中",
  134. })
  135. const db = wx.cloud.database()
  136. const _ = db.command
  137. db.collection("searchData").add({
  138. data: {
  139. key: this.data.searchText,
  140. time: new Date()
  141. },
  142. success: function () {
  143. var reg = new RegExp(this.data.searchText, "i")
  144. db.collection("mainData").where(
  145. _.or([
  146. {title: reg},
  147. {subTitle: reg},
  148. {details: reg}
  149. ])
  150. ).orderBy("time", "desc").limit(20).get({
  151. success: function (res) {
  152. for (let i = 0; i < res.data.length; i++) {
  153. res.data[i].time = this.handleDate(res.data[i].time)
  154. }
  155. this.setData({
  156. searchResult: res.data
  157. })
  158. wx.hideLoading()
  159. }.bind(this)
  160. })
  161. }.bind(this)
  162. })
  163. },
  164. cancel: function () {
  165. if (!this.data.toggleEnable || !this.data.searchEnable) return
  166. this.data.toggleEnable = false
  167. this.setData({
  168. searchText: "",
  169. })
  170. this.animate('.cancel-button', [
  171. { opacity: 1 },
  172. { opacity: 0 },
  173. ], 150)
  174. this.animate('.search-block', [
  175. { opacity: 1 },
  176. { opacity: 0 },
  177. ], 150)
  178. setTimeout(function () {
  179. this.setData({
  180. toggleEnable: true,
  181. searchEnable: false,
  182. searchResult: []
  183. })
  184. }.bind(this), 200)
  185. },
  186. searchTag: function (e) {
  187. this.setData({
  188. searchText: e.target.dataset.searchTag
  189. })
  190. this.focus()
  191. this.search()
  192. },
  193. searchAgain: function (e) {
  194. this.setData({
  195. searchText: e.target.dataset.history
  196. })
  197. this.focus()
  198. this.search()
  199. },
  200. removeHistory: function (e) {
  201. var history = this.data.searchHistory
  202. if (history == undefined) history = []
  203. for (let i = 0; i < history.length; i++) {
  204. if (history[i] == e.target.dataset.history) {
  205. history.splice(i, 1)
  206. break
  207. }
  208. }
  209. this.setData({
  210. searchHistory: history
  211. })
  212. wx.setStorage({
  213. key: "searchHistory",
  214. data: history
  215. })
  216. },
  217. viewActivity: function (e) {
  218. wx.navigateTo({
  219. url: "/pages/activity/activity?id=" + e.target.dataset.activityId
  220. })
  221. }
  222. });