/** * * Basic * 所有页面的共用基础功能 * * */ function utils_get_param(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null)return unescape(r[2]); return null; } function reloadPage(){ location.reload() } let Queue = {THRESHOLD: 1000, IS_LOADED : false, nowuse : 0, SVR_URL:"http://localhost:8803/server/", groupsCount : 0, groups:[], shouldStop : false, backup:["http://localhost:8803/server/","/server/","http://172.30.84.3:8803/server/","https://1.niimei.com/server/"] ,list:[]}; let Users = {list : []}; let Sectors = {list : []}; function get_server_url() { let output = $.ajax({url: Queue.SVR_URL + "/fcWorker/list?userId=1", timeout:1000 }).fail(on_check_fail).done(function(){ Queue.IS_LOADED = true; console.warn("Using SERVER : "); console.warn(Queue.SVR_URL); setTimeout(parse_queue, 50); }); } function on_check_fail(){ if(Queue.nowuse === Queue.backup.length - 1){ console.warn("Backup Failed : " + Queue.backup[Queue.nowuse]); console.warn(this); var out = ""; Queue.backup.forEach(function(val,index,arr){ out += val + "\n"; }); send_alert("抱歉, 服务器连接失败! \n 请确认 " + out + "其中之一可用"); }else{ Queue.nowuse ++; Queue.SVR_URL = Queue.backup[Queue.nowuse]; $.ajax({url: Queue.SVR_URL + "/fcWorker/list?userId=1", timeout:1000 }).fail(on_check_fail).done(function(){ Queue.IS_LOADED = true; console.warn("Using SERVER : "); console.warn(Queue.SVR_URL); setTimeout(parse_queue, 50); }); } } function get_data(url, callback) { Queue.list.push({url:url, callback:callback, gid:-1}); } function new_data_group(groupName, callback){ var gid = -1; Queue.groups.forEach(function(val,index,arr){ if(val.groupName === groupName){ gid = val.gid; } }); if(gid === -1) { gid = Queue.groupsCount; Queue.groupsCount++; Queue.groups.push({ gid: gid, groupName:groupName, callme: callback, counter:0 }); } return gid; } function group_get_data(groupName, url, callback){ var gid = -1; Queue.groups.forEach(function(val,index,arr){ if(val.groupName === groupName){ gid = val.gid; } }); if(gid === -1) { send_alert("Group 方法使用错误!!!") } Queue.list.push({url:url, callback:callback, gid:gid}); } function parse_queue(){ console.log("Queue Length : " + Queue.list.length); if(Queue.list.length > 15){ for(i = 0; i < Queue.list.length; i++) { Queue.list.pop(); } } if(Queue.list.length > 0){ let one = Queue.list.pop(); if(one.gid === -1) { request_data(one.url, one.callback); }else{ g_request_data(one.url, one.callback, one.gid); } } if(Queue.shouldStop == false) { setTimeout(parse_queue, 50); } } function request_data(url, callback){ $.ajax({ url: Queue.SVR_URL + url, apiName: url, sendTime: new Date(), callme: callback }).done(on_dat_success).fail(on_fail); } function g_request_data(url, callback, gid){ $.ajax({ url: Queue.SVR_URL + url, apiName: url, sendTime: new Date(), gid: gid, callme: callback }).done(on_gp_success).fail(on_fail); } function on_gp_success(a,b) { var gid = this.gid; if(check_if_success(a,b)) { this.callme(a,b); Queue.groups.forEach(function(v,i,a){ if(v.gid === gid){ v.counter ++; v.callme(v); } }); } } function on_dat_success(a,b){ if(check_if_success(a,b)) { this.callme(a,b); } } function check_if_success(a,b){ if(typeof a === "string"){ try { a = JSON.parse(a); }catch (e) { console.warn("=---------- 异常 -----------="); console.warn(a); console.warn("=----------------------------="); send_alert("信息失败 : "+e); return; } }else if(typeof a === "undefined"){ console.warn("=---------- 异常 -----------="); console.warn(a); console.warn("=------------------------------="); send_alert("信息失败 : "+e); return; } if(a.ret === "10000") { return true; }else{ console.trace(); console.error(b); console.error(JSON.stringify(a)); send_alert("查询失败 : 请参阅控制台输出! "); } return false; } function on_fail(msg) { send_alert("很抱歉,网络错误,请查看Console"); console.warn("----- 网络请求失败 ---------"); console.warn(JSON.stringify(this)); console.warn("----------------------------"); Queue.shouldStop = true; } function first_parse(ajax, obj) { if (typeof obj === "string") { obj = JSON.parse(obj); } if (typeof obj !== "object") { console.warn(ajax); throw "Error ! Parsing JSON failed ." + JSON.stringify(obj); } if (obj.ret === "10000") { return obj.model; } else { console.warn(ajax); throw "Error ! Server returned error." + JSON.stringify(obj); } } function on_document_load() { if(typeof (page_document_load) !== "undefined"){ page_document_load(); } if(typeof (resizeAll) !== "undefined"){ resizeAll(); } } function send_alert(a, b){ if(Queue.shouldStop) return; $("#mpAlert").html(a); $("#mpAlert").show(); return; var q = "\n
\n
\n
\n
\n
New message
\n \n
\n
\n
\n
\n \n \n
\n
\n \n \n
\n
\n
\n
\n \n \n
\n
\n
\n
"; //var model = document.createElement("div"); if($("#modalContainer").length <= 0){ alert(a); return ; } $("#modalContainer").html(q); var modalO = $("alertModal"); if(modalO.length === 0){ alert(a); }else{ $("#alertModelLabel").text(b); $("#message-text").text(b); modalO.modal('show'); } } function getUserName(uid){ var uname = null; Users.list.forEach(function(val, aid, arr){ if(val.worker.userId == uid){ uname = val.worker.userName; } }); if(uname == null) { get_data("fcWorker/list?userId=" + uid, on_username_retn); return "[用户:"+uid+"]"; }else{ return uname; } } function on_username_retn(obj, sta) { var uid = this.url.substring(this.url.indexOf("userId=") + 7); let ruid = parseInt(uid); var out = first_parse(this, obj); if(out.list.length > 0){ Users.list.push({ uid : ruid, worker: out.list[0] }); } } function getSectorName(sectorId){ var secName = null; Sectors.list.forEach(function(val, aid, arr){ if(val.sector.sectorId === sectorId){ secName = val.sector.sectorName; } }); if(secName == null) { get_data("fcSector/list?sectorId=" + sectorId, on_secname_retn); return "[工位:"+sectorId+"]"; }else{ return secName; } } function on_secname_retn(obj, sta) { var secid = this.url.substring(this.url.indexOf("sectorId=") + 9); let rsecid = parseInt(secid); var out = first_parse(this, obj); if(out.list.length > 0){ Sectors.list.push({ secid : rsecid, sector: out.list[0] }); } } function getSectorId(spotId){ var secId = null; Sectors.list.forEach(function(val, aid, arr){ if(val.sector.spotId === spotId){ secId = val.sector.sectorId; } }); if(secId == null) { get_data("fcSectorSpot/list?spotId=" + spotId, on_secid_retn); return -1; }else{ return secId; } } function on_secid_retn(obj, sta) { var spotId = this.url.substring(this.url.indexOf("spotId=") + 7); let rsecid = parseInt(spotId); var out = first_parse(this, obj); if(out.list.length > 0){ Sectors.list.push({ secid : rsecid, sector: out.list[0] }); } } /** * Calc b/a+b * 100(precision = 2), when b is zero, return zero * @param a (pass) * @param b (fail) */ function get_factor(a,b){ if(a+b === 0){ return 0; }else{ return Math.floor(10000* b / (a+b) ) / 100; } } $(document).ready(on_document_load); get_server_url();