seckill-detail.page.js 1.49 KB
const tip = require('plugin/tip');

$(window).on('seckill', function() {
    $('.btn-c').before('<a href="https://union.yoho.cn/union/app-downloads.html" class="seckill-download-app">下载APP购买</a>');
    $('.btn-c').remove();
});

$(function() {
    $('#goodsDiscount').hide(); // 隐藏折扣楼层
    $('.current-price').hide();
    let ajaxUrl = '/product/seckillDetail/seckillData/' + $('#productSkn').val();
    let timestamp = Date.parse(new Date());

    $.ajax({
        type: 'GET',
        url: ajaxUrl + '?tamp=' + timestamp,
        success: function(data) {
            let secKillPrice = toDecimal2(data.secKillPrice); // eslint-disable-line

            if (secKillPrice) {
                $('.current-price').before('<h1 class="seckill-price" style="display: inline-block;"></h1>').hide();
                $('.current-price').remove();
                $('.seckill-price').text('¥' + secKillPrice).show();
            } else {
                $('.current-price').show();
            }
        },
        error: function() {
            tip.show('网络异常~');
        }
    });
});

// 强制保留2位小数点
function toDecimal2(num) {
    let f = parseFloat(num);

    if (isNaN(f)) {
        return false;
    }
    f = Math.round(num * 100) / 100;
    let s = f.toString();
    let rs = s.indexOf('.');

    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

// 调用新商品详情页js
require('./new-detail');