请叫我小C的gravatar头像
请叫我小C 2016-09-14 13:49:48

【小C出品】听闻阿里大神抢月饼没法过中秋节了,我也献上一份以前抢购某商城的秒杀js代码片段

(function(globle) {
	var popVerificationCode;
	var suffix = new Date().getTime();
	var param;
	var paramPageToken;// 参数页获取标识

	var ToolsUtil = {
		suffix : suffix,
		// 生成随机数
		randomSum : function(min, max) {
			return min + Math.round(Math.random() * (max - min));
		},
		/*
		 * 函数说明:根据MAP返回字符串参数 param map: {mid:"1",page:"2"} return "mid=1&page"
		 */
		dealParamMapToStr : function(param) {
			var str = "";
			var i = 1;
			for ( var key in param) {
				if (i === 1) {
					str += key + "=" + param[key];
				} else {
					str += "&" + key + "=" + param[key];
				}
				i++;
			}
			return str;
		},
		// 弹出遮罩功能
		addLoading : function() {
			timerId = window.setTimeout(function() {
				if ($("#sendDataLoad").length == 0) {
					$("body").append(
							Js.Tools.getShadeLayer("loadingContent") + "<div id='wjLoad-body' class='loadingContent'>"
									+ "<div id='sendDataLoad'><img src='/htmlResource/images/loading.gif'/></div>"
									+ "<div>" + "<div class='wjLoad-text'>努力加载中...<span id='dot'>请稍等</span></div>"
									+ "</div>" + "</div>");
					Js.Tools.setEleToCenter("#wjLoad-body");
				}
			}, 10);
		},
		removeCloseLoading : function() {
			Js.closeLoadContent();
		},
		codeError : function() {
			var suffix = this.suffix;
			var closeSubmitLoading = function() {
				$("#verificationCodeSubmit_" + suffix).find("span").html("提交");
			}
			closeSubmitLoading();
			$("#codeError_" + suffix).empty().append("您输入的验证码有误,请重新输入");
			$("#verify_" + suffix).attr("src", sendLink.auth + "validCodeImage?" + (new Date().getTime()));
			$("#verifycode_" + suffix).focus();
		},
		addSubmitLoading : function() {
			$("#verificationCodeSubmit_" + this.suffix).find("span").html("提交中...");
		},
		verificationCode : function() {
			var suffix = this.suffix;
			var verificationCode = $("#verifycode_" + suffix).val();
			if (verificationCode == null || $.trim(verificationCode).length == 0) {
				$("#codeError_" + suffix).empty().append("请填写验证码");
				$("#verifycode_" + suffix).focus();
				return "";
			}
			return verificationCode;
		},
		/*
		 * 函数说明:延迟执行 param fn:需要执行的函数 millisec:在执行代码前需等待的毫秒数
		 */
		delayExecution : function(fn, millisec) {
			setTimeout(fn, millisec);
		},
		createPopElement : function() {
			var str = '<div id="openVerificationCode_'
					+ suffix
					+ '" class="hidden" style="width:600px; margin:0 auto;border:1px solid #dedede;background-color:#fff;">'
					+ '<a class="block right  pr5 font14 arial" href="javascript:void(0);" id="closeVerificationCode_'
					+ suffix
					+ '">x</a>'
					+ '<div class="p60 t_c">'
					+ '<dl>'
					+ '<dt><span class="font30 dark">我们一起防黄牛!</span><br /><br /><span class="font18">请您输入下面验证码</span></dt>'
					+ '<dd id="code_'
					+ suffix
					+ '" class="pt30 clearfix" style="width:210px; margin:0 auto;">'
					+ '<div class="left" style="line-height:36px;"><img id="verify_'
					+ suffix
					+ '" src="http://authentication.go.lemall.com/validCodeImage" title="点击换一张" style="height:36px;" /></div>'
					+ '<a class="left pl20" style="line-height:36px;" href="javascript:;" id="a_verify_' + suffix
					+ '">换一张</a>' + '</dd>' + '<dd  class="pt10 ">' + '<input name="" id="verifycode_' + suffix
					+ '" type="text" class="" style="width:210px;height:36px;" />' + '</dd>'
					+ '<dd class="pt10"><div id="codeError_' + suffix + '" class="error clear red" ></div></dd>'
					+ '</dl>' + '<div  class="pt30">'
					+ '<a class="red_bt_l inline_block" href="javascript:void(0);" id="verificationCodeSubmit_'
					+ suffix + '"><span class="white">确定</span></a>' + '</div>' + '</div>' + '</div>';
			$(document.body).append(str);
			return {
				popId : "#openVerificationCode_" + suffix,
			}
		}
	};
	var InitEventBund = {
		suffix : suffix,
		verificationEvent : function() {
			var suffix = this.suffix;
			// 按钮点击事件
			$("#verificationCodeSubmit_" + suffix).live('click', function() {
				var code = ToolsUtil.verificationCode();
				if (code != "") {
					if (typeof param != "undefined") {
						param.RANDOMCODE = code;
						ToolsUtil.addSubmitLoading();
						globle.huoDongService.sendRequest(param, paramPageToken);
					}
				} else {
					return false;
				}
			});
			$("#code_" + suffix).live('click', function() {
				$("#verify_" + suffix).attr("src", sendLink.auth + "validCodeImage?" + (new Date().getTime()));
			});
			$("#a_verify_" + suffix).live('click', function() {
				$("#verify_" + suffix).attr("src", sendLink.auth + "validCodeImage?" + (new Date().getTime()));
			});
			$("#closeVerificationCode_" + suffix).live('click', function() {
				if (popVerificationCode) {
					popVerificationCode.close();
				}
			});
			$("#verifycode_" + suffix).live('click', function() {
				$("#codeError_" + suffix).empty();
			});
		}
	};
	/*
	 * 函数说明:发送抢购服务请求 param args:
	 * {rushId:"1",RANDOMCODE:"2"}/{rushId:"1"},如果RANDOMCODE存在说明由验证码处理提交的请求
	 * param_page_token:参数页获取标识 return "mid=1&page"
	 */
	var HuoDongService = function() {
	};
	HuoDongService.prototype.sendRequest = function(args, param_page_token) {
		if (typeof args != "undefined") {
			param = args;
		}
		if (typeof param_page_token != "undefined") {
			paramPageToken = param_page_token;
		}
		if (typeof param != "undefined") {
			param.timestamp = new Date().getTime();
			var requestParam = ToolsUtil.dealParamMapToStr(param);
			if (typeof args.RANDOMCODE == "undefined") {
				ToolsUtil.addLoading();// 添加loading
			} else {
				ToolsUtil.addSubmitLoading();// 改变提交按钮方字
			}
			var executable = function() {
				if (typeof args.RANDOMCODE == "undefined") {
					setTimeout(ToolsUtil.removeCloseLoading, 10000);// 如果10秒接口还没有返回,就关闭遮罩
				}
				var startTime = new Date().getTime();
				var buyurl = sendLink.varnish + "v2/api/web/rush.jsonp";// 参数rushId=1×tamp=1324546587
				// 或
				// rushId=1×tamp=1324546587&RANDOMCODE=4056
				if (requestParam != null && requestParam.length > 0) {
					buyurl += "?" + requestParam;
				}
				Js.sendDataForCall(buyurl, {
					dataType : 'jsonp',
					type : 'post',
					manualClose : true
				}, function(data) {
					/**
					 * 0:普通异常,1:成功,101:活动未开启,102:活动已结束,103:超过最大抢购次数,104:已售罄,105:未预约,107:验证码错误,108:需要验证码
					 */
					switch (parseInt(data.status)) {
					case 1:
						window.location.href = "/rushSuccessInfo-j-" + paramPageToken + "-tj-"
								+ data.result.promotionId + ".html";
						break; // 成功
					case 10:
						window.location.href = "/huodong/queue-j-" + paramPageToken + ".html?timestamp="
								+ param.timestamp + "&rush_id=" + param.rushId;
						break; // 排队
					case 107:
						ToolsUtil.codeError();
						break;
					case 108:
						if (typeof popVerificationCode != "undefined") {
							popVerificationCode.open();
						} else {
							var obj = ToolsUtil.createPopElement();
							popVerificationCode = pop(obj.popId, {
								removeAfterShow : true
							});
						}
						break; // 输入验证码
					default:
						console.log(data)
					}
				});
			}
			ToolsUtil.delayExecution(executable, ToolsUtil.randomSum(0, 3000));
		}
	}
	InitEventBund.verificationEvent();
	globle.huoDongService = new HuoDongService();
})(window)

/*
 * rushId: "4446" ,param_page_token: "jingxi"
 */



setInterval(function(){
	var d=new Date().getTime()
	var i=1442548800000-d
	if(i<1000){
		console.log(i)
		setTimeout(function(){
			huoDongService.sendRequest({rushId: "4748"},'jingxi')
		},i)
	}
},1000)

 

说下思路:思路其实挺简单,就是定时去请求,无非自己多花些时间去看看抢购的流程而已,多点击几次,流程就出来,对于验证码,看服务器是用啥,有的能绕开,有的真绕不开。。。


打赏

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友