basic.js 13 KB

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