import popup from "$utils/tool/popup";
import {InstructionsMessageType} from "$utils/request";

export default {

    methods:{

        // 触发 支付宝的回调
        triggerPayAlipay(data){
            window.open(data.result);
            this.setAlipayWaitPattern();
        },

        // 设置等待模式
        setAlipayWaitPattern(){

            // 开启验证弹窗
            this.openPayInfoConfirm(()=> this.checkAliPayStatus());

            let nowTime = Math.floor(new Date().getTime() / 1000);

            // 设置终止时间
            this.pattern = setInterval(()=>{

                let diff =  Math.floor(new Date().getTime()/1000) - nowTime;

                // 如果满足任意一种主动释放 本次循环
                if (document.hidden === true || document.hidden === undefined || diff >= 300) {
                    clearInterval(this.pattern);
                }

                if (document.hidden === true) {
                    this.checkAliPayStatus();
                }


            },1000);

        },

        // 检查支付宝支付状态
        checkAliPayStatus(){

            // 防止未清除掉
            clearInterval(this.pattern);

            // 关闭弹窗
            popup.closeConfirm();

            return this.$request({
                url:'hxpay/user_recharge_info',
                data:{
                  oid:this.storagePayReuslt.oid
                },
                token:true,
                failMessage:true,
                loading:'验证中',
                message: InstructionsMessageType.other
            }).then((data)=>{

                if (data.isSuccess) {
                    if (data.data ==2) {
                        popup.$toast('支付成功');
                        this.nowPayStatus = true;
                        this.$store.dispatch('updateUserMoneyPromise');
                        return this.paySuccessCallback && this.paySuccessCallback();
                    } else {
                        popup.$toast('支付未成功');
                    }
                }

                return  this.openPayInfoConfirm(()=> this.checkAliPayStatus());

            }).catch(()=>  this.openPayInfoConfirm(()=> this.checkAliPayStatus()));
        },

    }

}