riku
2022-08-10 51600113c20d84ee54660de23324400604704dc2
pages/module_consult/consultsearch/consultsearch.js
@@ -1,4 +1,7 @@
// pages/module_consult/consultsearch/consultsearch.js
const consultservice = require("../../../service/consultservice")
const app = getApp()
Page({
  /**
   * 页面的初始数据
@@ -8,16 +11,22 @@
    placeholder: '搜索',
    value: '',
    histroy: [],
    history: ['a', 'b'],
    hotTopic: [],
    hotTopic: [{
      name: '热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门热门'
    },{
      name: 't2'
    },{
      name: 't3'
    }],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHotTopic()
  },
  /**
@@ -27,61 +36,111 @@
  },
  onShow: function () {
    this.getLocalHistory()
  },
  clearInput() {
    // @ts-ignore
    this.setData({
      value: '',
      focus: true,
      result: []
    }); // @ts-ignore
    this.triggerEvent('clear');
  },
  // @ts-ignore
  inputFocus(e) {
    // this.setData({
    //     searchState: true
    // })
    // @ts-ignore
    this.triggerEvent('focus', e.detail);
  },
  // @ts-ignore
  inputBlur(e) {
    this.setData({
      focus: false
    });
    this.triggerEvent('blur', e.detail);
  },
  // @ts-ignore
  inputChange(e) {
    this.setData({
      value: e.detail.value
    });
    this.triggerEvent('input', e.detail);
    // if (Date.now() - this.lastSearch < this.data.throttle) {
    //   return;
    // }
    if (typeof this.data.search !== 'function') {
      return;
    }
    this.lastSearch = Date.now();
    this.timerId = setTimeout(() => {
      if (Date.now() - this.lastSearch < this.data.throttle) {
        return;
      }
      this.data.search(e.detail.value).then(json => {
        this.setData({
          result: json
        });
      }).catch(err => {
        console.error('search error', err);
      });
    }, this.data.throttle);
  },
  /**
   * 搜索
   */
  search() {
    if (this.data.value.length == 0) {
      wx.navigateBack({
        delta: 1,
      })
      return
    }
    var that = this
    wx.navigateTo({
      url: '/pages/module_consult/consultresult/consultresult',
      success: (res) => {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          keyword: that.data.value
        })
      },
    })
    if (this.data.history.indexOf(this.data.value) == -1) {
      let h = this.data.history
      h.unshift(this.data.value)
      if (h.length > 10) {
        h = h.slice(0, 10)
      }
      this.setData({
        history: h
      })
      wx.setStorage({
        data: JSON.stringify(h),
        key: 'search_history',
      })
    }
  },
  selectHistory (e) {
    var i = e.currentTarget.dataset.index
    var kw = this.data.history[i]
    this.setData({
      value: kw
    })
    this.search()
  },
  /**
   * 获取本地缓存搜索历史
   */
  getLocalHistory() {
    var that = this
    wx.getStorage({
      key: 'search_history',
      success: (res) => {
        let h = JSON.parse(res.data)
        that.setData({
          history: h
        })
      },
    })
  },
  /**
   * 获取热门搜索内容
   */
  getHotTopic() {
    var that = this
    consultservice.getTopicLaw(app.globalData.accessToken.userId, {
      success (res) {
        that.setData({
          hotTopic: res
        })
      }
    })
  },
  gotoDetail (e) {
    var i = e.currentTarget.dataset.index
    var fileId = this.data.hotTopic[i].id
    wx.navigateTo({
      url: '/pages/module_consult/consultdetail/consultdetail',
      success: (res) => {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          fileId: fileId
        })
      },
    })
  }
})