basic.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. *
  3. * Basic
  4. * 所有页面的共用基础功能
  5. *
  6. * */
  7. function utils_get_param(name){
  8. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  9. var r = window.location.search.substr(1).match(reg);
  10. if(r!=null)return unescape(r[2]); return null;
  11. }
  12. function reloadPage(){
  13. location.reload()
  14. }
  15. 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:[], temporary:0, errorCount:0};
  16. let Users = {list : []};
  17. let Sectors = {list : []};
  18. function get_server_url() {
  19. let output = $.ajax({url: Queue.SVR_URL + "/fcFlow/list", timeout:1000 }).fail(on_check_fail).done(function(){
  20. Queue.IS_LOADED = true;
  21. console.warn("Using SERVER : ");
  22. console.warn(Queue.SVR_URL);
  23. setTimeout(parse_queue, 50);
  24. });
  25. }
  26. function on_check_fail(){
  27. if(Queue.nowuse === Queue.backup.length - 1){
  28. console.warn("Backup Failed : " + Queue.backup[Queue.nowuse]);
  29. console.warn(this);
  30. var out = "";
  31. Queue.backup.forEach(function(val,index,arr){
  32. out += val + "\n";
  33. });
  34. send_alert("抱歉, 服务器连接失败! \n 请确认 " + out + "其中之一可用");
  35. }else{
  36. Queue.nowuse ++;
  37. Queue.SVR_URL = Queue.backup[Queue.nowuse];
  38. $.ajax({url: Queue.SVR_URL + "/fcFlow/list", timeout:1000 }).fail(on_check_fail).done(function(){
  39. Queue.IS_LOADED = true;
  40. console.warn("Using SERVER : ");
  41. console.warn(Queue.SVR_URL);
  42. setTimeout(parse_queue, 50);
  43. });
  44. }
  45. }
  46. function get_data(url, callback) {
  47. Queue.list.push({url:url, callback:callback, gid:-1});
  48. }
  49. function new_data_group(groupName, callback){
  50. var gid = -1;
  51. Queue.groups.forEach(function(val,index,arr){
  52. if(val.groupName === groupName){
  53. gid = val.gid;
  54. }
  55. });
  56. if(gid === -1) {
  57. gid = Queue.groupsCount;
  58. Queue.groupsCount++;
  59. Queue.groups.push({
  60. gid: gid, groupName:groupName, callme: callback, counter:0
  61. });
  62. }
  63. return gid;
  64. }
  65. function group_get_data(groupName, url, callback){
  66. var gid = -1;
  67. Queue.groups.forEach(function(val,index,arr){
  68. if(val.groupName === groupName){
  69. gid = val.gid;
  70. }
  71. });
  72. if(gid === -1) {
  73. send_alert("Group 方法使用错误!!!")
  74. }
  75. Queue.list.push({url:url, callback:callback, gid:gid});
  76. }
  77. function parse_queue(){
  78. console.log("Queue Length : " + Queue.list.length);
  79. if(Queue.temporary > new Date().getTime()){
  80. for(i = 0; i < Queue.list.length; i++) {
  81. Queue.list.pop();
  82. }
  83. }
  84. Queue.temporary = 0;
  85. if(Queue.list.length > 0){
  86. let one = Queue.list.pop();
  87. if(one.gid === -1) {
  88. request_data(one.url, one.callback);
  89. }else{
  90. g_request_data(one.url, one.callback, one.gid);
  91. }
  92. }
  93. if(Queue.shouldStop == false) {
  94. setTimeout(parse_queue, 50);
  95. }
  96. }
  97. function request_data(url, callback){
  98. $.ajax({
  99. url: Queue.SVR_URL + url,
  100. apiName: url,
  101. sendTime: new Date(),
  102. callme: callback
  103. }).done(on_dat_success).fail(on_fail);
  104. }
  105. function g_request_data(url, callback, gid){
  106. $.ajax({
  107. url: Queue.SVR_URL + url,
  108. apiName: url,
  109. sendTime: new Date(),
  110. gid: gid,
  111. callme: callback
  112. }).done(on_gp_success).fail(on_fail);
  113. }
  114. function on_gp_success(a,b) {
  115. var gid = this.gid;
  116. if(check_if_success(a,b)) {
  117. this.callme(a,b);
  118. Queue.groups.forEach(function(v,i,a){
  119. if(v.gid === gid){
  120. v.counter ++;
  121. v.callme(v);
  122. }
  123. });
  124. }
  125. }
  126. function on_dat_success(a,b){
  127. if(check_if_success(a,b)) {
  128. this.callme(a,b);
  129. }
  130. }
  131. function check_if_success(a,b){
  132. if(typeof a === "string"){
  133. try {
  134. a = JSON.parse(a);
  135. }catch (e) {
  136. console.warn("=---------- 异常 -----------=");
  137. console.warn(a);
  138. console.warn("=----------------------------=");
  139. send_alert("信息失败 : "+e);
  140. return;
  141. }
  142. }else if(typeof a === "undefined"){
  143. console.warn("=---------- 异常 -----------=");
  144. console.warn(a);
  145. console.warn("=------------------------------=");
  146. send_alert("信息失败 : "+e);
  147. return;
  148. }
  149. if(a.ret === "10000") {
  150. return true;
  151. }else{
  152. console.trace();
  153. console.error(b);
  154. console.error(JSON.stringify(a));
  155. send_alert("查询失败 : 请参阅控制台输出! ");
  156. }
  157. return false;
  158. }
  159. function on_fail(msg) {
  160. Queue.errorCount += 1;
  161. if(Queue.errorCount >= 10){
  162. Queue.errorCount = 0;
  163. Queue.temporary = new Date().getTime() + 60*1000;
  164. console.warn("----- Too many failure, temporary stop HTTP ------");
  165. }
  166. send_alert("很抱歉,网络错误,请查看Console");
  167. console.warn("----- 网络请求失败 ---------");
  168. console.warn(JSON.stringify(this));
  169. console.warn("----------------------------");
  170. Queue.shouldStop = true;
  171. }
  172. function first_parse(ajax, obj) {
  173. if (typeof obj === "string") {
  174. obj = JSON.parse(obj);
  175. }
  176. if (typeof obj !== "object") {
  177. console.warn(ajax);
  178. throw "Error ! Parsing JSON failed ." + JSON.stringify(obj);
  179. }
  180. if (obj.ret === "10000") {
  181. return obj.model;
  182. } else {
  183. console.warn(ajax);
  184. throw "Error ! Server returned error." + JSON.stringify(obj);
  185. }
  186. }
  187. function on_document_load() {
  188. if(typeof (page_document_load) !== "undefined"){
  189. page_document_load();
  190. }
  191. if(typeof (resizeAll) !== "undefined"){
  192. resizeAll();
  193. }
  194. }
  195. var AlertClosingTimeout = 0;
  196. function send_alert(a, b){
  197. if(Queue.shouldStop) return;
  198. $("#mpAlert").html(a);
  199. $("#mpAlert").show();
  200. if(AlertClosingTimeout > 0) {
  201. clearTimeout(AlertClosingTimeout);
  202. }
  203. AlertClosingTimeout = setTimeout(function(){
  204. if ($("#mpAlert").html().indexOf("<") >= 0) {
  205. return;
  206. }
  207. $("#mpAlert").hide();
  208. }, 10000);
  209. return;
  210. var q = "<!-- Modal 部分开始 -->\n<div class=\"modal fade\" id=\"alertModal\" tabindex=\"-1\" role=\"dialog\" aria-hidden=\"true\">\n <div class=\"modal-dialog\" role=\"document\">\n <div class=\"modal-content\">\n <div class=\"modal-header\">\n <h5 class=\"modal-title\" id=\"alertModalLabel\">New message</h5>\n <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\">\n <span aria-hidden=\"true\">&times;</span>\n </button>\n </div>\n <div class=\"modal-body\">\n <form>\n <div class=\"form-group\">\n <label for=\"recipient-name\" class=\"col-form-label\">Recipient:</label>\n <input type=\"text\" class=\"form-control\" id=\"recipient-name\">\n </div>\n <div class=\"form-group\">\n <label for=\"message-text\" class=\"col-form-label\">Message:</label>\n <textarea class=\"form-control\" id=\"message-text\"></textarea>\n </div>\n </form>\n </div>\n <div class=\"modal-footer\">\n <button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">Close</button>\n <button type=\"button\" class=\"btn btn-primary\">Send message</button>\n </div>\n </div>\n </div>\n</div>";
  211. //var model = document.createElement("div");
  212. if($("#modalContainer").length <= 0){
  213. alert(a);
  214. return ;
  215. }
  216. $("#modalContainer").html(q);
  217. var modalO = $("alertModal");
  218. if(modalO.length === 0){
  219. alert(a);
  220. }else{
  221. $("#alertModelLabel").text(b);
  222. $("#message-text").text(b);
  223. modalO.modal('show');
  224. }
  225. }
  226. function getUserName(uid){
  227. var uname = null;
  228. Users.list.forEach(function(val, aid, arr){
  229. if(val.worker.userId == uid){
  230. uname = val.worker.userName;
  231. }
  232. });
  233. if(uname == null) {
  234. get_data("fcWorker/list?userId=" + uid, on_username_retn);
  235. return "[用户:"+uid+"]";
  236. }else{
  237. return uname;
  238. }
  239. }
  240. function on_username_retn(obj, sta) {
  241. var uid = this.url.substring(this.url.indexOf("userId=") + 7);
  242. let ruid = parseInt(uid);
  243. var out = first_parse(this, obj);
  244. if(out.list.length > 0){
  245. Users.list.push({
  246. uid : ruid, worker: out.list[0]
  247. });
  248. }
  249. }
  250. function getSectorName(sectorId){
  251. var secName = null;
  252. Sectors.list.forEach(function(val, aid, arr){
  253. if(val.sector.sectorId === sectorId){
  254. secName = val.sector.sectorName;
  255. }
  256. });
  257. if(secName == null) {
  258. get_data("fcSector/list?sectorId=" + sectorId, on_secname_retn);
  259. return "[工位:"+sectorId+"]";
  260. }else{
  261. return secName;
  262. }
  263. }
  264. function on_secname_retn(obj, sta) {
  265. var secid = this.url.substring(this.url.indexOf("sectorId=") + 9);
  266. let rsecid = parseInt(secid);
  267. var out = first_parse(this, obj);
  268. if(out.list.length > 0){
  269. Sectors.list.push({
  270. secid : rsecid, sector: out.list[0]
  271. });
  272. }
  273. }
  274. function getSectorId(spotId){
  275. var secId = null;
  276. Sectors.list.forEach(function(val, aid, arr){
  277. if(val.sector.spotId === spotId){
  278. secId = val.sector.sectorId;
  279. }
  280. });
  281. if(secId == null) {
  282. get_data("fcSectorSpot/list?spotId=" + spotId, on_secid_retn);
  283. return -1;
  284. }else{
  285. return secId;
  286. }
  287. }
  288. function on_secid_retn(obj, sta) {
  289. var spotId = this.url.substring(this.url.indexOf("spotId=") + 7);
  290. let rsecid = parseInt(spotId);
  291. var out = first_parse(this, obj);
  292. if(out.list.length > 0){
  293. Sectors.list.push({
  294. secid : rsecid, sector: out.list[0]
  295. });
  296. }
  297. }
  298. /**
  299. * Calc b/a+b * 100(precision = 2), when b is zero, return zero
  300. * @param a (pass)
  301. * @param b (fail)
  302. */
  303. function get_factor(a,b){
  304. if(a+b === 0){
  305. return 0;
  306. }else{
  307. return Math.floor(10000* b / (a+b) ) / 100;
  308. }
  309. }
  310. function setCookieWithTimeout(name, value, liveMinutes) {
  311. if (liveMinutes == undefined || liveMinutes == null) {
  312. liveMinutes = 60 * 2;
  313. }
  314. if (typeof (liveMinutes) != 'number') {
  315. liveMinutes = 60 * 2;//默认120分钟
  316. }
  317. var minutes = liveMinutes * 60 * 1000;
  318. var exp = new Date();
  319. exp.setTime(exp.getTime() + minutes + 8 * 3600 * 1000);
  320. //path=/表示全站有效,而不是当前页
  321. document.cookie = name + "=" + value + ";path=/;expires=" + exp.toUTCString();
  322. }
  323. function setCookie(c_name,value,expiredays)
  324. {
  325. var exdate=new Date()
  326. exdate.setDate(exdate.getDate()+expiredays)
  327. document.cookie=c_name+ "=" +escape(value)+
  328. ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
  329. }
  330. function getCookie(c_name)
  331. {
  332. if (document.cookie.length>0)
  333. {
  334. c_start=document.cookie.indexOf(c_name + "=")
  335. if (c_start!=-1)
  336. {
  337. c_start=c_start + c_name.length+1
  338. c_end=document.cookie.indexOf(";",c_start)
  339. if (c_end==-1) c_end=document.cookie.length
  340. return unescape(document.cookie.substring(c_start,c_end))
  341. }
  342. }
  343. return "";
  344. }
  345. var userId = utils_get_param("userId");
  346. function getUserId() {
  347. console.log("Original USERID : " + userId);
  348. var use = getCookie("doibyUser");
  349. if (use == null || use === undefined || use.length <= 0) {
  350. location.href = "sign-in1.html?from=entrance-1&msg=nologin-mustauth";
  351. return;
  352. }
  353. console.log("Got UID From COOKIE : " + use);
  354. try{
  355. var mm = parseInt(use);
  356. if(mm <= 0){
  357. location.href = "sign-in1.html?from=entrance-1&msg=cookie-wrong-format";
  358. }else{
  359. userId = mm;
  360. }
  361. }catch (e) {
  362. location.href = "sign-in1.html?from=entrance-1&msg=cookie-wrong-format";
  363. return;
  364. }
  365. console.log("Got UserId Eventually : " + userId);
  366. }
  367. $(document).ready(on_document_load);
  368. get_server_url();