search.js 5.8 KB

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