basic.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. console.warn(JSON.stringify(this));
  201. console.warn("-----------msg-------------");
  202. console.warn(JSON.stringify(msg));
  203. console.warn("-----------sta--------------");
  204. console.warn(JSON.stringify(sta));
  205. console.warn("----------------------------");
  206. }
  207. function first_parse(ajax, obj) {
  208. if (typeof obj === "string") {
  209. obj = JSON.parse(obj);
  210. }
  211. if (typeof obj !== "object") {
  212. console.warn(ajax);
  213. throw "Error ! Parsing JSON failed ." + JSON.stringify(obj);
  214. }
  215. if (obj.ret === "10000") {
  216. return obj.model;
  217. } else {
  218. console.warn(ajax);
  219. throw "Error ! Server returned error." + JSON.stringify(obj);
  220. }
  221. }
  222. function on_document_load() {
  223. if(typeof (page_document_load) !== "undefined"){
  224. page_document_load();
  225. }
  226. if(typeof (resizeAll) !== "undefined"){
  227. resizeAll();
  228. }
  229. }
  230. var AlertClosingTimeout = 0;
  231. function send_alert(a, b){
  232. if(Queue.shouldStop) return;
  233. $("#mpAlert").html(a);
  234. $("#mpAlert").show();
  235. if(AlertClosingTimeout > 0) {
  236. clearTimeout(AlertClosingTimeout);
  237. }
  238. AlertClosingTimeout = setTimeout(function(){
  239. if ($("#mpAlert").html().indexOf("<") >= 0) {
  240. return;
  241. }
  242. $("#mpAlert").hide();
  243. }, 10000);
  244. return;
  245. 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>";
  246. //var model = document.createElement("div");
  247. if($("#modalContainer").length <= 0){
  248. alert(a);
  249. return ;
  250. }
  251. $("#modalContainer").html(q);
  252. var modalO = $("alertModal");
  253. if(modalO.length === 0){
  254. alert(a);
  255. }else{
  256. $("#alertModelLabel").text(b);
  257. $("#message-text").text(b);
  258. modalO.modal('show');
  259. }
  260. }
  261. function getUserName(uid){
  262. var uname = null;
  263. Users.list.forEach(function(val, aid, arr){
  264. if(val.worker.staffId === uid){
  265. uname = val.worker.staffName;
  266. }
  267. });
  268. if(uname == null) {
  269. get_data("oaStaff/list?staffId=" + uid, on_username_retn);
  270. return "[用户:"+uid+"]";
  271. }else{
  272. return uname;
  273. }
  274. }
  275. function on_username_retn(obj, sta) {
  276. var uid = this.url.substring(this.url.indexOf("staffId=") + 8);
  277. let ruid = parseInt(uid);
  278. var out = first_parse(this, obj);
  279. if(out.list.length > 0){
  280. Users.list.push({
  281. uid : ruid, worker: out.list[0]
  282. });
  283. }
  284. }
  285. function getSectorName(sectorId){
  286. var secName = null;
  287. Sectors.list.forEach(function(val, aid, arr){
  288. if(val.sector.sectorId === sectorId){
  289. secName = val.sector.sectorName;
  290. }
  291. });
  292. if(secName == null) {
  293. get_data("fcSector/list?sectorId=" + sectorId, on_secname_retn);
  294. return "[工位:"+sectorId+"]";
  295. }else{
  296. return secName;
  297. }
  298. }
  299. function on_secname_retn(obj, sta) {
  300. var secid = this.url.substring(this.url.indexOf("sectorId=") + 9);
  301. let rsecid = parseInt(secid);
  302. var out = first_parse(this, obj);
  303. if(out.list.length > 0){
  304. Sectors.list.push({
  305. secid : rsecid, sector: out.list[0]
  306. });
  307. }
  308. }
  309. function getSectorId(spotId){
  310. var secId = null;
  311. Sectors.list.forEach(function(val, aid, arr){
  312. if(val.sector.spotId === spotId){
  313. secId = val.sector.sectorId;
  314. }
  315. });
  316. if(secId == null) {
  317. get_data("fcSectorSpot/list?spotId=" + spotId, on_secid_retn);
  318. return -1;
  319. }else{
  320. return secId;
  321. }
  322. }
  323. function on_secid_retn(obj, sta) {
  324. var spotId = this.url.substring(this.url.indexOf("spotId=") + 7);
  325. let rsecid = parseInt(spotId);
  326. var out = first_parse(this, obj);
  327. if(out.list.length > 0){
  328. Sectors.list.push({
  329. secid : rsecid, sector: out.list[0]
  330. });
  331. }
  332. }
  333. /**
  334. * Calc b/a+b * 100(precision = 2), when b is zero, return zero
  335. * @param a (pass)
  336. * @param b (fail)
  337. */
  338. function get_factor(a,b){
  339. if(a+b === 0){
  340. return 0;
  341. }else{
  342. return Math.floor(10000* b / (a+b) ) / 100;
  343. }
  344. }
  345. function setCookieWithTimeout(name, value, liveMinutes) {
  346. if (liveMinutes == undefined || liveMinutes == null) {
  347. liveMinutes = 60 * 2;
  348. }
  349. if (typeof (liveMinutes) != 'number') {
  350. liveMinutes = 60 * 2;//默认120分钟
  351. }
  352. var minutes = liveMinutes * 60 * 1000;
  353. var exp = new Date();
  354. exp.setTime(exp.getTime() + minutes + 8 * 3600 * 1000);
  355. //path=/表示全站有效,而不是当前页
  356. document.cookie = name + "=" + value + ";path=/;expires=" + exp.toUTCString();
  357. }
  358. function setCookie(c_name,value,expiredays)
  359. {
  360. var exdate=new Date()
  361. exdate.setDate(exdate.getDate()+expiredays)
  362. document.cookie=c_name+ "=" +escape(value)+
  363. ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
  364. }
  365. function getCookie(c_name)
  366. {
  367. if (document.cookie.length>0)
  368. {
  369. c_start=document.cookie.indexOf(c_name + "=")
  370. if (c_start!=-1)
  371. {
  372. c_start=c_start + c_name.length+1
  373. c_end=document.cookie.indexOf(";",c_start)
  374. if (c_end==-1) c_end=document.cookie.length
  375. return unescape(document.cookie.substring(c_start,c_end))
  376. }
  377. }
  378. return "";
  379. }
  380. var userId = utils_get_param("userId");
  381. function getUserId() {
  382. console.log("Original USERID : " + userId);
  383. var use = getCookie("doibyUser");
  384. if (use == null || use === undefined || use.length <= 0) {
  385. location.href = "sign-in1.html?from=entrance-1&msg=nologin-mustauth";
  386. return;
  387. }
  388. console.log("Got UID From COOKIE : " + use);
  389. try{
  390. var mm = parseInt(use);
  391. if(mm <= 0){
  392. }else{
  393. userId = mm;
  394. }
  395. }catch (e) {
  396. location.href = "sign-in1.html?from=entrance-1&msg=cookie-wrong-format";
  397. return;
  398. }
  399. console.log("Got UserId Eventually : " + userId);
  400. }
  401. $(document).ready(on_document_load);
  402. get_server_url();