search.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. res.result.list[i].name = res.result.list[i].name.replace(new RegExp(this.data.searchText, 'gi'), '<span style="color: #009195;">$&</span>')
  103. res.result.list[i].brief = res.result.list[i].brief.replace(new RegExp(this.data.searchText, 'gi'), '<span style="color: #009195;">$&</span>')
  104. res.result.list[i].detail = res.result.list[i].detail.replace(new RegExp(this.data.searchText, 'gi'), '<span style="color: #009195;">$&</span>')
  105. }
  106. this.setData({
  107. searchResult: res.result.list,
  108. pageToken: res.result.next_page_token
  109. })
  110. })
  111. },
  112. loadMore: function () {
  113. if (this.data.loading) {
  114. return
  115. }
  116. this.setData({
  117. loading: true
  118. })
  119. wx.showNavigationBarLoading()
  120. wx.cloud.callFunction({
  121. name: 'listMessages',
  122. data: {
  123. keyword: this.data.searchText,
  124. page_token: this.data.pageToken,
  125. page_size: 20
  126. }
  127. }).then(res => {
  128. wx.hideNavigationBarLoading()
  129. if (res.result.status !== 'OK') {
  130. wx.showToast({
  131. title: res.result.errMsg,
  132. icon: 'none'
  133. })
  134. return
  135. }
  136. for (let i = 0; i < res.result.list.length; i++) {
  137. res.result.list[i] = util.dbToMsg(res.result.list[i])
  138. res.result.list[i].name = res.result.list[i].name.replace(new RegExp(this.data.searchText, 'gi'), '<span style="color: #009195;">$&</span>')
  139. res.result.list[i].brief = res.result.list[i].brief.replace(new RegExp(this.data.searchText, 'gi'), '<span style="color: #009195;">$&</span>')
  140. res.result.list[i].detail = res.result.list[i].detail.replace(new RegExp(this.data.searchText, 'gi'), '<span style="color: #009195;">$&</span>')
  141. }
  142. this.setData({
  143. searchResult: this.data.searchResult.concat(res.result.list),
  144. pageToken: res.result.next_page_token,
  145. loading: false
  146. })
  147. })
  148. },
  149. cancel: function () {
  150. if (!this.data.toggleEnable || !this.data.searchEnable) {
  151. return
  152. }
  153. this.setData({
  154. toggleEnable: false,
  155. })
  156. this.animate('.cancel-button', [{
  157. opacity: 1
  158. },
  159. {
  160. opacity: 0
  161. },
  162. ], 150)
  163. this.animate('.search-block', [{
  164. opacity: 1
  165. },
  166. {
  167. opacity: 0
  168. },
  169. ], 150)
  170. setTimeout(function () {
  171. this.setData({
  172. toggleEnable: true,
  173. searchEnable: false,
  174. searchText: '',
  175. searched: false,
  176. searchResult: []
  177. })
  178. }.bind(this), 200)
  179. },
  180. searchTag: function (e) {
  181. this.setData({
  182. searchText: e.currentTarget.dataset.searchTag
  183. })
  184. this.focus()
  185. this.search()
  186. },
  187. removeHistory: function (e) {
  188. let history = this.data.searchHistory
  189. if (history === undefined) history = []
  190. const index = history.indexOf(e.currentTarget.dataset.searchTag)
  191. if (index !== -1) {
  192. history.splice(index, 1)
  193. }
  194. this.setData({
  195. searchHistory: history
  196. })
  197. wx.setStorage({
  198. key: 'searchHistory',
  199. data: history
  200. })
  201. },
  202. viewMessage: function (e) {
  203. wx.navigateTo({
  204. url: '/pages/message/message'
  205. }).then(res => {
  206. res.eventChannel.emit('loadCommonData', {
  207. data: e.currentTarget.dataset.message
  208. })
  209. })
  210. },
  211. loadHotData: function () {
  212. wx.showNavigationBarLoading()
  213. const arr = []
  214. arr.push(wx.cloud.callFunction({
  215. name: 'listSearches'
  216. }))
  217. arr.push(wx.cloud.callFunction({
  218. name: 'listMessages',
  219. data: {
  220. hot: true
  221. }
  222. }))
  223. Promise.all(arr).then(res => {
  224. wx.hideNavigationBarLoading()
  225. if (res[0].result.status !== 'OK' || res[1].result.status !== 'OK') {
  226. wx.showToast({
  227. title: res[0].result.errMsg || res[1].result.errMsg,
  228. icon: 'none'
  229. })
  230. return
  231. }
  232. for (let i = 0; i < res[1].result.list.length; i++) {
  233. res[1].result.list[i].message = util.dbToMsg(res[1].result.list[i].message)
  234. const name = res[1].result.list[i].message.name
  235. res[1].result.list[i].message.hot_name = name.substr(0, 20) + (name.length > 20 ? '...' : '')
  236. }
  237. this.setData({
  238. hotTagData: res[0].result.list,
  239. hotBarData: res[1].result.list
  240. })
  241. })
  242. },
  243. onLoad: function () {
  244. wx.getStorage({
  245. key: 'searchHistory',
  246. success: function (res) {
  247. this.setData({
  248. searchHistory: res.data
  249. })
  250. }.bind(this)
  251. })
  252. this.loadHotData()
  253. }
  254. });