search.js 3.3 KB
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')
    if (token) {
      if (that.data.S4_num == '') {
        that.getList()
      }
    } else {
      that.popConfirm() // 未登录
    }
  },
  onLoad: function (options) {
    //  console.log(options);
    this.setData({
      S4_num: options.S4_num || '' // 首页进来传递的值  其他页面没有
    })
  },
  async getList() { //  获取搜索记录
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/shop/souLog',
      })
      //  console.log(data, "搜索历史");
      that.setData({
        searchRecord: data
      })
    } catch (err) {
      //  console.log(err);
      a.popTest(err.msg)
    }
  },
  async delClear() { // 删除
    let that = this
    try {
      const {
        data
      } = await request({
        url: 'api/shop/delSou'
      })
      //  console.log(data);
      a.popSuccessTest(data.msg)
      setTimeout(() => {
        that.getList()
      }, 1000);
    } catch (err) {
      //  console.log(err);
      a.popTest(err.msg)
    }
  },
  getInput(e) { // 获取input内容
    this.setData({
      searchValue: e.detail.value
    })
  },
  async searchInput() { // 点击搜索按钮
    let that = this;
    if (that.data.searchValue != '') {
      //  console.log(that.data.searchValue, "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({
        searchValue: '',
      })
    } else {
      a.popTest('请输入搜索内容')
    }
  },
  popMaskTest() {
    wx.showToast({
      title: this.data.msg,
      duration: 1300,
      icon: 'none',
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },
  toSearch(e) { // 点击搜索历史进行搜索
    let that = this;
    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: ''
    })
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 1300
    })
  },
  popConfirm() {
    wx.showModal({
      title: '提示',
      content: '您尚未登录,请登录后操作',
      success: function (res) {
        if (res.confirm) {
          //  console.log('点击确认回调')
          wx.navigateTo({
            url: '/pages/authorization/authorization?num=1'
          })
        } else {
          //  console.log('点击取消回调')
        }
      }
    })
  },
})