shop_detail.js 4.8 KB
const WxParse = require('../../wxParse/wxParse.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1, //数量
    current: 0,
    status: 0,
    nav: ['商品介绍', '规格参数', '售后保障'],
    mask: false
  },
  num_change(e) {
    let num = Number(this.data.num)
    let type = e.currentTarget.dataset.type
    if (type == 1) {
      num++
      if (num > Number(this.data.info.sold)) {
        wx.showToast({
          title: '已到最大库存',
          icon: 'none'
        })
        return
      }
    } else {
      num--
      if (num < 1) {
        wx.showToast({
          title: '不能再少了',
          icon: 'none'
        })
        return
      }
    }
    this.setData({
      num: num
    })
  },
  //添加购物车
  addshop() {
    let that = this;
    if(that.data.info.sold==0){
      wx.showToast({
        title: '库存不足,不能加入购物车',
        icon:"none"
      })
      return false
    }
    let url = app.interface.addshop
    let params = {
      num: that.data.num,
      product_id: that.data.info.id
    }
    app.post(url, params).then((res) => {
      that.setData({
        "info.shop": Number(that.data.info.shop) + that.data.num,
        num: 1
      })
      wx.showToast({
        title: '加入成功',
        duration: 1500
      })
    })
  },
  // 立即购买
  purchaseNow() {
    let that = this;
    if (app.globalData.status==0){
      wx.showToast({
        title: '您已被拉黑',
        icon:'none'
      })
      return false
    }
    if (that.data.info.sold==0){
      wx.showToast({
        title: '库存不足,不能下单',
        icon:'none'
      })
      return false
    }
    console.log(that.data.info)
    let info = {
      shop_id: that.data.info.id,
      num: that.data.num
    }
    wx.navigateTo({
      url: '/packageA/pages/order/order?type=3' + '&info=' + JSON.stringify(info) + '&id=' + that.data.info.id
    })
  },
  //去查看购物车
  go_car() {
    wx.redirectTo({
      url: '/packageA/pages/shop_cart/shop_cart'
    })
  },
  //收藏
  colloct() {
    let url = app.interface.collect
    app.post(url, {
      id: this.data.info.id
    }).then((res) => {
      wx.showToast({
        title: !this.data.info.colloct?'已收藏':'取消收藏',
        icon: 'none',
        duration: 1000
      })
      this.setData({
        "info.colloct": !this.data.info.colloct
      })
    })
  },
  //轮播点
  getCurrent(e) {
    var current = e.detail.current
    this.setData({
      current
    })
  },
  //点击下面的导航
  clickNav(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      status: index
    })
  },

  change_cover(e) {
    if( app.globalData.status==0){
      wx.showToast({
        title: '您已被拉黑',
        icon:'none'
      })
      return false
    }
    if (e.currentTarget.dataset.type) {
      this.setData({
        btn_type: e.currentTarget.dataset.type
      })
    }
    this.setData({
      mask: !this.data.mask
    })
  },



  // 获取页面信息
  get_info(id) {
    let url = app.interface.getInfo
    let params = {
      id: id
    }
    app.post(url, params).then((res) => {
      console.log('9999',res)
      this.setData({
        info: res.msg
      })
      WxParse.wxParse('contenta', 'html', res.msg.maincontent, this, 5)
      WxParse.wxParse('contenb', 'html', res.msg.guigecontent, this, 5)
      WxParse.wxParse('contenc', 'html', res.msg.shouhoucontent, this, 5)
    })
  },
  //去登录
  go_login() {
    wx.showModal({
      title: '提示',
      content: '请先登录',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/register/register',
          })
        } else if (res.cancel) {
        }
      }
    })
  },

  //判断是否登录
  judge_login() {
    if (!wx.getStorageSync("token") && (wx.getStorageSync('login') == 0 || !wx.getStorageSync('login'))) {
      this.setData({
        no_login: true
      })
    } else {
      this.setData({
        no_login: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.get_info(options.id)
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 登录判断
    this.judge_login()


  },

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

  },

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

  },

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

  },

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

  },

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

  }
})