sousuo.js 5.9 KB
// pages/sousuo/sousuo.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    textarr: [],
    current: 0,
    imgUrls: [1, 2, 3],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    currentSwiper: 0,
    showlabel: false,
    keyword: '',
    //历史记录
    historylist: [],
    noticelist: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  //获取第一个公告列表
  getonedenglu() {

    let url = 'index/firstNoticeList'
    app.post(url, '', 'get').then((res) => {
      console.log(res);
      this.setData({
        noticelist: res
      })
    }).catch((err) => {

    })
  },

  //获取模糊搜索下拉框
  getmohu() {
    let url = 'index/getLikeList';
    let param = {
      keyword: this.data.keyword
    }
    app.post(url, param, 'get').then((res) => {
      console.log(res);
      this.setData({
        textarr: res
      })
    }).catch((err) => {

    })
  },

  //获取历史记录
  gethistorylist() {
    let that = this;
    let url = 'index/getHistoryList';

    app.post(url, '', 'get').then((res) => {
      console.log(res);

      for (var obj of res) {
        obj.sel = false
      }
      this.setData({
        historylist: res
      })

    }).catch((err) => {
      console.log(err)
      if (err.code == 0) {
        that.setData({
          historylist: []
        })
      }
    })
  },

  //长按事件
  handleLongPress(e) {

    let token = wx.getStorageSync("token");
    if (token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })

      setTimeout(function() {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1500)
    } else {
      let index = e.currentTarget.dataset.index;
      console.log(index)
      let newhistorylist = this.data.historylist;
      for (var i = 0; i < newhistorylist.length; i++) {
        if (i == index) {
          newhistorylist[i].sel = true
        }
      }

      this.setData({
        historylist: newhistorylist
      })

    }

  },
  //删除全部历史记录
  delhistory() {
    let that = this;
    let token = wx.getStorageSync("token")
    if (token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })

      setTimeout(function() {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1500)
    } else {
      let url = 'index/clearHistory'
      app.post(url, '', 'get').then((res) => {

        wx.showToast({
          title: '删除成功',
          icon: "none"
        })

        setTimeout(function() {
          that.gethistorylist()
        }, 1000)

      }).catch((err) => {

      })
    }


  },

  //删除单个历史记录
  shanhistory(e) {
    let that = this;
    let name = e.currentTarget.dataset.name;
    console.log(name)
    let url = 'index/delHistory'

    let param = {
      history_name: name
    }
    app.post(url, param, 'get').then((res) => {

      wx.showToast({
        title: '删除成功',
        icon: "none"
      })

      setTimeout(function() {
        that.gethistorylist()
      }, 1000)

    }).catch((err) => {

    })
  },

  //进入页面
  historyname(e) {
    let name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/gouwucart/gouwucart?name=' + name,
    })
  },

  //搜索页面
  soucontent() {
    wx.navigateTo({
      url: '/pages/gouwucart/gouwucart?name=' + this.data.keyword,
    })
  },



  // 轮播图滚动
  swiperChange: function(e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  texttouch(e) {
    this.setData({
      current: e.currentTarget.dataset.index
    })

    let name = e.currentTarget.dataset.name

    wx.navigateTo({
      url: '/pages/gouwucart/gouwucart?name=' + name,
    })
  },

  //显示文本内容
  showsearch() {
    this.setData({
      showlabel: true
    })
  },
  showsearch() {
    let token = wx.getStorageSync("token");
    if (token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })

      setTimeout(function() {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1500)
    }
  },

  //输入文本内容
  entersearch(e) {


    this.setData({
      keyword: e.detail.value
    })

    this.getmohu()

    if (e.detail.value == '') {
      this.setData({
        showlabel: false
      })
    } else {
      this.setData({
        showlabel: true
      })
    }


  },

  //隐藏更多
  hidegoodshow() {
    this.setData({
      showlabel: false
    })
  },
  //查看更多
  chaknamore() {

    let token = wx.getStorageSync("token");
    if (token == '') {

      wx.showToast({
        title: '请先登录',
        icon: "none"
      })

      setTimeout(function() {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1500)

    } else {
      wx.navigateTo({
        url: '/pages/seemore/seemore',
      })
    }

  },



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

  },
  toObj(arr) {
    var result = {};
    var p = [];
    for (var a = 0; a < arr.length; a++) {
      result[a] = arr[a];
      p.push(result)
    }
   
    return p;
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getonedenglu();
    //获取历史记录
    this.gethistorylist();

    let arr = ['1', '2', '3', '4'];
    console.log(this.toObj(arr))
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

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

  }
})