goAccessories.js 4.6 KB
import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    accessories: [],
    showDialog: false,
    time_id: 2, //上架时间:2=由新到旧,1=由旧到新[非必传]
    price_id: 1, //价格:2=由高到低,1=由低到高[非必传]
    msg: '',
    kw: '',
    noCar: '',
    s4_id: '',
  },

  onShow: function () {
    let that = this
    let token = wx.getStorageSync("token")
    if (token) {
      setTimeout(() => {
        that.getAccessories()
      }, 1000);
    } else {
      that.popConfirm()
    }
  },
  onLoad: function (options) {
    this.setData({
      s4_id: options.id
    })
    console.log(options);
  },
  async getAccessories() { // 获取精品配件
    let that = this;
    try {
      const {
        data: {
          data
        }
      } = await
      request({
        url: 'api/shop4s/peiList',
        data: {
          s4_id: that.data.s4_id
        }
      })
      console.log(data);
      if (data.length > 0) {
        that.setData({
          accessories: data,
          noCar: 0
        })
      } else {
        that.setData({
          noCar: 1
        })
      }

    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }
  },
  topTime() { //上架时间
    let that = this
    let time_id = that.data.time_id
    if (time_id == 2) {
      that.setData({
        time_id: 1
      })
      that.getAccessories()

    } else {
      that.setData({
        time_id: 2
      })
      console.log(that.data.time_id);
      that.getAccessories()
    }
  },
  topPrice() { // 价格
    let that = this
    let price_id = that.data.price_id
    if (price_id == 2) {
      that.setData({
        price_id: 1
      })
      that.getAccessories()
    } else {
      that.setData({
        price_id: 2
      })
      that.getAccessories()
    }
  },
  storeDetail(e) {
    console.log(e, "普通商品详情");
    let id = e.currentTarget.dataset.id
    let img = e.currentTarget.dataset.img
    if (img == 1) {
      wx.navigateTo({
        url: '/pages/goAccessoriesDetailIMG/goAccessoriesDetailIMG?id=' + id
      })
    } else {
      // 无图详情
      wx.navigateTo({
        url: '/pages/goAccessoriesDetail/goAccessoriesDetail?id=' + id
      })
    }

  },
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    })
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 1300
    })
  },
  async searchSubmitFn(e) {
    // console.log(e, "输入框数据");
    let that = this
    let inputVal = e.detail.value
    if (inputVal == '') {
      that.setData({
        msg: "您未输入内容",
      })
      that.popMaskTest()
    } else {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/shop4s/souPei',
        data: {
          kw: inputVal,
          time_id: that.data.time_id,
          price_id: that.data.price_id,
        }
      })
      console.log(data);
      if (data.length > 0) {
        that.setData({
          list: data,
          kw: '',
        })
      } else {
        that.setData({
          msg: '没有找到与' + '"' + inputVal + '"' + '相关的结果',
          kw: '',
        })
        that.popMaskTest()
      }
    }
  },
  popMaskTest() {
    wx.showToast({
      title: this.data.msg,
      duration: 1300,
      icon: 'none',
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },
  onShareAppMessage(options) {
    var that = this;
    console.log(that.data.user_id)
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: "车唛分享", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/goAccessories/goAccessories?id=' + that.data.s4_id, // 默认是当前页面,必须是以‘/’开头的完整路径
      imageUrl: '', //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4

    };
    // 返回shareObj
    return shareObj;

  },
  popConfirm: function () {
    wx.showModal({
      title: '提示',
      content: '您尚未登录,请登录后操作',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/authorization/authorization'
          })
        } else {
          wx.switchTab({
            url: '/pages/home/home'
          })
        }
      }
    })

  },

})