basic.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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_authfail(a,b)){
  128. setCookieWithTimeout("doibyUser","",-10);
  129. location.href = "sign-in1.html?from=entrance-5&msg=server-redirect-detected";
  130. return;
  131. }
  132. if(check_if_success(a,b)) {
  133. this.callme(a,b);
  134. }
  135. }
  136. function check_if_authfail(a){
  137. if(a == null || typeof a !== "string") {
  138. return false;
  139. }else{
  140. if(a.indexOf("login?from=") > 0 || a.startsWith("<!DOCTYPE html>") || a.indexOf("<!-- SME::LOGINPAGE -->") > 0){
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. function check_if_success(a,b){
  147. if(typeof a === "string"){
  148. try {
  149. a = JSON.parse(a);
  150. }catch (e) {
  151. console.warn("=---------- 异常 -----------=");
  152. console.warn(a);
  153. console.warn("=----------------------------=");
  154. send_alert("信息失败 : "+e);
  155. return;
  156. }
  157. }else if(typeof a === "undefined"){
  158. console.warn("=---------- 异常 -----------=");
  159. console.warn(a);
  160. console.warn("=------------------------------=");
  161. send_alert("信息失败 : "+e);
  162. return;
  163. }
  164. if(a.ret === "10000") {
  165. return true;
  166. }else{
  167. console.trace();
  168. console.error(b);
  169. console.error(JSON.stringify(a));
  170. send_alert("查询失败 : 请参阅控制台输出! ");
  171. }
  172. return false;
  173. }
  174. function on_fail(msg, sta) {
  175. Queue.errorCount += 1;
  176. if(Queue.errorCount >= 10){
  177. Queue.errorCount = 0;
  178. Queue.temporary = new Date().getTime() + 30*1000;
  179. Queue.shouldStop = true;
  180. console.warn("----- Too many failure, temporary stop HTTP ------");
  181. send_alert("很抱歉,网络错误数量过多,暂时终止HTTP功能30s");
  182. }
  183. let vurl = "";
  184. try {
  185. vurl = " : "+this.url.substring(7, 12);
  186. }catch (e) {
  187. vurl = "";
  188. }
  189. send_alert("很抱歉,网络错误,请查看 Console" + vurl);
  190. console.warn("----- 网络请求失败 ---------");
  191. console.warn(JSON.stringify(this));
  192. console.warn("-----------msg-------------");
  193. console.warn(JSON.stringify(msg));
  194. console.warn("-----------sta--------------");
  195. console.warn(JSON.stringify(sta));
  196. console.warn("----------------------------");
  197. }
  198. function first_parse(ajax, obj) {
  199. if (typeof obj === "string") {
  200. obj = JSON.parse(obj);
  201. }
  202. if (typeof obj !== "object") {
  203. console.warn(ajax);
  204. throw "Error ! Parsing JSON failed ." + JSON.stringify(obj);
  205. }
  206. if (obj.ret === "10000") {
  207. return obj.model;
  208. } else {
  209. console.warn(ajax);
  210. throw "Error ! Server returned error." + JSON.stringify(obj);
  211. }
  212. }
  213. function on_document_load() {
  214. if(typeof (page_document_load) !== "undefined"){
  215. page_document_load();
  216. }
  217. if(typeof (resizeAll) !== "undefined"){
  218. resizeAll();
  219. }
  220. }
  221. var AlertClosingTimeout = 0;
  222. function send_alert(a, b){
  223. if(Queue.shouldStop) return;
  224. $("#mpAlert").html(a);
  225. $("#mpAlert").show();
  226. if(AlertClosingTimeout > 0) {
  227. clearTimeout(AlertClosingTimeout);
  228. }
  229. AlertClosingTimeout = setTimeout(function(){
  230. if ($("#mpAlert").html().indexOf("<") >= 0) {
  231. return;
  232. }
  233. $("#mpAlert").hide();
  234. }, 10000);
  235. return;
  236. 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>";
  237. //var model = document.createElement("div");
  238. if($("#modalContainer").length <= 0){
  239. alert(a);
  240. return ;
  241. }
  242. $("#modalContainer").html(q);
  243. var modalO = $("alertModal");
  244. if(modalO.length === 0){
  245. alert(a);
  246. }else{
  247. $("#alertModelLabel").text(b);
  248. $("#message-text").text(b);
  249. modalO.modal('show');
  250. }
  251. }
  252. function getUserName(uid){
  253. var uname = null;
  254. Users.list.forEach(function(val, aid, arr){
  255. if(val.worker.staffId === uid){
  256. uname = val.worker.staffName;
  257. }
  258. });
  259. if(uname == null) {
  260. get_data("oaStaff/list?staffId=" + uid, on_username_retn);
  261. return "[用户:"+uid+"]";
  262. }else{
  263. return uname;
  264. }
  265. }
  266. function on_username_retn(obj, sta) {
  267. var uid = this.url.substring(this.url.indexOf("staffId=") + 8);
  268. let ruid = parseInt(uid);
  269. var out = first_parse(this, obj);
  270. if(out.list.length > 0){
  271. Users.list.push({
  272. uid : ruid, worker: out.list[0]
  273. });
  274. }
  275. }
  276. function getSectorName(sectorId){
  277. var secName = null;
  278. Sectors.list.forEach(function(val, aid, arr){
  279. if(val.sector.sectorId === sectorId){
  280. secName = val.sector.sectorName;
  281. }
  282. });
  283. if(secName == null) {
  284. get_data("fcSector/list?sectorId=" + sectorId, on_secname_retn);
  285. return "[工位:"+sectorId+"]";
  286. }else{
  287. return secName;
  288. }
  289. }
  290. function on_secname_retn(obj, sta) {
  291. var secid = this.url.substring(this.url.indexOf("sectorId=") + 9);
  292. let rsecid = parseInt(secid);
  293. var out = first_parse(this, obj);
  294. if(out.list.length > 0){
  295. Sectors.list.push({
  296. secid : rsecid, sector: out.list[0]
  297. });
  298. }
  299. }
  300. function getSectorId(spotId){
  301. var secId = null;
  302. Sectors.list.forEach(function(val, aid, arr){
  303. if(val.sector.spotId === spotId){
  304. secId = val.sector.sectorId;
  305. }
  306. });
  307. if(secId == null) {
  308. get_data("fcSectorSpot/list?spotId=" + spotId, on_secid_retn);
  309. return -1;
  310. }else{
  311. return secId;
  312. }
  313. }
  314. function on_secid_retn(obj, sta) {
  315. var spotId = this.url.substring(this.url.indexOf("spotId=") + 7);
  316. let rsecid = parseInt(spotId);
  317. var out = first_parse(this, obj);
  318. if(out.list.length > 0){
  319. Sectors.list.push({
  320. secid : rsecid, sector: out.list[0]
  321. });
  322. }
  323. }
  324. /**
  325. * Calc b/a+b * 100(precision = 2), when b is zero, return zero
  326. * @param a (pass)
  327. * @param b (fail)
  328. */
  329. function get_factor(a,b){
  330. if(a+b === 0){
  331. return 0;
  332. }else{
  333. return Math.floor(10000* b / (a+b) ) / 100;
  334. }
  335. }
  336. function setCookieWithTimeout(name, value, liveMinutes) {
  337. if (liveMinutes == undefined || liveMinutes == null) {
  338. liveMinutes = 60 * 2;
  339. }
  340. if (typeof (liveMinutes) != 'number') {
  341. liveMinutes = 60 * 2;//默认120分钟
  342. }
  343. var minutes = liveMinutes * 60 * 1000;
  344. var exp = new Date();
  345. exp.setTime(exp.getTime() + minutes + 8 * 3600 * 1000);
  346. //path=/表示全站有效,而不是当前页
  347. document.cookie = name + "=" + value + ";path=/;expires=" + exp.toUTCString();
  348. }
  349. function setCookie(c_name,value,expiredays)
  350. {
  351. var exdate=new Date()
  352. exdate.setDate(exdate.getDate()+expiredays)
  353. document.cookie=c_name+ "=" +escape(value)+
  354. ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
  355. }
  356. function getCookie(c_name)
  357. {
  358. if (document.cookie.length>0)
  359. {
  360. c_start=document.cookie.indexOf(c_name + "=")
  361. if (c_start!=-1)
  362. {
  363. c_start=c_start + c_name.length+1
  364. c_end=document.cookie.indexOf(";",c_start)
  365. if (c_end==-1) c_end=document.cookie.length
  366. return unescape(document.cookie.substring(c_start,c_end))
  367. }
  368. }
  369. return "";
  370. }
  371. var userId = utils_get_param("userId");
  372. function getUserId() {
  373. console.log("Original USERID : " + userId);
  374. var use = getCookie("doibyUser");
  375. if (use == null || use === undefined || use.length <= 0) {
  376. location.href = "sign-in1.html?from=entrance-1&msg=nologin-mustauth";
  377. return;
  378. }
  379. console.log("Got UID From COOKIE : " + use);
  380. try{
  381. var mm = parseInt(use);
  382. if(mm <= 0){
  383. }else{
  384. userId = mm;
  385. }
  386. }catch (e) {
  387. location.href = "sign-in1.html?from=entrance-1&msg=cookie-wrong-format";
  388. return;
  389. }
  390. console.log("Got UserId Eventually : " + userId);
  391. }
  392. $(document).ready(on_document_load);
  393. get_server_url();