chongzhi.js 4.2 KB
// pages/chongzhi/chongzhi.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weizhi: false,
    agree: false,
    money: '',
    order_id:'',
    hui:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(this.data.options)
   this.setData({
     hui:options.hui
   })
  },
  chongzhihui(e){
    console.log(e)
    this.setData({
      money:e.detail.value
    })
  },
  chongzhi(e) {
    console.log(e)
    this.setData({
      money: e.detail.value
    })
  },
  huiyuan(){
    console.log(typeof(this.data.hui))
    if(this.data.hui=='1'){
      console.log(9999)
      wx.redirectTo({
        url:'/pages/vipdetail/vipdetail?hui=' + 1,
      })
    }else{
      console.log(888)
      wx.redirectTo({

        url: '/pages/vipdetail/vipdetail',
      })
    }
    
  },

  xuanzhifu() {
    this.setData({
      weizhi: !this.data.weizhi
    })
  },

  agreet() {
    this.setData({
      agree: !this.data.agree
    })
  },
  zhifu(){
    console.log(this.data.money)

    if(this.data.money==''){
        wx.showToast({
          title: '请输入充值金额',
          icon:"none"
        })

        return false
    }
    
    if (this.data.agree==false){
      wx.showToast({
        title: '请勾选用户须知',
        icon:"none"
      })

      return false
    }

    if(this.data.money!=''){
      if(this.data.hui==1){
        if(this.data.money<8000){
          wx.showToast({
            title: '请输入超过8000的金额',
            icon:"none"
          })

          return false
        }
      }
    }
    if (this.data.hui == 1){
      this.huiyuanorder()
    }else{
      this.feihuiyaun()
    }
    
   

      // let that = this;
      // let url = 'user/toporder';
      // let param={
      //   money:that.data.money
      // }
      // app.post(url, param, 'post').then((res) => {
      //   console.log(res);
      //   that.setData({
      //     order_id:res.order_id,
      //   })

      //   that.pay()

      // }).catch((err) => {

      // })
    
  },
  // 非会员订单
  feihuiyaun(){
    let that = this;
    let url = 'user/toporder';
    let param = {
      money: that.data.money
    }
    app.post(url, param, 'post').then((res) => {
      console.log(res);
      that.setData({
        order_id: res.order_id,
      })

      that.pay()

    }).catch((err) => {

    })
  },

  // 创建会员订单
  huiyuanorder(){
    let that = this;
    let url = 'user/viporder';
    let param = {
      total: that.data.money
    }
    app.post(url, param, 'post').then((res) => {
      console.log(res);
      that.setData({
        order_id: res.order_id,
      })

      that.pay()

    }).catch((err) => {
      wx.showToast({
        title: err.msg,
        icon:'none'
      })

    })
  },

  // 支付接口
  pay(){
    let that = this;
    let url = 'pay/pay';
    let param = {
      order_id: that.data.order_id
    }
    app.post(url, param, 'post').then((res) => {
      console.log(res);
      that.payment(res)

    }).catch((err) => {

    })
  },

  //唤起支付


  payment(res) {
    let that = this;
    var timeStamp = res.timeStamp.toString();
    // console.log(timeStamp);
    wx.requestPayment({
      timeStamp: timeStamp,
      nonceStr: res.nonceStr,
      package: res.package,
      signType: res.signType,
      paySign: res.paySign,
      success: function (res) {
        console.log(res)
        wx.navigateTo({
       
          url: '/pages/mine/mine',
        })
       
      },
      fail: function (res) {
        console.log(123);
        console.log(res);
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})