charge.js 4.8 KB
// pages/mall/charge/charge.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id: '',
    orderInfo: '',
    goods_num: '',
    price: '',
    total_price: '',
    remark: '',
    address_list: ''

  },
  //商品数量
  getNum(e) {
    this.setData({
      goods_num: e.detail.value
    })
    this.getTotalPrice()
  },
  //计算总额
  getTotalPrice() {
    var goods_num = this.data.goods_num
    var price = this.data.price
    this.setData({
      total_price: goods_num * price
    })
  },
  //备注
  remark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  //商品详情
  goodsDetail() {
    let url = 'portal/Goods/goodsOrder'
    let params = {
      id: this.data.goods_id,
    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
    }
    app.post(url, params, header).then((res) => {
      console.log(res)
      if (res.data.code == 20000) {
        this.setData({
          orderInfo: res.data.data,
          price: res.data.data.price
        })
      }
      this.getPrice()
    })
  },
  //默认数量1
  getPrice() {
    var count = this.data.goods_num
    if (count == '') {
      this.setData({
        goods_num: 1
      })
      this.getTotalPrice()
    }
  },
  //地址管理
  addressList() {
    var goods_id = this.data.goods_id
    wx.navigateTo({
      url: '../addressManagement/addressManagement?goods_id=' + goods_id,
    })
  },

  //获得地址
  getAddress() {
    let url = 'portal/Goods/address'
    let params = {}
    let header = {
      "XX-Token": wx.getStorageSync('token'),
    }
    app.post(url, params, header).then((res) => {
      let address = res.data.data
      for (let i = 0; i < address.length; i++) {
        if (address[i].status == 1) {
          this.setData({
            address_list: address[i]
          })
        }
      }
      console.log(this.data.address_list)
    })
  },

  //地址详情
  getAddressDetail() {
    let url = 'portal/Goods/addressChoose'
    let params = {
      id: this.data.address_id
    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
    }
    app.post(url, params, header).then((res) => {
      this.setData({
        address_list: res.data.data
      })
      console.log(this.data.address_list)
    })
  },

  //立即兑换
  goBuy(e) {
    var address_id = e.currentTarget.dataset.address_id
    if (this.data.goods_num <= 0) {
      wx.showToast({
        title: '请输入正确数量',
        icon: 'none'
      })
    } else if (address_id == '') {
      wx.showToast({
        title: '请添加地址',
        icon: 'none'
      })
    } else {
      let url = 'portal/Goods/orderPost'
      let params = {
        goods_id: e.currentTarget.dataset.goods_id,
        address_id: address_id,
        remark: this.data.remark,
        count: this.data.goods_num,
        price: this.data.total_price
      }
      let header = {
        "XX-Token": wx.getStorageSync('token'),
      }
      app.post(url, params, header).then((res) => {
        console.log(res)
        if (res.data.code == 40000) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        } else if (res.data.code == 40001) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        } else if (res.data.code == 40002) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        } else {
          wx.navigateTo({
            url: '../success/success',
          })
          // wx.showToast({
          //   title: '购买成功',
          //   success: function() {
          //     setTimeout(function() {
          //       wx.switchTab({
          //         url: '../../mall/mall',
          //       })
          //     }, 2000)
          //   }
          // })
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      goods_id: options.goods_id,
      address_id: options.address_id
    })
    console.log(this.data.address_id)
    if (options.address_id) {
      this.getAddressDetail()
    } else {
      this.getAddress()
    }
    this.goodsDetail()


  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.goodsDetail()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})