wxpay.page.js 2.74 KB
var $ = require('yoho-jquery'),
    dialog = require('../common/dialog');

var wxPage = {}, inter;
var $wOdetail = $('.w-odetail'),
    $wPayment = $('.w-payment'),
    $wAddrinfo = $('.w-addrinfo'),
    $wPerm = $('.w-p-erm'),
    time = $('.js-time').data('time'),
    timeInterval,
    $timer = $('.js-timer'),
    orderCode = $wPayment.data('order');

require('../simple-header');
require('yoho-jquery-qrcode');

wxPage = {
    init: function() {
        $wPerm.qrcode({
            render: 'table', // 显示方式,canvas,image和div
            text: $wPerm.data('url'), // 二维码的内容
            size: 250
        });
        this.setEvent();
        inter = setInterval(this.checkPayStatus, 3000);
        this.setTimer();
        timeInterval = setInterval(() => {
            this.setTimer();
        }, 1000);
    },
    setEvent: function() {
        $wOdetail.on('click', function() {
            $wAddrinfo.slideToggle('slow');
            if ($(this).find('i').hasClass('up')) {
                $(this).html('收起详情<i class="down"></i>');
            } else {
                $(this).html('订单详情<i class="up"></i>');
            }
        });
    },
    checkPayStatus: function() {
        $.ajax({
            url: '//www.yohobuy.com/shopping/newpay/weixin/state',
            dataType: 'json',
            type: 'post',
            data: {
                code: orderCode
            },
            success: function(d) {
                if (d.code === 401) {
                    clearInterval(inter);
                    return new dialog.Dialog({
                        content: d.message,
                        closeIcon: false,
                        className: 'alert-dialog',
                        btns: [{
                            id: 'alert-sure',
                            btnClass: ['alert-sure'],
                            name: '查看我的订单',
                            cb: function() {
                                window.location.href = '//www.yohobuy.com/home/orders';
                            }
                        }]
                    }).show();
                } else if (d.code === 200) {
                    clearInterval(inter);
                    window.location.href = d.data.href;
                }
            }
        });
    },
    setTimer: function() {
        if (time < 0) {
            return clearInterval(timeInterval);
        }

        let hour = Math.floor(time / 3600);
        let minus = Math.floor((time % 3600) / 60);
        let second = time % 60;
        let text = '';

        if (hour > 0) {
            text += hour + '小时';
        }

        text += minus + '分' + second + '秒';

        $timer.text(text);
        time--;
    }
};

$(function() {
    wxPage.init();
});