pay.page.js 7.17 KB
let $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload');

let prompt = $('.fraud-prompt'),
    tipContent = $('.tip-content'),
    tipEle = $('.tip'),
    fraudTip = $('.fraud-tip');

let tipConWidth = tipContent.width(),
    tipWidth = tipEle.width(),
    promptWidth = prompt.width();

let scrollVal,
    onePageW,
    diff;
let loading = require('js/plugin/loading'),
    tip = require('js/plugin/tip'),
    Hammer = require('yoho-hammer');

let richTip = require('js/plugin/rich-tip');


// 点击微信显示加载
let $loadingToast = $('.loading-toast');

let theOrderCode = document.getElementById('ordercode') ? document.getElementById('ordercode').value : '';
let orderType = document.getElementById('ordertype') ? document.getElementById('ordertype').value : '';

let wxPayEl = document.getElementById('weixin'),
    $aliPayEL = $('#alipay'),
    wxHammer = wxPayEl && new Hammer(wxPayEl);

let appIconPosition = {
    baidu: '-2.7rem',
    weixin: '-1.2rem',
    QQ: '-5.4rem',
    bank: '-4rem'
};

$('.nav-back').on('click', function() {
    richTip.show(
        ['超过支付时效后订单会被取消,', '请尽快到我的订单完成支付哦!'],
        [
            {
                href: '/home/orders',
                text: '确认离开',
                class: ''
            },
            {
                href: '',
                text: '继续支付',
                class: 'order-tip-btnred'
            }
        ]
    );
});

require('js/common');

lazyLoad($('img.lazy'));

function getGender() {
    return window.cookie('_Channel') || 'boys';
}

require('js/channel/maybe-like')({gender: getGender()});

$(document).on('PAY_SUCCESS_PAGE_MAYBE_LIKE_LOAD', () => {
    if ($('#goods-list').find('.good-info').length === 0) {
        $('.maybe-like').hide();
    }
});

function Marquee() {
    prompt.scrollLeft(prompt.scrollLeft() + 1);

    scrollVal = prompt.scrollLeft();
    onePageW = tipContent.eq(0).width();
    diff = scrollVal - onePageW;

    if (diff > 0) {
        prompt.scrollLeft(Math.abs(diff));
    }
}

// 防诈骗提示滚动
if (tipConWidth > promptWidth - tipWidth) {
    let speed = 30;

    tipContent.width(tipConWidth + (promptWidth / 2));

    fraudTip.append('<div class="tip-content">' + tipContent.html() + '</div>').css({
        width: 2 * tipContent.width()
    });

    setInterval(Marquee, speed);
}

// 隐藏微信分享选项
if (window.wx) {
    window.wx.hideOptionMenu();
}

function onBridgeReady() {
    document.addEventListener('WeixinJSBridgeReady', function() {
        window.WeixinJSBridge && window.WeixinJSBridge.call('hideOptionMenu');
    });
}

if (typeof WeixinJSBridge === 'undefined') {
    if (document.addEventListener) {
        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
    } else if (document.attachEvent) {
        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
    }
    onBridgeReady();
} else {
    onBridgeReady();
}

// 调用微信JS api 支付
function jsApiCall(orderCode, jsApiParameters) {
    $loadingToast.addClass('hide');

    // 防止重复操作弹框
    window.WeixinJSBridge && window.WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        jsApiParameters,
        function() {
            if (orderType === '3_2') { // 电子刊
                window.location.href = '/cart/magazine/pay/success';
            } else {
                window.location.href = '/home/orders/detail?order_code=' + orderCode + '&openId=true';
            }
        }
    );
}

// 支付宝支付
if ($aliPayEL && $aliPayEL.length > 0 && $aliPayEL.data('href')) {
    $aliPayEL.on('click', function() {
        $.get($aliPayEL.data('href'), function(result) {
            if (result && result.code === 200 && result.url) {
                window.location.href = result.url;
            } else {
                tip.show(result && result.message || '系统错误!');
            }
        });
    });
}

/**
 * 获取微信支付数据
 * @param {*} orderCode
 */
function getWechatPayData(orderCode) {
    let jsApiParameters;

    $.ajax({
        type: 'GET',
        url: '/cart/index/new/pay',
        data: {
            order_code: orderCode,
            payment: '22_platform',
        },
        dataType: 'json',
        success: function(res) {
            if (res.code === 200) {
                jsApiParameters = res.data.jsApiParameters;
                jsApiCall(orderCode, jsApiParameters);
            } else {
                tip.show('微信支付调取失败');
            }
        },
        error: function() {
            tip.show('请刷新本页面,完成微信支付');
        },
        complete: function() {
            $loadingToast.addClass('hide');
        }
    });
}

// 微信支付
function callpay(orderCode) {
    $loadingToast.removeClass('hide');
    if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', function() {
                getWechatPayData(orderCode);
            }, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', function() {
                getWechatPayData(orderCode);
            });
            document.attachEvent('onWeixinJSBridgeReady', function() {
                getWechatPayData(orderCode);
            });
        }
    } else {
        getWechatPayData(orderCode);
    }
}


function isWXOpen() {
    let ua = window.navigator.userAgent.toLowerCase();

    return ua.indexOf('micromessenger') > 0;
}

function hideWeChatPay() {
    let $payApps = $('.app');

    $payApps.each(function(idx, app) {
        if ($(app).parent().attr('id') === 'weixin') {
            $(app).parent().css('display', 'none');
            return false;
        }
    });
}

function hideAliPay() {
    let $payApps = $('.app');

    $payApps.each(function(idx, app) {
        if ($(app).parent().attr('id') === 'alipay') {
            $(app).parent().css('display', 'none');
            return false;
        }
    });
}

function handleForWX() {
    if (!isWXOpen()) {
        hideWeChatPay();
    } else {
        hideAliPay();
    }
}


function setAppIcon(el, position) {
    $(el).css('background-position-y', position);
}

function loadIcon() {
    let $boxs = $('.box');
    let $div = null;
    let appid = null;

    $boxs.each(function(idx, box) {
        $div = $(box).find('.icon').find('div');
        if ($div.length > 0) {
            appid = $(box).attr('id');
            if (appid !== 'alipay') {
                setAppIcon($div, appIconPosition[appid]);
            }
        }
    });
}

function showPage() {
    $('.payapp-list').css('visibility', 'visible');
}

function bindTouchedEvent() {
    let $boxs = $('.box');

    $boxs.on('touchstart', function() {
        $(this).addClass('bytouch');
    }).on('touchend touchcancel', function() {
        $boxs.removeClass('bytouch');
    });
}

if (wxHammer) {
    wxHammer.on('tap', function() {
        callpay(theOrderCode);
    });
}

function main() {
    handleForWX();
    loading.hideLoadingMask();
    showPage();
    loadIcon();
    bindTouchedEvent();
}

loading.showLoadingMask();

$(document).ready(main);