search.js 5.8 KB
// pages/search/search.js
const app = getApp()
import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    list: [],
    inputVal: '',
    searchRecord: [],
    showDialog: false,
    messageAlert: '',
    msg: '',
    content: '',
    page: 1,
    searchValue: '',
    S4_num: '',
  },
  onShow: function () {
    let that = this
    let token = wx.getStorageSync('token')
    setTimeout(() => {
      if (token) {
        that.getList()
      } else {
        that.setData({
          content: "您尚未登录,请登录后操作"
        })
        that.popConfirm()
      }
    }, 1300);

  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      S4_num: options.S4_num || ''
    })
  },
  openHistorySearch() {
    this.setData({
      searchRecord: wx.getStorageSync('searchRecord') || []
    })
  },
  // 删除
  async delClear() {
    let that = this
    try {
      const {
        data
      } = await request({
        url: 'api/shop/delSou'
      })
      console.log(data);
      that.setData({
        msg: data.msg,
        searchRecord: []
      })
      that.popMaskTest()
      setTimeout(() => {
        that.getList()
      }, 1000);

    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }

  },


  // 获取input内容
  getInput(e) {
    this.setData({
      searchValue: e.detail.value
    })
  },

  // 点击搜索按钮
  async searchInput() {
    let that = this;
    if (that.data.searchValue != '') {
      if (that.data.S4_num == 1) {
        wx.navigateTo({
          url: '/pages/Vmore4SList/Vmore4SList?kw=' + that.data.searchValue
        })
      } else {
        wx.navigateTo({
          url: '/pages/searchList/searchList?kw=' + that.data.searchValue + '&change=1'
        })
      }

      that.setData({
        page: 1,
        list: [],
        searchValue: '',
      })
    } else {
      that.setData({
        msg: '请输入搜索内容'
      })
      that.popMaskTest()
    }

  },

  //点击搜索按钮提交表单跳转并储存历史记录
  async searchSubmitFn(e) {
    console.log(e, "输入框数据");
    let that = this
    let searchRecord = that.data.searchRecord || []
    let inputVal = e.detail.value
    if (inputVal == '') {
      //输入为空时的处理 
      that.setData({
        messageAlert: "您未输入内容请输入搜索信息",
        showDialog: !this.data.showDialog
      })
    } else {
      if (searchRecord.length < 5) {
        searchRecord.unshift({
          value: inputVal,
          id: searchRecord.length
        })
      } else {
        searchRecord.pop() //删掉旧的时间最早的第一条
        searchRecord.unshift({
          value: inputVal,
          id: searchRecord.length
        })
      }
      this.setData({
        searchRecord: searchRecord,
        searchValue: ''
      })
      wx.setStorageSync('searchRecord', searchRecord)

      const {
        data: {
          data
        }
      } = await request({
        url: 'api/shop/sou',
        data: {
          kw: inputVal,
          page: that.data.page,
          time_id: 2,
          price_id: 2
        }
      })
      console.log(data);
      if (data.length > 0) {
        that.setData({
          list: data
        })
      } else {
        that.setData({
          msg: '没有找到与' + '"' + inputVal + '"' + '相关的结果'
        })
        that.popMaskTest()
      }


    }
  },
  popMaskTest() {
    wx.showToast({
      title: this.data.msg,
      duration: 1300,
      icon: 'none',
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },

  // 点击搜索历史进行搜索
  toSearch(e) {
    let that = this;
    console.log(e);
    let kw = e.currentTarget.dataset.value
    if (that.data.S4_num == 1) {
      wx.navigateTo({
        url: '/pages/Vmore4SList/Vmore4SList?kw=' + kw
      })
    } else {
      wx.navigateTo({
        url: '/pages/searchList/searchList?kw=' + kw + '&change=1'
      })
    }

    that.setData({
      searchValue: ''
    })
  },
  //  获取搜索记录
  getList() {
    let that = this
    request({
        url: 'api/shop/souLog',
      })
      .then(res => {
        if (res.data.data.length > 0) {
          that.setData({
            searchRecord: res.data.data
          })
          console.log(this.data.searchRecord, res.data.data, "搜索")
        } else {
          that.setData({
            msg: '暂无搜索记录'
          })
          that.popTest()
        }
      }).catch(err => {
        console.log(err);
        that.setData({
          msg: err.msg
        })
        that.popTest()
      })
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 1300
    })
  },
  popConfirm() {
    wx.showModal({
      title: '提示',
      content: this.data.content,
      success: function (res) {
        if (res.confirm) {
          console.log('点击确认回调')
          wx.navigateTo({
            url: '/pages/authorization/authorization?num=1'
          })
        } else {
          console.log('点击取消回调')
        }
      }
    })
  },

  history() {
    //将搜索值放入历史记录中,只能放前五条this.data
    request({
        // url: 'api/shop/sou',
        data: {
          kw: inputVal
        }
      })
      .then(res => {
        console.log(res);
        this.setData({
          searchRecord: res.data.data
        })
        console.log(that.data.searchRecord, "搜索")
      }).catch(err => {
        console.log(err);
      })

  },

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

  }
})