common.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. common={};
  2. var issave = false;
  3. common.spider={};
  4. common.form={};
  5. common.maskShow=function(title){
  6. var change=function(){
  7. if(title){
  8. $("#maskTitle").text(title);
  9. }else{
  10. $("#maskTitle").text("加载中");
  11. }
  12. var width=$(window).width();
  13. var height=$("body").height();
  14. $(".mask>.box").css("left",(width-width*0.3)/2);
  15. $(".mask").removeClass("hide").css("height",height);
  16. }
  17. $(window).resize(function(){
  18. change();
  19. });
  20. change();
  21. }
  22. common.maskHide=function(){
  23. $(".mask").addClass("hide");
  24. $(window).unbind("resize");
  25. }
  26. common.setActive=function(name){
  27. $("ul.sidebar-menu li.active").removeClass("active");
  28. $("ul.sidebar-menu li[data="+name+"]").addClass("active");
  29. }
  30. common.form.isCheck=function(id,func){
  31. var ischeck=true;
  32. $("#"+id+" [name]").each(function(){
  33. if(this){
  34. var value=$(this).prop("value");
  35. var name=$(this).prop("name");
  36. if(!value || value==""){
  37. if(func){
  38. func(this);
  39. }else{
  40. ischeck=false;
  41. $(this).css("border-color","red");
  42. $(this).attr("placeholder","此项不能为空")
  43. }
  44. }else{
  45. if($(this).css("border-color")=="rgb(255, 0, 0)"){
  46. $(this).css("border-color","#d2d6de");
  47. }
  48. }
  49. }
  50. })
  51. return ischeck;
  52. }
  53. common.form.serializeArray=function(id){
  54. var serializeArray={};
  55. $("#"+id+" [name]").each(function(){
  56. var name=$(this).prop("name");
  57. var value=$(this).prop("value");
  58. if($(this).get(0).tagName=="TEXTAREA" && $(this).attr("id")){
  59. if($(this).attr("id").indexOf("step1")>-1){
  60. value=editor_1.getValue()
  61. }else if($(this).attr("id").indexOf("step2")>-1){
  62. value=editor_2.getValue()
  63. }else if($(this).attr("id").indexOf("step3")>-1){
  64. value=editor_3.getValue()
  65. }else if($(this).attr("id").indexOf("stepre3")>-1){
  66. value=editor_re3.getValue()
  67. }
  68. }else{
  69. value=$(this).prop("value");
  70. value=value?value:"";
  71. }
  72. serializeArray[name]=value.trim();
  73. });
  74. return serializeArray
  75. }
  76. common.form.submit=function(){}
  77. common.spider.initMirror=function(){
  78. editor_1 = CodeMirror.fromTextArea($("#step1_mirror")[0], {
  79. theme:"solarized light",
  80. lineNumbers: true,
  81. indentUnit:4,
  82. smartIndent:true,
  83. indentWithTabs:true,
  84. extraKeys:{
  85. "F11": function(cm) {
  86. cm.setOption("fullScreen", !cm.getOption("fullScreen"));
  87. },
  88. "F10": function(cm) {
  89. var r=window.confirm("生成代码后原先的将会被覆盖,是否生成?")
  90. if(r){
  91. $("a[for=step1]").eq(0).trigger("click",["editor_1"])
  92. }
  93. },
  94. "F9":function(cm) {
  95. var str_tab = "\t".repeat(cm.getCursor().ch);
  96. var _keyWord = "something";
  97. cm.replaceSelection(`if true then\n${str_tab}\treturn ${_keyWord}\n${str_tab}end`);
  98. },
  99. "F8":function(cm){
  100. var str_tab = "\t".repeat(cm.getCursor().ch);
  101. cm.replaceSelection(`local attachments = com.getFileAttachmentsArrayWithTag(data["href"],"dl","<dl>"..data["contenthtml"].."</dl>",false)\n${str_tab}if table.getn(attachments)>0 then\n\t\t${str_tab}data["projectinfo"]={\n\t\t${str_tab}["attachments"]=attachments\n\t${str_tab}}\n${str_tab}end`);
  102. },
  103. "Esc": function(cm) {
  104. if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
  105. }
  106. }
  107. });
  108. editor_2 = CodeMirror.fromTextArea($("#step2_mirror")[0], {
  109. theme:"solarized light",
  110. lineNumbers: true,
  111. indentUnit:4,
  112. smartIndent:true,
  113. indentWithTabs:true,
  114. extraKeys:{
  115. "F11": function(cm) {
  116. cm.setOption("fullScreen", !cm.getOption("fullScreen"));
  117. },
  118. "F10": function(cm) {
  119. var r=window.confirm("生成代码后原先的将会被覆盖,是否生成?")
  120. if(r){
  121. $("a[for=step2]").eq(0).trigger("click",["editor_2"])
  122. }
  123. },
  124. "F9":function(cm) {
  125. var str_tab = "\t".repeat(cm.getCursor().ch);
  126. var _keyWord = "something";
  127. cm.replaceSelection(`if true then\n${str_tab}\treturn ${_keyWord}\n${str_tab}end`);
  128. },
  129. "F8":function(cm){
  130. var str_tab = "\t".repeat(cm.getCursor().ch);
  131. cm.replaceSelection(`local attachments = com.getFileAttachmentsArrayWithTag(data["href"],"dl","<dl>"..data["contenthtml"].."</dl>",false)\n${str_tab}if table.getn(attachments)>0 then\n\t\t${str_tab}data["projectinfo"]={\n\t\t${str_tab}["attachments"]=attachments\n\t${str_tab}}\n${str_tab}end`);
  132. },
  133. "Esc": function(cm) {
  134. if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
  135. }
  136. }
  137. });
  138. editor_3 = CodeMirror.fromTextArea($("#step3_mirror")[0], {
  139. theme:"solarized light",
  140. lineNumbers: true,
  141. indentUnit:4,
  142. smartIndent:true,
  143. indentWithTabs:true,
  144. extraKeys:{
  145. "F11": function(cm) {
  146. cm.setOption("fullScreen", !cm.getOption("fullScreen"));
  147. },
  148. "F10": function(cm) {
  149. var r=window.confirm("生成代码后原先的将会被覆盖,是否生成?")
  150. if(r){
  151. $("a[for=step3]").eq(0).trigger("click",["editor_3"])
  152. }
  153. },
  154. "F9":function(cm) {
  155. var str_tab = "\t".repeat(cm.getCursor().ch);
  156. var _keyWord = "something";
  157. cm.replaceSelection(`if true then\n${str_tab}\treturn ${_keyWord}\n${str_tab}end`);
  158. },
  159. "F8":function(cm){
  160. var str_tab = "\t".repeat(cm.getCursor().ch);
  161. cm.replaceSelection(`local attachments = com.getFileAttachmentsArrayWithTag(data["href"],"dl","<dl>"..data["contenthtml"].."</dl>",false)\n${str_tab}if table.getn(attachments)>0 then\n\t\t${str_tab}data["projectinfo"]={\n\t\t${str_tab}["attachments"]=attachments\n\t${str_tab}}\n${str_tab}end`);
  162. },
  163. "Esc": function(cm) {
  164. if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
  165. }
  166. }
  167. });
  168. editor_re3 = CodeMirror.fromTextArea($("#stepre3_mirror")[0], {
  169. theme:"solarized light",
  170. lineNumbers: true,
  171. indentUnit:4,
  172. smartIndent:true,
  173. indentWithTabs:true
  174. });
  175. editor_4 = CodeMirror.fromTextArea($("#testcontents")[0], {
  176. theme:"abcdef",
  177. lineNumbers: true,
  178. extraKeys:{
  179. "F11": function(cm) {
  180. cm.setOption("fullScreen", !cm.getOption("fullScreen"));
  181. },
  182. "Esc": function(cm) {
  183. if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
  184. }
  185. }
  186. });
  187. editor_1.setSize("auto","auto");
  188. editor_2.setSize("auto","auto");
  189. editor_3.setSize("auto","auto");
  190. editor_re3.setSize("auto","auto");
  191. editor_4.setSize("auto","300");
  192. }
  193. common.spider.setExpert=function(editor,text){
  194. text=text.substr(text.indexOf("function "));
  195. eval(editor+".setValue("+JSON.stringify(text, null, "\t")+")")
  196. }
  197. common.spider.guideActive=function(name,li){
  198. $(".guide button.active").removeClass("active");
  199. $(".guide button[data-mode="+name+"]").addClass("active");
  200. var target=$(li).find("a").attr("href")+ " fieldset";
  201. $(target).each(function(){
  202. if(name==$(this).attr("data-mode")){
  203. $(this).removeClass("hide");
  204. }else{
  205. $(this).addClass("hide")
  206. }
  207. });
  208. }
  209. common.spider.model={}
  210. common.spider.getModel=function(func){
  211. $.ajax({
  212. url:"/center/model",
  213. method:"post",
  214. success:function(d){
  215. common.spider.model=d;
  216. for(var name in d){
  217. var option=$("<option>");
  218. option.val(name);
  219. option.text(name);
  220. $("#model").append(option);
  221. }
  222. if(func){
  223. if (models!="" && typeof(models)!="object"){
  224. models=JSON.parse(models)
  225. func(models.model);
  226. }else{
  227. models={};
  228. func();
  229. }
  230. }
  231. }
  232. })
  233. }
  234. common.spider.json=[];
  235. common.spider.test=function(data,func){
  236. $.ajax({
  237. url:"/center/run",
  238. method:"post",
  239. data:data,
  240. success:function(rs){
  241. if(func){
  242. func(rs);
  243. if(rs["msg"] == "附件中含有乱码"){
  244. showMsg("附件中含有乱码!!",function(){});
  245. }
  246. }else{
  247. common.maskHide();
  248. showMsg("超时",function(){});
  249. }
  250. },
  251. error: function (XMLHttpRequest, textStatus, errorThrown) {
  252. common.spider.error(XMLHttpRequest, textStatus, errorThrown);
  253. },
  254. })
  255. }
  256. common.spider.error = function (XMLHttpRequest, textStatus, errorThrown) {
  257. common.maskHide();
  258. alert(`XMLHttpRequest.status : ${XMLHttpRequest.status}\ntextStatus : ${textStatus}`);
  259. };
  260. common.spider.setModel=function(name,p){
  261. if(!name){
  262. name=$("#model").val();
  263. }
  264. var modelbody=$("#model-body .others");
  265. modelbody.children().remove();
  266. var model=common.spider.model[name];
  267. if(model){
  268. for(var k in model){
  269. var div=$("<div class='form-group'></div>");
  270. var label=$("<label class='col-sm-4 control-label'>"+model[k]+"</label>");
  271. var c_div=$("<div class='col-md-7'></div>");
  272. if(model[k]=="省份"){
  273. var select=$("<select class='form-control' onchange='getcity($(this).val())'></select>")
  274. select.attr("name",k);
  275. select.attr("id",k);
  276. for(var i in common.spider.json){
  277. var field=common.spider.json[i];
  278. for(name in field){
  279. var option=$("<option value='"+name+"'>"+name+"</option>")
  280. select.append(option);
  281. }
  282. }
  283. modelbody.append(div.append(label).append(c_div.append(select)));
  284. }else if(model[k]=="城市"){
  285. var select=$("<select class='form-control' onchange='cleardistrict()'></select>")
  286. select.attr("name",k);
  287. select.attr("id",k);
  288. modelbody.append(div.append(label).append(c_div.append(select)));
  289. }else{
  290. var input=$("<input type='text' class='form-control'/>");
  291. input.attr("name",k);
  292. input.attr("id",k);
  293. input.attr("placeholder",model[k]);
  294. modelbody.append(div.append(label).append(c_div.append(input)));
  295. }
  296. }
  297. $("#area").prepend('<option value="">--请选择--</option>')
  298. if(models!=""){
  299. if(typeof(models)!="object"){
  300. models=JSON.parse(models);
  301. }
  302. for(var k in models){
  303. if(p && k=="model"){
  304. }else if(k=="city"){
  305. getcity(models["area"],models[k])
  306. if(models[k]!=""){
  307. $("#"+k).prepend('<option selected value="'+models[k]+'">'+models[k]+'</option>')
  308. }
  309. }else{
  310. $("#"+k).val(models[k]);
  311. }
  312. }
  313. }
  314. }
  315. return true
  316. }
  317. function getcity(area,city){
  318. $("#city").find("option").remove();
  319. $("#city").append('<option value="">--请选择--</option>')
  320. $.ajax({
  321. url:"/center/getCity",
  322. method:"post",
  323. data:{"area":area},
  324. success:function(r){
  325. for(var i in r){
  326. if(r[i]!=city){
  327. $("#city").append('<option value="'+r[i]+'">'+r[i]+'</option>')
  328. }
  329. }
  330. }
  331. })
  332. }
  333. function cleardistrict(){
  334. $("#district").val("");
  335. }
  336. common.spider.upstate=function(code){
  337. if(!issave){
  338. alert("请保存爬虫!")
  339. return
  340. }
  341. var id=$("#spiderCode").val();
  342. showConfirm("是否提交?\n完成后将不能更改", function() {
  343. common.spider.save_unnew();
  344. $.get("/center/spider/upstate?state=1&code="+id,function(r){
  345. if (r["istotask"]){
  346. window.location.href = "/center/task/edit/"+r["taskid"]+"__2";
  347. }else{
  348. $(window).unbind('beforeunload');
  349. window.location.href = "/center";
  350. }
  351. })
  352. })
  353. }
  354. common.spider.save=function(a,b){
  355. var base=common.form.serializeArray("baseinfo");
  356. if(window.location.pathname=="/center/spider"){
  357. $.get("/center/spider/isrepeat?code="+base["Base.SpiderCode"],function(r){
  358. if(r=="y"){
  359. var y=window.confirm("检测到您处于新建页面,爬虫代码已重复,确定覆盖已有爬虫?")
  360. if(!y){
  361. return false
  362. }
  363. }
  364. })
  365. }
  366. common.spider.save_unnew();
  367. }
  368. common.spider.upload=function(code){
  369. var url="/center/spider/upstate?state=3&code="+code;
  370. showConfirm("审核通过?", function() {
  371. common.maskShow("正在处理...");
  372. $.get(url,function(res){
  373. common.maskHide();
  374. if(res["err"]==""){
  375. showTip("处理成功",500,function(){});
  376. window.location.href = "/center";
  377. }else{
  378. showMsg(res["err"],function(){});
  379. }
  380. })
  381. });
  382. }
  383. common.spider.save_unnew=function(){
  384. var base=common.form.serializeArray("baseinfo");
  385. var step1=common.form.serializeArray("step1");
  386. var step2=common.form.serializeArray("step2");
  387. var step3=common.form.serializeArray("step3");
  388. var stepre3=common.form.serializeArray("stepre3");
  389. step1["Step1.types"]=$("[href='#tab_1']").parent().attr("data-mode");
  390. step1["Step1.types"]=step1["Step1.types"]=="guide"?"0":"1"
  391. step1["Step2.types"]=$("[href='#tab_2']").parent().attr("data-mode");
  392. step1["Step2.types"]=step1["Step2.types"]=="guide"?"0":"1"
  393. step1["Step3.types"]=$("[href='#tab_3']").parent().attr("data-mode");
  394. step1["Step3.types"]=step1["Step3.types"]=="guide"?"0":"1"
  395. stepre3["StepRe3.Checked"] = $("#copyTab").prop('checked');//是否复制三级页
  396. var all=$.extend({},base,step1,step2,step3,stepre3);
  397. $.ajax({
  398. url:"/center/save",
  399. method:"post",
  400. data:all,
  401. success:function(r){
  402. if(r["msg"]=="保存成功"){
  403. issave = true;
  404. common.spider.saveTip("保存成功");
  405. if(window.location.pathname=="/center/spider"){
  406. $(window).unbind('beforeunload');
  407. window.location.href="/center/spider/edit/"+r["code"]+"__";
  408. }
  409. $(window).unbind('beforeunload');
  410. }else{
  411. showMsg(r,function(){});
  412. }
  413. }
  414. })
  415. }
  416. common.spider.saveTip=function(t){
  417. var d=new Date();
  418. t=t+" "+d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+(d.getMinutes()<10?"0"+d.getMinutes():d.getMinutes())+":"+(d.getSeconds()<10?"0"+d.getSeconds():d.getSeconds());
  419. $(".callout-a").text(t);
  420. }
  421. common.passwords=function(){
  422. if($("#passwords").parent().parent().hasClass("hide")){
  423. $("#passwords").parent().parent().removeClass("hide");
  424. return;
  425. }else{
  426. var v=$("#passwords").val();
  427. if(!v || v==""){
  428. alert("密码不能为空")
  429. }else{
  430. $.get("/center/password?password="+v,function(r){
  431. alert(r);
  432. $("#passwords").parent().parent().addClass("hide");
  433. })
  434. window.location.href="/center/logout"
  435. }
  436. }
  437. }
  438. function setCookie(name,value){
  439. var expdate = new Date(); //初始化时间
  440. expdate.setTime(expdate.getTime() + 30 * 60 * 1000); //时间
  441. document.cookie = name+"="+value+";expires="+expdate.toGMTString()+";path=/";
  442. }
  443. function getCookie(c_name){
  444. if (document.cookie.length>0){
  445. c_start=document.cookie.indexOf(c_name + "=")
  446. if (c_start!=-1){
  447. c_start=c_start + c_name.length+1
  448. c_end=document.cookie.indexOf(";",c_start)
  449. if (c_end==-1) c_end=document.cookie.length
  450. return unescape(document.cookie.substring(c_start,c_end))
  451. }
  452. }
  453. return ""
  454. }
  455. function statusbox(){
  456. if($(".checkstatusbox").is(":hidden")){
  457. $(".checkstatusbox").removeClass("hide");
  458. }else{
  459. $(".checkstatusbox").addClass("hide");
  460. }
  461. }
  462. common.spider.checkStatus=function(t,code){
  463. var node = $(t).parent().find("input[type='radio']:checked").val()
  464. common.maskShow("正在测试,这可能需要一点时间");
  465. $.get("/center/spider/pass?code="+code+"&node="+node,function(r){
  466. if(r==null){
  467. r=[false,false,false,false]
  468. showMsg("脚本解析失败!!",function(){});
  469. }
  470. var allstatus=true;
  471. if(r[0]){
  472. $("#status_base").removeClass("text-danger").addClass("text-success").text("已通过");
  473. }else{
  474. $("#status_base").removeClass("text-success").addClass("text-danger").text("未通过");
  475. allstatus=false;
  476. }
  477. if(r[1]){
  478. $("#status_time").removeClass("text-danger").addClass("text-success").text("已通过");
  479. }else{
  480. $("#status_time").removeClass("text-success").addClass("text-danger").text("未通过");
  481. allstatus=false;
  482. }
  483. if(r[2]){
  484. $("#status_list").removeClass("text-danger").addClass("text-success").text("已通过");
  485. }else{
  486. $("#status_list").removeClass("text-success").addClass("text-danger").text("未通过");
  487. allstatus=false;
  488. }
  489. if(r[3]){
  490. $("#status_content").removeClass("text-danger").addClass("text-success").text("已通过");
  491. }else{
  492. $("#status_content").removeClass("text-success").addClass("text-danger").text("未通过");
  493. allstatus=false;
  494. }
  495. if(allstatus){
  496. $("#status_all").removeClass("text-danger").addClass("text-success").text("已通过");
  497. }else{
  498. $("#status_all").removeClass("text-success").addClass("text-danger").text("未通过");
  499. }
  500. $(".callout-a").text("下载请求:"+r[4]+"次,时间函数:"+r[5]+"次,列表函数:"+r[6]+"次 "+r[8]+"条信息,内容函数:"+r[7]+"次");
  501. common.maskHide();
  502. $("#jsonClick")[0].click();
  503. //window.open("/center/spider/json");
  504. //window.open("https://editor.qmx.top/center/spider/json");
  505. })
  506. }
  507. common.spider.checkTime=function(code){
  508. $.ajax({
  509. url:"/center/spider/checktime",
  510. method:"post",
  511. data:{"code":code},
  512. success:function(b){
  513. if(b){
  514. showMsg("success",function(){});
  515. ttable.ajax.reload( null, false );
  516. }
  517. }
  518. })
  519. }
  520. Date.prototype.format=function(format){var o={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),"S":this.getMilliseconds()};if(/(y+)/.test(format)){format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))}for(var k in o){if(new RegExp("("+k+")").test(format)){format=format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+o[k]).substr((""+o[k]).length))}}return format};function getDate(l){var date=new Date(l);pattern="yyyy-MM-dd hh:mm:ss";return date.format(pattern)};