search.ts 2.9 KB

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