basic.js 13 KB

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