Skip to content

Instantly share code, notes, and snippets.

@GZShi
Last active May 8, 2016 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GZShi/b4e23722b86562cce4973c853e0d9771 to your computer and use it in GitHub Desktop.
Save GZShi/b4e23722b86562cce4973c853e0d9771 to your computer and use it in GitHub Desktop.
SNH购票…
(function () {
if (location.host !== 'shop.48.cn') {
console.error('kidding me?');
return ;
}
if (typeof jQuery !== 'function') {
console.error('缺少基础库');
return ;
}
if (typeof Promise !== 'function') {
console.error('请升级浏览器');
return ;
}
var seat = {
'vip坐票': '2',
'普通坐票': '3',
'普通站票': '4'
};
function buy(ticketId, ticketsCount, seatType) {
ticketId = ticketId || /\/(\d+)$/.exec(location.href)[1];
ticketsCount = ticketsCount || 1;
seatType = seatType || '普通坐票';
return new Promise(function (resolve, reject) {
$.ajax({
url: '/TOrder/add',
type: 'post',
dataType: 'json',
data: {
id: ticketId,
num: ticketsCount,
seattype: seat[seatType] || '3',
brand_id: '1',
r: Math.random()
},
error: reject,
success: function (result) {
if (result.HasError) {
reject(result.Message);
}
else {
resolve(result.ReturnObject);
}
}
});
});
}
function delay(tick) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, tick);
});
}
// 循环刷票,直到下单成功
// tick: 刷单间隔,单位ms
var failedTimes = 0;
var freshControl = 1;
function fresh(tick) {
buy('106', 1, '普通坐票')
.then(function (orderId) {
if (orderId) window.location.href = orderId; // 跳到下单页面
}, function (failedReason) {
failedTimes++;
console.info('刷单失败' + failedTimes + '次,原因:' + failedReason);
if (failedReason.indexOf('请不要反复点击') >= 0) {
// 刷新间隔过大,停止循环
var newTick = tick + 1000 * freshControl;
freshControl += 1;
console.log('系统检测到下单频率过快,延迟调整至:' + newTick);
delay(newTick).then(fresh.bind(this, tick));
} else {
freshControl = 1;
delay(tick).then(fresh.bind(this, tick));
}
});
}
fresh(3000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment