search.js 5.9 KB

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