search.js 6.5 KB

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