basic.js 13 KB

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