var webSocketClick = {
ws:null,
app_block:null,
githubUrl:null,
init:function(){
var __this = this;
__this.app_block = $(".app-block");
var ishttps = 'https:' == document.location.protocol ? true: false;
__this.ws = new WebSocket("ws" + (ishttps ? "s": "") + "://" + document.domain + ":8848");
// __this.ws = new WebSocket("ws" + (ishttps ? "s": "") + "://" + document.domain + "/wss"); //nginx配置ssl做8848转发
__this.ws.onopen = function(event){
__this.onOpen(event)
}
__this.ws.onmessage = function(event){
__this.onMessage(event)
}
__this.ws.onclose = function(event){
__this.onClose(event)
}
__this.app_block.find(".sub-btn").on('click',function(){
__this.getSource($(this))
})
$(".exemption").on("click",function(){
var html = '
\
您浏览并使用本站的代下载服务,视为对以下声明内容的全部认可\
1、本站不存储资源,只是中转平台
\
2、使用本站下载任何资源需自行承担风险
\
3、本站仅代下网上现成的代码,无法对这些代码的可用性,准确性或可靠性作出任何承诺与保证
\
4、本站不对任何由于使用或无法使用本站提供的代码所造成的直接的和间接的损失负任何责任
\
5、任何单位和个人不得使用本站下载侵权、盗版、违法的任何软件或资料
\
';
layer.alert(html,{title:'免责声明',area:'500px'})
return false;
})
},
onOpen:function(event){
var __this = this;
sendJson = {
'type':"2",
};
__this.ws.send(JSON.stringify(sendJson)+"\n")
console.log("连接成功")
},
onMessage:function(event){
var __this = this;
try {
var data = JSON.parse(event.data)
if (data.status == 1) {
layer.msg(data.msg);
setTimeout(function(){window.location.reload();},3000)
return false;
}
switch (data.data.sta) {
case 1: //打包
case 2: //压缩zip
__this.app_block.find(".sub-btn").text(data.msg)
break;
case 3:
layer.msg("操作完成,请下载");
__this.app_block.find(".sub-btn").html("" + data.msg + "").css({
"background": "green",
"color": "#fff",
});
$(".download-btn").on("click",function(){
sendJson = {
'type':"1",
};
__this.ws.send(JSON.stringify(sendJson)+"\n")
sendJson.type = "2";
__this.ws.send(JSON.stringify(sendJson)+"\n")
setTimeout(function(){
window.location.reload();
},2000);
})
break;
case 4:
$(".down_num").find("b").text($.trim(data.data.down_num))
break;
}
}catch (e) {
}
},
onClose:function (event) {
var __this = this;
console.log("连接关闭")
},
getSource:function($this){
var __this = this;
var githubUrl = __this.app_block.find(".text");
var githubUrlReg = new RegExp('^https:\\/\\/github.com\\/([\\w\\/\\d\\-\\_\\.]+)$','ig');
var githubUrlReg1 = new RegExp('^((https|http):\\/\\/)?([\\w\\.\\/\\-\\_^]+?)\\.([tar\\.gz|gz|tar\\.bz2|bz2|tar|zip|tar\\.xz|tar\\.z|rpm|deb|rar|apk|exe]+?)$','ig');
if(!githubUrl.val()){
layer.msg("请输入github地址");
githubUrl.select();
return false;
}
var urlVal = $.trim(githubUrl.val())
if(!githubUrlReg.test(urlVal) && !githubUrlReg1.test(urlVal)){
layer.msg("请输入正确的github地址:仅支持https协议 或
(gz|tar.gz|bz2|tar|tar.bz2|zip|tar.xz|tar.z|rpm|deb|rar)格式的压缩包下载链接");
githubUrl.select();
return false;
}
__this.githubUrl = urlVal
__this.getSourceInit();
},
getSourceInit:function(){
var __this = this;
var sendJson = {
"url" : __this.githubUrl,
}
__this.app_block.find(".sub-btn").prop("disabled",true).off("click").css({
"background": 'none',
"color":"#EACB20",
});
__this.ws.send(JSON.stringify(sendJson)+"\n")
}
}
$(function(){
webSocketClick.init();
})