basic.js 13 KB

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