wxpay.page.js 2.12 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'),
    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);
    },
    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;
                }
            }
        });
    }
};

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