$.fn.dataStatistics = function(options) {
options = $.extend({
max: 500, //初始数值
min: 0, //最大数字
time: 60000, //时长
len: 4, //数字是几位数
price: 0, //金额
couponId: 0, //订单id
platform: 'app',
count: 0,
login: true,
activityStartDate: 0,
activityEndDate: 0,
nowDate: 0,
nextAc:false,
}, options || {});
var ths = this; //解决this指向问题
//初始化---------------------------------------start
var el = ths.find('.set_last');
var html = '
' +
'
' +
' ' +
'
' +
'
' +
'
' +
' ' +
'
' +
'
' +
'
'
//初始化值
var nowNums = zfill(options.max, options.len).toString().split("");
//补0
function zfill(num, size) {
var s = "000" + num;
return s.substr(s.length - size);
}
ths.find('.digit_set').each(function() {
for (i = 0; i <= 9; i++) {
$(this).append(html);
currentDigit = $(this).find('.digit')[i];
$(currentDigit).find('.digit_wrap').append(9 - i);
}
});
//初始化数值填入
$.each(nowNums, function(index, val) {
var set = ths.find('.digit_set').eq(index);
var i = parseInt(val)
set.find('.digit').eq(9 - i).addClass('active');
set.find('.digit').eq(9 - i + 1).addClass('previous');
});
//初始化---------------------------------------end
//执行
var difference = 0; //执行次数
var nownumber = options.min; //优惠券数量
var Activityendtime = ''; //活动结束时间
function run(diff) {
if (diff) {
difference = diff
} else {
difference = options.max - options.min; //要执行动画的次数
}
if (nownumber == 0) { //优惠券没有的时候处理
clearInterval(timer2); //优惠券用完 清楚定时器
// $(".received_img").attr("src", "");
$('.img_bg_M').css("background-image", "url(../images/MJ_200@3x.png)");
$('.img_bg_p1').css("color", "#CCCCCC");
$('.img_bg_p2').css("color", "#CCCCCC");
$('.img_btn p').css("color", "#FFFFFF");
$('.img_btn p').html('活动结束')
$('.box_content_t').hide()
$('.img_btn').css("background-image", "url(../images/nobuy_btn.png)");
if (options.nextAc == true) { //显示活动预告
$('.dataStatistics').css('display', "none");
$('.deta_replace').css('display', "flex");
$('#notice').css('display', "block");
$('.notice_img').css('display', "block");
}
// layer.msg('优惠券被抢光了!')
}
//后一位数
function increase() {
//执行次数为0时,停止执行
if (difference < 1) {
clearInterval(timer1);
return false;
}
difference--;
//翻页动画
var current = el.find('.active'),
previous = el.find('.previous');
previous.removeClass('previous');
current.removeClass('active').addClass('previous');
if (current.next().length == 0) {
el.find('.digit:first-child').addClass('active');
var prev = el.prev();
prevNumber(prev);
} else {
current.next().addClass('active');
}
}
var timer1 = setInterval(increase, 1000);
}
//当数字翻到9的时候,前一位数执行一次动画
function prevNumber(ths) {
var current = ths.find('.active'),
previous = ths.find('.previous');
previous.removeClass('previous');
current.removeClass('active').addClass('previous');
if (current.next().length == 0) {
ths.find('.digit:first-child').addClass('active');
var prev = ths.prev();
if (prev.length > 0) {
prevNumber(prev);
}
} else {
current.next().addClass('active');
}
}
run();
var timer2 = setInterval(() => {
$.ajax({
type: "POST",
url: `${getApiUrl().urlJava}api/appActivity/getCurrentActivity`,
contentType: "application/json",
data: getJavaData({ couponId: options.couponId }),
async: false,
success: function(res) {
if (res.resultCode === 1) {
nodisplay = true
if (nownumber > 0) {
let num = nownumber - res.data.repertory; // 算出动画执行的次数
nownumber = res.data.repertory; // 算出现在的数字
if (num > 0) {
if (num > 5) {
$('.digit_set').empty()
clearInterval(timer2);
$('.dataStatistics').dataStatistics({
max: nownumber,
min: nownumber,
len: 4,
add: true,
price: options.price,
couponId: options.couponId,
platform: options.platform,
login: options.login,
count: options.count, //判断是否可以抢购 0为不可以
activityStartDate: options.activityStartDate,
activityEndDate: options.activityEndDate,
nextAc: options.nextAc,
}); //刚进入页面不执行动画
} else {
run(num)
}
} else if (num < 0) {
$('.digit_set').empty()
clearInterval(timer2);
$('.dataStatistics').dataStatistics({
max: nownumber,
min: nownumber,
len: 4,
add: true,
price: options.price,
couponId: options.couponId,
platform: options.platform,
login: options.login,
count: options.count, //判断是否可以抢购 0为不可以
activityStartDate: options.activityStartDate,
activityEndDate: options.activityEndDate,
nextAc: options.nextAc,
}); //刚进入页面不执行动画
}
// options.count = res.data.count;
let activityStartDate = options.activityStartDate;
let activityEndDate = options.activityEndDate;
let nowDate = new Date(res.data.currentTime.replace(/-/g, '/')).getTime(); //服务器时间
// let nowDate = options.nowDate; //服务器时间
if (activityStartDate >= nowDate) { //活动还没开始
clearInterval(timer2);
} else if (nowDate >= activityEndDate || res.data.activityIsEnding == 1) { //活动结束
// $(".received_img").attr("src", "");
$('.img_bg_M').css("background-image", "url(../images/MJ_200@3x.png)");
$('.img_bg_p1').css("color", "#CCCCCC");
$('.img_bg_p2').css("color", "#CCCCCC");
$('.img_btn p').css("color", "#FFFFFF");
$('.img_btn').css("background-image", "url(../images/nobuy_btn.png)");
$('.img_btn p').html('活动结束')
$('.box_content_t').hide()
if (options.nextAc == true) { //显示活动预告
$('.dataStatistics').css('display', "none");
$('.deta_replace').css('display', "flex");
$('#notice').css('display', "block");
$('.notice_img').css('display', "block");
}
clearInterval(timer2); //活动结束 清楚定时器
} else {
if (nownumber > 0) {
if (options.count != 0) {
// $(".received_img").attr("src", "");
$('.img_btn p').html('立即抢购')
} else {
$(".received_img").css('display', "block");
$('.img_btn p').html('去使用')
$('.img_btn').css("background-image", "url(../images/buy_btn.png)");
}
} else {
// $(".received_img").attr("src", "");
$('.img_bg_M').css("background-image", "url(../images/MJ_200@3x.png)");
$('.img_bg_p1').css("color", "#CCCCCC");
$('.img_bg_p2').css("color", "#CCCCCC");
$('.img_btn p').css("color", "#FFFFFF");
$('.img_btn p').html('活动结束')
$('.box_content_t').hide()
$('.img_btn').css("background-image", "url(../images/nobuy_btn.png)");
if (options.nextAc == true) { //显示活动预告
$('.dataStatistics').css('display', "none");
$('.deta_replace').css('display', "flex");
$('#notice').css('display', "block");
$('.notice_img').css('display', "block");
}
}
}
} else {
clearInterval(timer2); //没有优惠券 清楚定时器
}
} else {
clearInterval(timer2); //没有优惠券 清楚定时器
}
},
error: function(res) {
}
});
}, (difference + 5) * 1000) //5s请求一次接口
$('.img_btn').click(function() {
let userInfo = JSON.parse(localStorage.userInfo)
if(userInfo.accountId && userInfo.accountId == '1182'){ //如果为唐杰的号直接跳转
if (options.count != 0) { //判断是可以抢购
window.location.href = `https://activity.oytour.com/html/Rushbuy.html?redemptionPrice=${options.price}&couponId=${options.couponId}&platform=${options.platform}`;
} else {
let data = {
'action': 'jump', // showToast->一个提示信息 showHud->一个黑色的loading
'page': 'teamWalker_2',
'pageData': {
'backType': 'pop',
}
}
if (options.platform == 'app') {
window.postMessage(JSON.stringify(data));
} else if (options.platform == 'H5') {
window.location.href = 'https://activity.oytour.com/html/downloadApp.html'
}
}
}else {
if ($('.img_btn p').css('color') == 'rgb(224, 50, 52)') { //判断按钮是否可以点击
if (options.login == true) {
if (options.count != 0) { //判断是可以抢购
// let buyInfo = {
// redemptionPrice: options.price,
// couponId: options.couponId,
// platform: options.platform,
// }
// localStorage.buyInfo = JSON.stringify(buyInfo)
window.location.href = `https://activity.oytour.com/html/Rushbuy.html?redemptionPrice=${options.price}&couponId=${options.couponId}&platform=${options.platform}`;
} else {
let data = {
'action': 'jump', // showToast->一个提示信息 showHud->一个黑色的loading
'page': 'teamWalker_2',
'pageData': {
'backType': 'pop',
}
}
if (options.platform == 'app') {
window.postMessage(JSON.stringify(data));
} else if (options.platform == 'H5') {
window.location.href = 'https://activity.oytour.com/html/downloadApp.html'
}
}
} else {
window.location.href = 'https://activity.oytour.com/html/login.html'
}
} else {
if ($('.img_btn p').text() == '活动结束') {
if (options.platform == 'app') {
window.postMessage(JSON.stringify({ 'action': 'showToast', 'message': '活动已结束' }));
} else {
layer.msg('活动已结束')
}
} else {
if (options.platform == 'app') {
window.postMessage(JSON.stringify({ 'action': 'showToast', 'message': '活动还未开始' }));
} else {
layer.msg('活动还未开始')
}
}
}
}
})
};