goldArticleSearch.js 3.1 KB
// pages/index/goldArticleSearch/goldArticleSearch.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '' ,//1童子功,2背单词
    historydata:'',
    keyword:''
  },
  //搜索 
  gooutcome(){
    let that=this;
    if(that.data.keyword==''){
      wx.showToast({
        title: '请输入关键词',
        icon: 'none',
        duration: 2000,
      })
    }else{
      let url = 'Search/getSearchData';
      let params = {
        token: wx.getStorageSync("token"),
        keyword: that.data.keyword,
        type: that.data.type
      }
      app.post(url, params).then((res) => {
        if (res.data.code == 1) {
          if(res.data.data != null){
            wx.setStorageSync('searchend', res.data.data)
            if(that.data.type==1){
              wx.navigateTo({
                url: '../searchlist/searchlist',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }else if(that.data.type==2){
              wx.navigateTo({
                url: '../../MemorizingWords/searchwords/searchwords',
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
              })
            }
          }else{
            console.log('123456789');
            wx.showToast({
              title: '暂无结果',
              icon: 'none',
              duration: 2000,
            })
          }
        }
      }).catch((errMsg) => {
        console.log(errMsg)
      })
    }
    
  
  },
  setkeyword(e){
    let that=this;
    that.setData({
      keyword: e.detail.value
    })
  },
  //点击历史搜索
  setlabel(e){
    let that=this;
    that.setData({
      keyword: e.currentTarget.dataset.keyword
    })
    that.gooutcome();

  },
  //获取历史搜索数据
  getdata() {
   
    let that = this;
    let url = 'Search/getHistoryData';
    let params = {
      token: wx.getStorageSync('token'),
      type: that.data.type
    }
    app.post(url, params).then((res) => {
      if (res.data.code == 1) {
        that.setData({
          historydata: res.data.data
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    that.setData({
      type:options.type
    })
    that.getdata();
  },

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

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

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

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

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

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

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