firm_order1.js 7.4 KB
// pages/kind/firm_order1/firm_order1.js
const app = getApp();
Page({
  // 点击立即支付
  goFirm_order2: function (e) {

  },
  /**
   * 页面的初始数据
   */
  data: {
    shop_id: null,
    order_list: {},
    shop_numb:'',
    goods: '',
    address_id: '',
    text:'',
    address:false,
    shopinfo:true,
    shopnum:'',
    days: '',
    hours: '',
    minutes: '',
    seconds: '',
    navH: '',
    nav:'',
    addressid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.nav!=undefined){
        this.setData({
          nav:1
        })
    }

    if(options.addressid!=undefined){
      this.setData({
        addressid:options.addressid
      })
    }
   
    console.log(options.num)
    this.setData({
      shop_id: options.id,
      shopnum:options.num,
     
    })

    this.setData({
      navH: app.globalData.navHeight
    })
    console.log(options.id)
    
  },

  go() {
    if (this.data.nav == 1) {
      wx.switchTab({
      
        url: '/pages/homeindex/homeindex',
      })
    } else {
      wx.navigateBack({
        checked: true
      })
    }

  },
  //输入备注
  entertext(e){
    this.setData({
      text:e.detail.value
    })
  },
  //选择地址
  selectplace(){
    wx.navigateTo({
      url: '/pages/mine/addr_1/addr_1?shop_id='+this.data.shop_id+'&shopnum='+this.data.shopnum,
    })
    wx.setStorageSync('shopid', this.data.shop_id)
    wx.setStorageSync('shopnum', this.data.shopnum)
  },

  //添加收货地址
  addplace(){
    wx.navigateTo({
      url: '/pages/mine/addr_2/addr_2?noorder=' + 1 + '&noorderid=' + this.data.shop_id + '&num=' + this.data.shopnum,
    })
  },

  // 确认订单1数据
  order() {
    let that = this;
    let url = "home/index/goods";
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      "XX-Device-Type": 'wxapp'
    }
    let params = {
      shop_id: that.data.shop_id,
      shop_numb: that.data.shopnum,
      address_id:that.data.addressid
    }
    app.post(url, params, header).then((res) => {
      console.log(res)
      if(res.data.address_id==0){
        that.setData({
          address: true,
        })
      }
      that.setData({
        order_list: res.data,
        shop_numb: res.data.shop_numb,
        goods: res.data.goods,
        address_id: res.data.address_id,
        text: that.data.text,
        
        shopinfo:true
      })

      let number = res.data.shop.end_time - res.data.now_time;
      console.log(number)
      // let number = res.over_time;
      // 时间戳处理
      var totalSecond = number;
      var interval = setInterval(function () {
        // 秒数
        var second = totalSecond;
        // 天数位
        var day = Math.floor(second / 3600 / 24);
        var dayStr = day.toString();
        if (dayStr.length == 1) dayStr = '0' + dayStr;
        // 小时位
        var hr = Math.floor((second - day * 3600 * 24) / 3600);
        var hrStr = hr.toString();
        if (hrStr.length == 1) hrStr = '0' + hrStr;
        // 分钟位
        var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
        var minStr = min.toString();
        if (minStr.length == 1) minStr = '0' + minStr;
        // 秒位
        var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
        var secStr = sec.toString();
        if (secStr.length == 1) secStr = '0' + secStr;

        that.setData({
          days: dayStr,
          hours: hrStr,
          minutes: minStr,
          seconds: secStr,
        });
        totalSecond--;

        if (totalSecond <= 0) {
          clearInterval(interval);
          wx.showToast({
            title: '已结束',
            icon: 'none'
          });
          // 倒计时结束
          //that.countDown();
          that.setData({
            days: '00',
            hours: '00',
            minutes: '00',
            seconds: '00',
          });
        }
      }.bind(this), 1000);

      console.log(that.data.order_list)
    }).catch((err) => {
      console.log(err)
      that.setData({
        shopinfo:false
      })
      if (err.msg =="请添加收货地址"){
        wx.showModal({
          title: '提示',
          content: err.msg,
          showCancel: false,
          success: function (res) {
            console.log(res)
            if (res.confirm) {
              
              wx.navigateTo({
                url: '/pages/mine/addr_2/addr_2?noorder=' + 1 + '&noorderid=' + that.data.shop_id + '&num=' + that.data.shopnum,
              })
            }
          }
        })

        // that.setData({
        //   address:true
        // })
      } else if (err.msg == "商品已到时"){
        wx.showModal({
          title: '提示',
          content: err.msg,
          showCancel: false,
          success: function (res) {
            console.log(res)
            if (res.confirm) {
             wx.navigateBack({
               checked:true
             })
            }
          }
        })
      } else if (err.msg == "商品已成团"){
        wx.showModal({
          title: '提示',
          content: err.msg,
          showCancel: false,
          success: function (res) {
            console.log(res)
            if (res.confirm) {
              wx.navigateBack({
                checked: true
              })
            }
          }
        })
      }
     
      // wx.showToast({
      //   title: err.msg,
      //   icon:'none'
      // })

      // setTimeout(function(){
      //   wx.navigateBack({
      //     checked:true
      //   })
      // },1500)
    })
  },
  paynow(){
    let that = this;
    console.log(that.data.address_id)
    if (that.data.address_id==0){
      wx.showToast({
        title: '请选择收货地址',
        icon:'none'
      })

      return false
    }
    let url = "home/index/pay";
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      "XX-Device-Type": 'wxapp'
    }
    let params = {
      shop_id: that.data.shop_id,
      shop_numb: that.data.shopnum,
      goods: that.data.goods,
      address_id: that.data.address_id,
      text: that.data.text
    }
    app.post(url, params, header).then((res) => {
      console.log(res)
      // that.setData({
      //   order_list: res.data
      // })
      let money=res.data.pay

      that.payment(money)
      console.log(that.data.order_list)
    }).catch((err) => {
      console.log(err)
     
    })
  },

  payment(res){
    wx.requestPayment({
       timeStamp: res.timeStamp,
      nonceStr: res.nonceStr,
      package: res.package,
      signType: res.signType,
      paySign: res.paySign,
      success: function (res) {

       wx.switchTab({
         url: '/pages/homeindex/homeindex',
       })
      },
      fail: function (res) {
      }
    })
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})