/**
 *Created by ZHJY on 2020/7/14.
 */
var payContainer = new Vue({
    el:"#payContainer",
    data: {
        orderID:'',
        pageType:'',//页面跳转类型 1为二手 0为订单
        payAmount:0,//本次支付金额
        wechatcode:'',//微信code
        state:0,//随机数
        isRequest:false,
        productId:'',//二手商品ID
    },
    computed: {

    },
    methods: {
        infoPayOrderCheckoutCounter:function(){//初始化订单数据
            var _this = this;
            _this.wechatcode = _util.getUrlParam('code');
            _this.state = JSON.parse(decodeURIComponent(_util.getUrlParam('state')));
            _this.payAmount = _this.state.payAmount;
            _this.pageType = _this.state.pageType;
            _this.isRequest = true;
            if( _this.pageType == 1){
                _this.productId = _this.state.productId;
            }else{
                _this.orderID = _this.state.orderID;
            }
        },
        WxPaySubmit:function(){
            var _this = this;
            if(_this.pageType == 1){
                _this.WxRequestsecondHandPay();
            }else{
                _this.WxRequestPaymentFn();
            }
        },
        WxRequestsecondHandPay:function(){
            var params = {
                payWay:'WEIXIN',
                code:_this.wechatcode,
                state:_this.rand(1000,9999),
                productId:_this.productId,
                returnUrl:'https://www.caimei365.com/',
            };
            PayApi.PayOrdersecondHandPay(params,function(response){
                if(response.code == 0){
                    let PayInfo = JSON.parse(response.data.data.payInfo);
                    if (typeof WeixinJSBridge == "undefined"){
                        if( document.addEventListener ){
                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                        }else if (document.attachEvent){
                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                        }
                    }else{
                        _this.onBridgeReady(PayInfo);
                    }
                }else{
                    layer.msg("支付链接已失效,请重新扫码支付");
                    setTimeout(function(){
                        WeixinJSBridge.call('closeWindow');
                    },3000);
                }
            });
        },
        WxRequestPaymentFn:function(){
            var _this = this;
            var params={
                    payAmount:_this.payAmount*100,
                    payWay:"WEIXIN",
                    code:_this.wechatcode,
                    state:_this.rand(1000,9999),
                    orderId:_this.orderID,
                    returnUrl:'https://www.caimei365.com/',
            };
            PayApi.PayOrderMiniWxPay(params,function(response){
                if(response.code == 0){
                    let PayInfo = JSON.parse(response.data.data.payInfo);
                    if (typeof WeixinJSBridge == "undefined"){
                        if( document.addEventListener ){
                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                        }else if (document.attachEvent){
                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                        }
                    }else{
                        _this.onBridgeReady(PayInfo);
                    }
                }else{
                    layer.msg("支付链接已失效,请重新扫码支付");
                    setTimeout(function(){
                        WeixinJSBridge.call('closeWindow');
                    },3000);
                }
            })
        },
        onBridgeReady:function(data){
            console.log('支付验证签名',data);
            WeixinJSBridge.invoke('getBrandWCPayRequest', {
                        "appId":data.appId,     //公众号名称，由商户传入
                        "timeStamp":data.timeStamp,//时间戳，自1970年以来的秒数
                        "nonceStr":data.nonceStr, //随机串
                        "package":data.package,
                        "signType":data.signType,        //微信签名方式：
                        "paySign":data.paySign,  //微信签名
                    },
                function(res) {
                    if(res.err_msg == "get_brand_wcpay_request:ok"){
                        layer.msg('支付成功');
                        window.open("https://www.caimei365.com/");
                    }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                        layer.msg("用户取消支付,请重新扫码支付");
                        setTimeout(function(){
                            WeixinJSBridge.call('closeWindow');
                        },3000);
                    }else{
                        layer.msg("支付失败,请重新扫码支付");
                        setTimeout(function(){
                            WeixinJSBridge.call('closeWindow');
                        },3000);
                    }
            });
        },
        rand:function (min,max) {//随机数
            return Math.floor(Math.random()*(max-min))+min;
        },
        toFixedFn:function(text){//处理小数点后两位数
            return Number(text).toFixed(2);
        },
    },
    mounted: function () {
        var _this = this;
        _this.infoPayOrderCheckoutCounter();
    }
});
