taocanpay.js 6.5 KB
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    success: false,
    fail: false,
    name: '',

    addressId: 0,
    tel: '',
    area: '',
    address: '',
    cover_type: false,
    totalmoney:''
  },
  //加减商品数量
  num_change(e) {
    let that = this
    let num = Number(e.currentTarget.dataset.num)
    let type = e.currentTarget.dataset.type
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let check = e.currentTarget.dataset.check
    let coudan = this.data.coudan
    let coudan_arr = this.data.coudan_arr
    if (type == 1) {
      num++
    } else {
      num--
      if (num < 1) {
        return
      }
    }
    that.setData({
      [`info.coudanProduct[${index}].num`]: num
    })
    if (check) {
      coudan.forEach((item, index) => {
        if (item.id == id) {
          item.num = num
          coudan_arr[index].num = num
        }
      })
      this.get_choudan()
    }
  },
  // 计算凑单费用
  get_choudan() {
    let coudan_arr = this.data.coudan_arr
    let choudan_money = 0
    for (let obj of coudan_arr) {
      choudan_money = Number((obj.price * 100 * obj.num)) / 100 + Number(choudan_money) 
    }
    this.setData({
      choudan_money: choudan_money
    })
  },
  show_cover() {
    let that = this
    if (!that.data.cover_type) {
      // if (that.data.info.status == 0) {
      //   that.setData({
      //     fail: true
      //   })
      //   return
      // }
      if (!that.data.info.address) {
        wx.showToast({
          title: '请添加地址',
          icon: 'none',
          duration: 1000
        })
        return
      }
    }
    that.setData({
      cover_type: !this.data.cover_type
    })
  },
  // 去添加地址
  add_address() {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },


  //选择凑单商品
  chooseGoods(e) {
    // coudan 凑单商品组成的数组
    // coudan_type 凑单的状态  可能废弃
    let that = this
    let coudan = this.data.coudan ? this.data.coudan : []
    let coudan_arr = this.data.coudan_arr ? this.data.coudan_arr : []
    let check = e.currentTarget.dataset.check
    let num = e.currentTarget.dataset.num
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let price = e.currentTarget.dataset.price
    if (check) {
      check = 0
      coudan.forEach((item, index) => {
        if (item.id == id) {
          coudan.splice(index, 1)
          coudan_arr.splice(index, 1)
        }
      })
    } else {
      check = 1
      //选中
      coudan.push({
        id: id,
        num: num
      })
      coudan_arr.push({
        id: id,
        num: num,
        price: Number(price)
      })
    }
    that.setData({
      coudan: coudan,
      coudan_arr: coudan_arr,
      [`info.coudanProduct[${index}].check`]: check
    })
    that.get_choudan()
  },

  //获取页面信息
  get_info() {
    let that = this
    let url = '/combo/affirm_order'
    console.log(that.data.params)
    
    let params = {
      combo_id:that.data.params.shop_id
    }
    app.post(url, params).then((res) => {
      console.log(res)
      that.setData({
        info: res.msg,
        totalmoney: (Number(res.msg.total) + Number(res.msg.yunfei * 100 / 100)).toFixed("1")
      })
      console.log('999',that.data.totalmoney)

      // that.info.total = Number(that.data.info.total)
      // that.info.yunfei = Number(that.data.info.yunfei);
      // that.setData({
      //   info:that.data.info
      // })
    })
  },


  //获取页面信息
  get_youfei(id) {
    let that = this
    let url = that.data.type == 4 ? app.interface.youfei1 : app.interface.youfei
    let params = {
      info: JSON.parse(that.data.params),
      address_id: id
    }
    app.post(url, params).then((res) => {
      that.setData({
        "info.yunfei": res.msg.yunfei
      })
    })
  },


  //点击支付
  submit() {
    let that = this
    let list = that.data.info.products
    let coudan = that.data.coudan
    let info = []
    if (that.data.type == 4) {
      for (let obj of list) {
        info.push({
          id: obj.product.id,
          num: obj.product.num
        })
      }
    } else {
      for (let obj of list) {
        info.push({
          id: obj.id,
          num: obj.num
        })
      }
    }
    if (coudan && coudan.length > 0) {
      info = info.concat(coudan)
    }
    let url = '/combo/create_order'
    let params = {
      address: that.data.info.address.id,
      combo_id: that.data.params.shop_id
    }
    app.post(url, params).then((res) => {
      that.setData({
        cover_type: false
      })
      that.order_pay(res.msg)
    })
  },

  //订单支付
  order_pay(order_odd) {
    let url = app.interface.order_pay
    let params = {
      order_odd: order_odd
    }
    app.post(url, params).then((res) => {
      this.setData({
        order: res.msg
      })
      if (res.msg.message == "支付成功") {
        this.setData({
          success: true
        })
      } else if (res.msg.message == "余额不足") {
        this.setData({
          fail: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      type: options.type,
      params: JSON.parse(options.info)
    })
    this.get_info(options.type, options.info, options.id)
  },


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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]
    if (currPage.data.id) {
      this.setData({
        "info.address.address": currPage.data.address,
        "info.address.area": currPage.data.area,
        "info.address.id": currPage.data.id,
        "info.address.tel": currPage.data.tel,
        "info.address.name": currPage.data.name
      })
      this.get_youfei(currPage.data.id)
    }
    if (wx.getStorageSync('no_address')) {
      wx.setStorageSync('no_address', false)
      this.setData({
        "info.address": null
      })
    }

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})