search.js 5.1 KB

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