Vue.config.devtools = true;
var v = new Vue({
    el: ".container",

    data: {
        objType: '',
        objId: '',
        objInfo: {},
        subing: false,
        memToken: 'H5_HQ2RRR.Vw87oBWZqtYDg_waofbMfkjDtc5BorX21pXNw7R5_wY3ogXZaJZ8Gzom9wy5p2oi_K98hI.NdPAKBJA%3D%3D',
        memCard: 'bf524aec007f4f58b8835c211f846269',
        payTypes: [false, true],
        payType: 2,
        timer: null,
        showPoint: false, // 积分提示
        showPointYs: false, // 积分提示
    },
    created: function () {
        // if(!checkLogin()){
        //     goLogin();
        //     return false;
        // }
        var that = this;
        that.objType = $("#objType").val();
        that.objId = $("#objId").val();
        that.getObjInfo();
    },
    mounted: function () {
        var that = this;
        if ($("#appId").val()) {
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady',
                        that.onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady',
                        that.onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady',
                        that.onBridgeReady);
                }
            } else {
                that.onBridgeReady();
            }
        }
        that.findUserInfoWithSm()
        that.findUserInfoWithYsWeb()
    },
    methods: {
        onBridgeReady() {
            var that = this;
            WeixinJSBridge.invoke('getBrandWCPayRequest', {
                "appId": $("#appId").val(), //公众号名称，由商户传入
                "timeStamp": $("#timeStamp").val(), //时间戳，自1970年以来的秒数
                "nonceStr": $("#nonceStr").val(), //随机串
                "package": $("#packages").val(),
                "signType": $("#signType").val(), //微信签名方式：
                "paySign": $("#paySign").val()
                //微信签名
            }, function (res) {
                //alert(JSON.stringify(res));
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    //支付成功
                    alert('支付成功');
                    that.gotoObjDetail();
                } else {
                    //支付失败
                    alert("支付失败");
                    //alert(JSON.stringify(res));
                }
            });
        },
        toPay() {
            var that = this;
            if (that.payType == 1) {
                var orderId = that.createOrder();
                var localurl = encodeURIComponent(window.location.href);
                if (orderId) {
                    that.NATIVEPay(localurl, orderId);
                }
            } else if (that.payType == 2) { // 积分
                that.pointPay();
            }
        },
        // 微信支付
        NATIVEPay(localurl, orderId) {
            window.location.href = serverHttp + "/weixin/nativepay?orderid=" + orderId;

            // 每个3秒调用后台方法，查看订单是否已经支付成功
            this.timer = setInterval(this.checkPayResult, 3000);
        },
        // 查询是否支付成功
        checkPayResult() {
            console.log('定时查询')
            $.get("/weixin/queryPayStatus?orderNo=" + out_trade_no, function (data) {
                console.log(data);
                if (data.flag) {
                    //支付成功 清除定时器
                    clearInterval(this.timer);
                    window.location.href = "/pay/payFinish?out_trade_no=" + out_trade_no;
                }
                if (data.flag == false) {
                    alert("支付失败")
                }
            });
        },
        createOrder() {
            var that = this;
            var orderId = "";
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/createOrderWeb",
                async: false,
                data: {
                    memToken: that.memToken,
                    memCard: that.memCard,
                    objType: that.objType,
                    objId: that.objId,
                    paySource: 1,
                    fromMemCard: ''
                },
                success: function (data) {
                    if (data.code == 200) {
                        //订单创建成功
                        orderId = data.data;
                    } else {
                        alert(data.message);
                    }
                },
                error: function (data) {
                }
            });
            return orderId;
        },
        pointPay() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/pointPayWeb",
                data: {
                    memToken: that.memToken,
                    memCard: that.memCard,
                    objType: that.objType,
                    objId: that.objId,
                    paySource: 1,
                    fromMemCard: ''
                },
                success: function (data) {
                    if (data.code == 200) {
                        //积分兑换成功，跳转回商品页面
                        playMusic("积分兑换成功", 10);
                        setTimeout(that.gotoObjDetail, 1500)
                    } else {
                        alert(data.message);
                    }
                },
                error: function (data) {
                }
            });
        },
        payTypeChange(payType) {
            this.payType = payType;
        },
        getObjInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/getObjInfoWeb",
                data: {
                    memToken: that.memToken,
                    objType: that.objType,
                    objId: that.objId,
                    memCard: this.memCard,
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.objInfo = data.data;
                        if (that.objInfo.objPrice == 0) {
                            that.payTypes = [false, true];
                            that.payType = 2;
                        } else if (that.objInfo.objPoint == 0) {
                            that.payTypes = [true, false];
                            that.payType = 1;
                        }
                    }
                },
                error: function (data) {
                }
            });
        },
        gotoObjDetail() {
            window.history.go(-1);
            // var objType = $("#objType").val();
            // var objId = $("#objId").val();
            // if (objType == 1) {
            //     window.location.href = serverHttp + "/course/courseDetails?from=web&id=" + objId;
            // } else if (objType == 2) {
            //     window.location.href = serverHttp + "/live/detail?from=web&liveId=" + objId;
            // } else if (objType == 3) {
            //     window.location.href = serverHttp + "/meeting/details?from=web&conferId=" + objId;
            // } else if (objType == 4) {
            //     window.location.href = serverHttp + "/courseware/index?from=web";
            // }
        },
        // 查询是否实名认证过
        findUserInfoWithSm() {
            $.ajax({
                type: 'POST',
                url: "/user/findUserInfoWithSmWeb",
                success: (data) => {
                    this.showPoint = data.code == 200 && data.data == null;
                },
                error: function (data) {
                }
            });
        },
        // 查询是否医师认证过
        findUserInfoWithYsWeb() {
            $.ajax({
                type: 'POST',
                url: "/user/findUserInfoWithYsWeb",
                success: (data) => {
                    this.showPointYs = data.code == 200 && data.data == null;
                },
                error: function (data) {
                }
            });
        },
    }
})
