searchList.js 3.6 KB
import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    searchStoreList: [],
    size: 5,
    nominate_switch: ' ', // 推荐 
    showDialog: false,
    time_id: '', //上架时间:2=由新到旧,1=由旧到新[非必传]
    price_id: '', //价格:2=由高到低,1=由低到高[非必传]
    kw: '',
    msg: '',
    limit: 11, //显示数据量
    list: '',
    page: 1, //当前页
    load: true,
    loading: false, //加载动画的显示
    all: 0,
    total: '',
    pageNum: 1,
    nomore: false

  },
  onShow: function () {},
  onLoad: function (options) {
    let that = this
    console.log(options);
    that.setData({
      kw: options.kw || ''
    })
    wx.stopPullDownRefresh() //刷新完成后停止下拉刷新动效
    that.getSearch()
  },
  onReachBottom() {
    var that = this;
    let pageNum = that.data.pageNum
    if (that.data.searchStoreList.length < that.data.total) {
      pageNum++
      that.setData({
        pageNum,
        nomore: false
      })
      that.getSearch()
    } else {
      that.setData({
        nomore: true
      })
    }
  },
  getInput(e) {
    this.setData({
      searchValue: e.detail.value
    })
  },
  async getSearch(e) { // 获取搜索
    let that = this;
    if (e) {
      that.setData({
        kw: e.detail.value.input ? e.detail.value.input : e.detail.value,
        searchStoreList: []
      })
    }
    wx.showLoading({
      title: '加载中',
    })
    try {
      const {
        data: {
          data
        }
      } = await
      request({
        url: 'api/shop/sou',
        data: {
          price_id: that.data.price_id,
          time_id: that.data.time_id,
          kw: that.data.kw,
          page: that.data.pageNum
        }
      })
      console.log(data);
      if (data.data.length != 0) {
        that.setData({
          total: data.total,
          searchStoreList: [...that.data.searchStoreList, ...data.data],
        })
        wx.hideLoading()
      } else {
        if (that.data.kw) {
          that.setData({
            msg: '没有找到与' + '"' + that.data.kw + '"' + '相关的结果',
            kw: '',
          })
          a.popTest()
          wx.hideLoading()
        } else {
          a.popTest('暂无数据')
          wx.hideLoading()
        }
      }
    } catch (err) {
      console.log(err);
      a.popTest(err.msg)
    }
  },
  topTime() { //上架时间
    let that = this
    let time_id = that.data.time_id
    if (time_id == 2) {
      that.setData({
        time_id: 1,
        page: 1,
        searchStoreList: []
      })
      that.getSearch()
    } else {
      that.setData({
        time_id: 2,
        page: 1,
        searchStoreList: []
      })
      console.log(that.data.time_id);
      that.getSearch()
    }
  },
  topPrice() { // 价格
    let that = this
    let price_id = that.data.price_id
    if (price_id == '') {
      that.setData({
        price_id: 1,
        page: 1,
        searchStoreList: []
      })
      that.getSearch()
    } else if (price_id == 1) {
      that.setData({
        price_id: 2,
        page: 1,
        searchStoreList: []
      })
      that.getSearch()
    } else if (price_id == 2) {
      that.setData({
        price_id: 1,
        page: 1,
        searchStoreList: []
      })
      that.getSearch()
    }
  },
  goDeailItem(e) { // 普通商品详情
    let goods_id = e.currentTarget.dataset.goods_id
    wx.navigateTo({
      url: '/pages/productdetailsImg/productdetailsImg?id=' + goods_id
    })
  },
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    })
  },





})