search.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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('search').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. wx.cloud.callFunction({
  116. name: 'listMessages',
  117. data: {
  118. keyword: this.data.searchText,
  119. page_token: 0,
  120. page_size: 20
  121. }
  122. }).then(res => {
  123. wx.hideLoading()
  124. if (res.result.status !== 'OK') {
  125. wx.showToast({
  126. title: res.result.errMsg,
  127. icon: 'none'
  128. })
  129. return
  130. }
  131. for (let i = 0; i < res.result.list.length; i++) {
  132. res.result.list[i] = util.dbToMsg(res.result.list[i])
  133. }
  134. this.setData({
  135. searchResult: res.result.list
  136. })
  137. })
  138. },
  139. cancel: function () {
  140. if (!this.data.toggleEnable || !this.data.searchEnable) return
  141. this.data.toggleEnable = false
  142. this.setData({
  143. searchText: '',
  144. })
  145. this.animate('.cancel-button', [{
  146. opacity: 1
  147. },
  148. {
  149. opacity: 0
  150. },
  151. ], 150)
  152. this.animate('.search-block', [{
  153. opacity: 1
  154. },
  155. {
  156. opacity: 0
  157. },
  158. ], 150)
  159. setTimeout(function () {
  160. this.setData({
  161. toggleEnable: true,
  162. searchEnable: false,
  163. searchResult: []
  164. })
  165. }.bind(this), 200)
  166. },
  167. searchTag: function (e) {
  168. this.setData({
  169. searchText: e.target.dataset.searchTag
  170. })
  171. this.focus()
  172. this.search()
  173. },
  174. searchAgain: function (e) {
  175. this.setData({
  176. searchText: e.target.dataset.history
  177. })
  178. this.focus()
  179. this.search()
  180. },
  181. removeHistory: function (e) {
  182. var history = this.data.searchHistory
  183. if (history === undefined) history = []
  184. for (let i = 0; i < history.length; i++) {
  185. if (history[i] == e.target.dataset.history) {
  186. history.splice(i, 1)
  187. break
  188. }
  189. }
  190. this.setData({
  191. searchHistory: history
  192. })
  193. wx.setStorage({
  194. key: 'searchHistory',
  195. data: history
  196. })
  197. },
  198. viewActivity: function (e) {
  199. wx.navigateTo({
  200. url: '/pages/activity/activity?id=' + e.target.dataset.activityId
  201. })
  202. }
  203. });