search.js 3.1 KB
// pages/search/search.js
let method = require("../../utils/reuqest.js")
let obj = {};
let list = [];
Page({
  data: {
    goodsId: '',
    searchBox: true,
    keyWord: '',
    labelArray: [],  //搜索历史
    title: '猜你喜欢',
    list: [],
    bottomHint:false  //触底提示
  },
  //获取输入的关键字
  onChange(e) {
    this.setData({
      keyWord: e.detail
    })
    console.log(this.data.keyWord, 'keys')
  },
  //搜索
  search() {
    wx.navigateTo({
      url: '/pages/searchRequest/searchRequest?keyword=' + this.data.keyWord,
    });
  },
  onCancel() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  cancel() {
    this.setData({
      keyWord: '',
    })
  },
  unsearch() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  searchHistory() {
    method.getRequest("/coupon/historySearch", data => {
      if (data.statusCode == 0) {
        this.setData({
          labelArray: data.data.slice(-12)
        })
      }
    })
  },
  delSearch() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除所有历史记录吗?',
      success(res) {
        if (res.confirm) {
          method.deleteRequest("/coupon/deleteHistory", data => {
            if (data.statusCode == 0) {
              that.setData({
                labelArray: []
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  historyClick(e) {
    console.log(e, 'e')
    let idx = e.currentTarget.dataset.index;
    let list = this.data.labelArray;
    list.forEach((item, index) => {
      if (idx == index) {
        this.setData({
          keyWord: list[idx]
        })
      }
    })
  },
  // 猜你喜欢
  youLike() {
    let postData = {
      currPage: 1,
      pageSize: 10
    }
    method.postRequest("/goods/youMayAlsoLike", postData, data => {
      if (data.statusCode == 0) {
        this.setData({
          list: data.data
        })
      }
    })
  },
  goGoodsDetail(e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      goodsId: id
    })
    wx.navigateTo({
      url: '/pages/product-detail/product-detail?goodsId=' + this.data.goodsId,
    })
  },
  onLoad: function (options) {
    let that = this;
    that.youLike();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.searchHistory();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      bottomHint: true
    })
    setTimeout(() => {
      this.setData({
        bottomHint: false
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})