以下就是小米搶購時的JS程式碼,分析一下就可以用它實現一個紅米搶購軟體,大家根據這個程式碼實現自己想要的功能吧!
return0>=d?["00小時00分00秒",!0]:h>0?(window._timestr=h "天" forNum(i[2]) "時" forNum(i[1 ]) "分" forNum(i[0]) "秒",[h "天" forNum(i[2]) "時" forNum (i[1 ]) "分" forNum(i[0]) "秒",!1]):(window._timestr=forNum( i[2]) "時" forNum(i[1]) "分" forNum(i[0]) "秒",[forNum(i[2]) "時" forNum (i[1]) "分" forNum(i[0]) "秒",!1])}
functionWebEventTrigger(a, b){
嘗試{
if(WE&&WE .trigger)
return"string"!=typeofb&&(b=JSON.stringify(b)),WE.trigger(a,b)
} catch(c){}
返回! 1
}
functionisApp(){
嘗試{
if(WE)return! 0
}catch(a){}
return!1
}
varquickBuy={init:function(){
vara=this;
this.config={},
this.config.hdurl={miphone:" AB3F58343496FD4983D /tip_SaledOverAll.html",mibox:"電視/B1CF7B57BFEAE1EBC37700694C123D894C668BF11F6F25D59311FD3694894C668BF11F6F25D59311FD369411F6 26C5A9B7676E57BE3D47 959D6557545E8A339/tip_SaledOverAll.html"
},
this.config. mode="",
this.mode={miphone :!1,mibox:!1},
this.modeModals={miphone:"#modal_miphone",mibox:"#modal_mibox"},
this.modeCookies={miphone:"xm_hd_so_phone",mibox:" xm_hd_so_box"},
this.config.startDate= newDate("10/15/201312:00:00"),
this.mas🎜 ("#maskLoad"),
this.config. minSignTime=120,
this.results=null,
this.timer=null,
this.timeCount=Math.floor(Math.abs (10*Math.random()-5)) 5;
varb=!0;
for(varcinthis.mode)
$.cookie(this.modeCookies[c])||(b= !1),$.cookie(this.modeCookies[c] )&&this.saleOut();
b||($.cookie("xm_diff_hd")?(this.config.serverTime=this.localTime() parseInt ($.cookie("xm_diff_hd"),10),this .config.serverTime>=parseInt(this.config.startDate/1e3,10)?$.cookie("xm_hd_so_phone")&&$.cookie("xm_hd_so_box") ||this.getData(!0,function(){a .goBuy()}):this.launch()):this.getData())},getData:function(a,b){
varc= this,a=a||null;
b&&( b=b),
$.ajax({
url:"http://tc.hd.xiaomi.com/hdget",
dataType:"jsonp",
jsonpCallback:"hdcontrol ",
逾時:7e3,
beforeSend:function(){
c.maskLoad.css("display","block")} ,
錯誤:function(){
c .maskLoad.css("display","none"),
alert("伺服器壓力山大,請您重新刷新頁面! ")
},
success:function(d){
if(c.maskLoad.css("display","none"),"undefined"==typeofd)
returnalert("伺服器壓力山大,請您重新刷新頁面!"),
void0;
if(c.results=d,a||(c.config.serverTime=c.results.stime),!$.cookie("xm_diff_hd")){
vare=parseInt(c.config.serverTime-c.localTime(),10);
$.cookie("xm_diff_hd",e,{
路徑:"/",
網域:".xiaomi .com",
中間:1
})
}
if(a){
c.initialData();
for(varfinc.mode)
if( c.config[f].hdstart===!1&&c.config[f].hdstop===!0){
varg=c.modeCookies[f];
$.cookie(g,1, {路徑:"/",網域:".xiaomi.com",過渡時間:1}),
c.saleOut()
}elseb&&b.call()
}
a|| c .launch()
}})},
initialData:function(){
this.config.allow=this.results.status.allow,
this.config.mibox=this. results .status.mibox,
this.config.miphone=this.results.status.miphone
},
launch:function(){
this.timeStart(),
this.倒數計時()
},
timeStart:function(){
vara=this,
b=1e3*a.config.serverTime,
c=a.config.startDate,
d=c-1e3*60*a.config.minSignTime;
b>=d&&c>b&&!isApp()?
$.cookie("userId")?
($("#view_button " ).css("可見性","隱藏"),
$("#purchase_btn_1").html('
$("#purchase_btn_2").html('
},
toLogin:function(){
$("#view_button").css ("可見性","隱藏"),
$("#purchase_btn_1").html('
$("#purchase_btn_2").html('
$("#purchase_btn_2").find(".btn_02").on("click",function(){
WebEventTrigger("login",null)||(window .location.href="http:/ / /m.xiaomi.com/mshopapi/index.php/v1/authorize/sso?client_id=180100031013&callback="encodeURIComponent(window.location.href))}
)},
倒數計時>倒數值
vara=this,
b=null,
c=a.config.startDate;
b=a.config.serverTime?a.config.serverTime: parseInt( newDate/1e3,10) ;
vard=c-1e3*60*a.config.minSignTime,
e=setInterval(function(){
varf=countdown(b,c);
f[1]?( clearInterval (e),$("#countdown").css("顯示","無"),a.toBuy()):(1e3*b!=d||isApp( )||($.cookie( " userId")?($("#view_button").css("可見性","隱藏"),
$("#purchase_btn_1").html('
$("#purchase_btn_2"). html('
$("#countdown") .html( f[0])},1e3)},
toBuy:function(){
vara=this;
a.goBuy();
varb=a.modeModals[a. config.mode];
$(b).find(".close").on("click",function(){
$(b).css("顯示","無"),
window.clearTimeout(a.timer),
a.timer=null})},
saleOut:function(){
vara={miphone:"#purchase_btn_2",mibox:"# buy_btn_1"},
b={miphone:"http://t.hd.xiaomi.com/r/?_a= payment_check&_m=1",mibox:"http://m.xiaomi.com/index. html#ac=home&op=addcart&product_id=2505"},
c={miphone:"支付小米手機3",mibox:"小米支付電視"};
for(vardinthis.mode)
$. cookie(this.modeCookies[d])&&($(a[d]).html('
$(this.modeModals[d]).is(":visible")&&$(this.modeModals[d]).find(".close").click())},
localTime:function(){
vara=parseInt( newDate/1e3,10);
returna
},
toProcess:function(a){
varb=this,
toProcess:function(a){
varb=this,
c =b.timer,
d=b.timeCount||10,e=$(b.modeModals[a]).find(".re_countdown"),
f=$(b.modeModals [a] ).find(".modal_btn"),
g=function(){0==d?(f.html('
b.toBuyGo(b.config.mode),
c&&window.clearTimeout(c),
c=null):(e.html("(" d ")"),
d--,
b.timer=window.setTimeout(function(){g()},1e3))};
c&&window.clearTimeout(c),g() },
goBuy :function(){
vara=this,
b={miphone:"購買小米手機3",mibox:"購買小米電視"},
c={miphone:"#purchase_btn_2",mibox :"#purchase_btn_1"};
for(vardina.mode)
$.cookie(a.modeCookies[d])||($(c[d]).html('
$(c[d]) is(":hidden")&&$ ( c[d]).css("display","block"));
$("#open_button").find(".btn_buy_go").on(" click",function(){a. mode [$(this).attr("資料型別")]=!0;
varb=a.config.mode=$(this).attr("資料型別");
if( $.cookie ("userId"))a.getData(!0,function(){
$(a.modeModals[b]).css("display","block "),
$(a .modeModals[ b]).find(".close").on("click",function(){
$(a.modeModals[b]).css("顯示","無"),window .clearTimeout( a.timer),a.timer=null}),a.toProcess(b)});else{if(WebEventTrigger("login",null)) return;window.location. href=" http://m.xiaomi.com/mshopapi/index.php/v1/authorize/sso?client_id=180100031013&callback="encodeURIComponent(window.location.href)}}) },
toBuyGo:function(a){
varb=this;
window.clearTimeout(b.timer),
b.timer=null,
$(b.modeModals[a] ).find(".modal_btn").find(".into_buy_btn").on("click",function(c){c.preventDefault(),b.getData(!0,function(){
if ($(b.modeModals[a]).css("display","block"),b.config.allow)if(b.config[a].hdurl){
varc="http:// t.hd.xiaomi.com/s/" b.config[a].hdurl "&_m=1";
window.setTimeout(function(){window.location.href=c},500)
}else{
vard="http://p.www.xiaomi.com/m/activities/open/" b.config.hdurl[a];
window.setTimeout(function(){window. location.href=d},500)
}elseif(
b.config[a].hdstart===!1&&b.config[a].hdstop===!0){
vard= "http://p.www.xiaomi.com/m/activities/open/" b.config.hdurl[a];
window.setTimeout(function(){window.location.href=d},500 )
}else$(b.modeModals[a]).find(".modal_btn").html('
b.toProcess(a)
})})}};