visualstatements.js 5.82 KB
import { API_HOST, SERVICE_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import { wexinPay } from '../../utils/wxpay';
import { getYHStorageSync } from '../../utils/util';

import {
  logEvent,
  YB_SC_ORD,
  YB_PAGE_OPEN_L,
} from '../../libs/analytics.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isVirtual: true,
    needInvoice: false,
    invoiceData: null,
    invoiceDetail: "电子发票-个人",
    payTips: "",
    shopping_cart_data:{
      goods_count:'',
      last_order_amount:'',
      order_amount:'',
    },
    goods_list: [],
    goods_item:{
      tags:[],
      buy_number:'',
      color_id:'',
      color_name:'',
      size_id:'',
      size_name:"",
      goods_images:"",
      goods_type:'',
      product_id:'',
      product_name:"",
      product_skc:"",
      product_skn:"",
      product_sku:"",
      sales_price:'',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp();
    let timestamp = options.timestamp;
    let value = getYHStorageSync(timestamp, 'statements');
    if (!value) {
      wx.showModal({
        title: '提示',
        content: '出现异常错误,请重试!',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
      return;
    }
    try {
      wx.setStorageSync(timestamp, null);//清除内存
    } catch (e) {
    }

    //赋值
    this.setData({
      payTips:value.data.tips,
      shopping_cart_data: value.data.shopping_cart_data,
    });
    this.setData({
      shopping_cart_data:{
        last_order_amount: parseInt(this.data.shopping_cart_data.last_order_amount).toFixed(2),
        order_amount: parseInt(this.data.shopping_cart_data.order_amount).toFixed(2),
      }
    });
    // console.log(this.data.shopping_cart_data);
    let goods = value && value.data && value.data.goods_list ? value.data.goods_list : [];
    for (var i = 0; i < goods.length;i++){
      let item = goods[i];
      item.goods_images = item.goods_images.replace(/{width}/, 100).replace(/{height}/, 100);
      item.sales_price = parseInt(item.sales_price).toFixed(2);
    let tags = item.tags;
    item.LRE = false;
    item.L15DE = false;
    for (var j = 0; j < tags.length; j++) {
      if (tags[j] == 'LRE') {
        item.LRE = true;
      } else if (tags[j] == 'L15DE') {
        item.L15DE = true;
      }
    }
    }
    this.setData({
      goods_list: goods,
    });

  },
  switch2Change: function (e) {
    let needInvoice = e.detail.value;
    this.setData({
      needInvoice,
    });
  },
  //发票
  invoiceIconAction: function (e) {
    var app = getApp();
    var timestamp = Date.parse(new Date());
    try {
      wx.setStorageSync(timestamp + '', this.data.invoiceData);
    } catch (e) {
    }
    wx.navigateTo({
      url: '../invoice/invoice' + '?timestamp=' + timestamp + '' + '&userTel=' + app.getMobile(),
    })
  },
  //发票选择回调
  invoiceCallBack: function (data) {
    let invoiceData = data;
    // console.log(invoiceData);
    this.setData({
      invoiceData,
    });

  },
  
  //支付
  payNow: function () {
    var app = getApp();
    let param = {
      method:'app.shopping.submitTicket',
      product_sku: this.data.goods_list[0].product_sku,
      buy_number: this.data.goods_list[0].buy_number,
    };
    if (this.data.needInvoice){
      param.mobile= this.data.invoiceData ? (this.data.invoiceData.phoneNum ? this.data.invoiceData.phoneNum : app.getMobile()) : app.getMobile();
        param.invoice_content =  12;
        param.invoices_type =  2;
        param.invoices_title = this.data.invoiceData ? (this.data.invoiceData.personal ? this.data.invoiceData.personName : this.data.invoiceData.companyName) : '个人';
        param.buyerTaxNumber = this.data.invoiceData ? (this.data.invoiceData.personal ? '' : this.data.invoiceData.taxNum) : '';
        param.receiverMobile = this.data.invoiceData ? (this.data.invoiceData.phoneNum ? this.data.invoiceData.phoneNum : app.getMobile()) : app.getMobile();

      if (!param.receiverMobile || param.receiverMobile == undefined || param.receiverMobile == 'undefined') {
          wx.showToast({
            title: '请输入发票手机号...',
            duration:1500
          })
        return
      }
    };
   
    var that = this;
    GET(API_HOST, param).then(function (data) {
      if (data && data.code === 200){
        if (that.data.shopping_cart_data.last_order_amount > 0) {
          let payParam = {
            isVirtual: true,
          };
          wexinPay(data.data,payParam);
        } else {
          wx.navigateTo({
            url: "../paymentSuccessed/paymentSuccessed?orderCode=" + data.data.order_code + '&hasSuc=true' + '&price=' + that.data.shopping_cart_data.last_order_amount + '&isVirtual=true'
          });
        }
      } else {
        wx.showModal({
          content: data.message,
          showCancel: false,
          confirmText: "确定",
        });
      }
    }).catch(function (error) {
      wx.showToast({
        title: error.message,
        duration:1500
      })
    });
  },

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

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

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

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

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

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

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