search.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import GoodApi from '../../services/good'
  2. import Navi from '../../utils/navi'
  3. // pages/search/search.ts
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. tags: [] as string[],
  10. focusable: false,
  11. showResult: false,
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad(query: { from: string }) {
  17. if (query.from === 'cate') {
  18. this.setData({
  19. showResult: true,
  20. })
  21. }
  22. },
  23. /**
  24. * 生命周期函数--监听页面初次渲染完成
  25. */
  26. onReady() {},
  27. /**
  28. * 生命周期函数--监听页面显示
  29. */
  30. onShow() {
  31. this.setData({
  32. focusable: true,
  33. })
  34. this.handleReload()
  35. },
  36. /**
  37. * 生命周期函数--监听页面隐藏
  38. */
  39. onHide() {
  40. this.setData({
  41. focusable: false,
  42. })
  43. },
  44. /**
  45. * 生命周期函数--监听页面卸载
  46. */
  47. onUnload() {},
  48. /**
  49. * 页面相关事件处理函数--监听用户下拉动作
  50. */
  51. onPullDownRefresh() {},
  52. /**
  53. * 页面上拉触底事件的处理函数
  54. */
  55. onReachBottom() {},
  56. /**
  57. * 用户点击右上角分享
  58. */
  59. onShareAppMessage() {},
  60. handleInput(e: WechatMiniprogram.CustomEvent<{ value: string }>) {
  61. console.log(e.detail.value)
  62. const keyword = e.detail.value
  63. const cacheSearch = wx.getStorageSync('searchkeys')
  64. let keywords: string[] = []
  65. if (cacheSearch) {
  66. keywords = keywords.concat(JSON.parse(cacheSearch))
  67. }
  68. if (keyword && keyword.replace(/ /gi, '').length > 0 && keywords.indexOf(keyword) < 0) {
  69. keywords.splice(0, 0, keyword)
  70. wx.setStorageSync('searchkeys', JSON.stringify(keywords))
  71. this.setData({
  72. tags: keywords,
  73. })
  74. }
  75. this.goes(keyword)
  76. },
  77. handleClick(e: WechatMiniprogram.CustomEvent<{}, {}, { keyword: string }>) {
  78. this.goes(e.currentTarget.dataset.keyword)
  79. },
  80. handleReload() {
  81. const cacheSearch = wx.getStorageSync('searchkeys')
  82. let keywords: string[] = []
  83. if (cacheSearch) {
  84. keywords = keywords.concat(JSON.parse(cacheSearch))
  85. }
  86. this.setData({
  87. tags: keywords,
  88. })
  89. },
  90. goes(keyword: string) {
  91. if (this.data.showResult) {
  92. Navi.navigateTo({
  93. url: '/pages/search/result?keyword=' + keyword,
  94. })
  95. } else {
  96. const pages = getCurrentPages() //获取小程序页面栈
  97. const beforePage = pages[pages.length - 2] //获取上个页面的实例对象
  98. beforePage.research(keyword) //触发上个页面自定义的shuaxin方法
  99. wx.navigateBack({
  100. delta: 1,
  101. })
  102. }
  103. },
  104. })