search.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. update: function (e) {
  19. this.setData({
  20. searchText: e.detail.value,
  21. searchResult: []
  22. })
  23. },
  24. focus: function () {
  25. if (!this.data.toggleEnable || this.data.searchEnable) return
  26. this.data.toggleEnable = false
  27. this.setData({
  28. searchEnable: true
  29. })
  30. this.animate('.cancel-button', [{
  31. opacity: 0
  32. },
  33. {
  34. opacity: 1
  35. },
  36. ], 150)
  37. this.animate('.search-block', [{
  38. opacity: 0
  39. },
  40. {
  41. opacity: 1
  42. },
  43. ], 150)
  44. setTimeout(function () {
  45. this.setData({
  46. toggleEnable: true
  47. })
  48. }.bind(this), 200)
  49. },
  50. blur: function () {
  51. if (this.data.searchText !== '') return
  52. this.cancel()
  53. },
  54. search: function () {
  55. let history = this.data.searchHistory
  56. if (history === undefined) history = []
  57. const index = history.indexOf(this.data.searchText)
  58. if (index !== -1) {
  59. history.splice(index, 1)
  60. }
  61. if (history.length > 9) {
  62. history.splice(9, history.length - 9)
  63. }
  64. history.splice(0, 0, this.data.searchText)
  65. this.setData({
  66. searchHistory: history
  67. })
  68. wx.setStorage({
  69. key: 'searchHistory',
  70. data: history
  71. })
  72. wx.showLoading({
  73. title: '搜索中'
  74. })
  75. wx.cloud.callFunction({
  76. name: 'listMessages',
  77. data: {
  78. keyword: this.data.searchText,
  79. page_token: 0,
  80. page_size: 20
  81. }
  82. }).then(res => {
  83. wx.hideLoading()
  84. if (res.result.status !== 'OK') {
  85. wx.showToast({
  86. title: res.result.errMsg,
  87. icon: 'none'
  88. })
  89. return
  90. }
  91. for (let i = 0; i < res.result.list.length; i++) {
  92. res.result.list[i] = util.dbToMsg(res.result.list[i])
  93. }
  94. this.setData({
  95. searchResult: res.result.list
  96. })
  97. })
  98. },
  99. cancel: function () {
  100. if (!this.data.toggleEnable || !this.data.searchEnable) return
  101. this.data.toggleEnable = false
  102. this.setData({
  103. searchText: '',
  104. })
  105. this.animate('.cancel-button', [{
  106. opacity: 1
  107. },
  108. {
  109. opacity: 0
  110. },
  111. ], 150)
  112. this.animate('.search-block', [{
  113. opacity: 1
  114. },
  115. {
  116. opacity: 0
  117. },
  118. ], 150)
  119. setTimeout(function () {
  120. this.setData({
  121. toggleEnable: true,
  122. searchEnable: false,
  123. searchResult: []
  124. })
  125. }.bind(this), 200)
  126. },
  127. searchTag: function (e) {
  128. this.setData({
  129. searchText: e.currentTarget.dataset.searchTag
  130. })
  131. this.focus()
  132. this.search()
  133. },
  134. removeHistory: function (e) {
  135. let history = this.data.searchHistory
  136. if (history === undefined) history = []
  137. const index = history.indexOf(e.currentTarget.dataset.searchTag)
  138. if (index !== -1) {
  139. history.splice(index, 1)
  140. }
  141. this.setData({
  142. searchHistory: history
  143. })
  144. wx.setStorage({
  145. key: 'searchHistory',
  146. data: history
  147. })
  148. },
  149. viewActivity: function (e) {
  150. wx.navigateTo({
  151. url: '/pages/activity/activity'
  152. }).then(res => {
  153. res.eventChannel.emit('loadCommonData', {
  154. data: e.currentTarget.dataset.activity
  155. })
  156. })
  157. },
  158. loadHotData: function () {
  159. wx.showNavigationBarLoading()
  160. const arr = []
  161. arr.push(wx.cloud.callFunction({
  162. name: 'listSearches'
  163. }))
  164. arr.push(wx.cloud.callFunction({
  165. name: 'listMessages',
  166. data: {
  167. hot: true
  168. }
  169. }))
  170. Promise.all(arr).then(res => {
  171. wx.hideNavigationBarLoading()
  172. if (res[0].result.status !== 'OK' || res[1].result.status !== 'OK') {
  173. wx.showToast({
  174. title: res[0].result.errMsg || res[1].result.errMsg,
  175. icon: 'none'
  176. })
  177. return
  178. }
  179. for (let i = 0; i < res[1].result.list.length; i++) {
  180. res[1].result.list[i].message = util.dbToMsg(res[1].result.list[i].message)
  181. const name = res[1].result.list[i].message.name
  182. res[1].result.list[i].message.hot_name = name.substr(0, 20) + (name.length > 20 ? '...' : '')
  183. }
  184. this.setData({
  185. hotTagData: res[0].result.list,
  186. hotBarData: res[1].result.list
  187. })
  188. })
  189. },
  190. onLoad: function () {
  191. wx.getStorage({
  192. key: 'searchHistory',
  193. success: function (res) {
  194. this.setData({
  195. searchHistory: res.data
  196. })
  197. }.bind(this)
  198. })
  199. this.loadHotData()
  200. }
  201. });