basic.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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:[]};
  16. let Users = {list : []};
  17. let Sectors = {list : []};
  18. function get_server_url() {
  19. let output = $.ajax({url: Queue.SVR_URL + "/fcWorker/list?userId=1", 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 + "/fcWorker/list?userId=1", 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.list.length > 15){
  80. for(i = 0; i < Queue.list.length; i++) {
  81. Queue.list.pop();
  82. }
  83. }
  84. if(Queue.list.length > 0){
  85. let one = Queue.list.pop();
  86. if(one.gid === -1) {
  87. request_data(one.url, one.callback);
  88. }else{
  89. g_request_data(one.url, one.callback, one.gid);
  90. }
  91. }
  92. if(Queue.shouldStop == false) {
  93. setTimeout(parse_queue, 50);
  94. }
  95. }
  96. function request_data(url, callback){
  97. $.ajax({
  98. url: Queue.SVR_URL + url,
  99. apiName: url,
  100. sendTime: new Date(),
  101. callme: callback
  102. }).done(on_dat_success).fail(on_fail);
  103. }
  104. function g_request_data(url, callback, gid){
  105. $.ajax({
  106. url: Queue.SVR_URL + url,
  107. apiName: url,
  108. sendTime: new Date(),
  109. gid: gid,
  110. callme: callback
  111. }).done(on_gp_success).fail(on_fail);
  112. }
  113. function on_gp_success(a,b) {
  114. var gid = this.gid;
  115. if(check_if_success(a,b)) {
  116. this.callme(a,b);
  117. Queue.groups.forEach(function(v,i,a){
  118. if(v.gid === gid){
  119. v.counter ++;
  120. v.callme(v);
  121. }
  122. });
  123. }
  124. }
  125. function on_dat_success(a,b){
  126. if(check_if_success(a,b)) {
  127. this.callme(a,b);
  128. }
  129. }
  130. function check_if_success(a,b){
  131. if(typeof a === "string"){
  132. try {
  133. a = JSON.parse(a);
  134. }catch (e) {
  135. console.warn("=---------- 异常 -----------=");
  136. console.warn(a);
  137. console.warn("=----------------------------=");
  138. send_alert("信息失败 : "+e);
  139. return;
  140. }
  141. }else if(typeof a === "undefined"){
  142. console.warn("=---------- 异常 -----------=");
  143. console.warn(a);
  144. console.warn("=------------------------------=");
  145. send_alert("信息失败 : "+e);
  146. return;
  147. }
  148. if(a.ret === "10000") {
  149. return true;
  150. }else{
  151. console.trace();
  152. console.error(b);
  153. console.error(JSON.stringify(a));
  154. send_alert("查询失败 : 请参阅控制台输出! ");
  155. }
  156. return false;
  157. }
  158. function on_fail(msg) {
  159. send_alert("很抱歉,网络错误,请查看Console");
  160. console.warn("----- 网络请求失败 ---------");
  161. console.warn(JSON.stringify(this));
  162. console.warn("----------------------------");
  163. Queue.shouldStop = true;
  164. }
  165. function first_parse(ajax, obj) {
  166. if (typeof obj === "string") {
  167. obj = JSON.parse(obj);
  168. }
  169. if (typeof obj !== "object") {
  170. console.warn(ajax);
  171. throw "Error ! Parsing JSON failed ." + JSON.stringify(obj);
  172. }
  173. if (obj.ret === "10000") {
  174. return obj.model;
  175. } else {
  176. console.warn(ajax);
  177. throw "Error ! Server returned error." + JSON.stringify(obj);
  178. }
  179. }
  180. function on_document_load() {
  181. if(typeof (page_document_load) !== "undefined"){
  182. page_document_load();
  183. }
  184. if(typeof (resizeAll) !== "undefined"){
  185. resizeAll();
  186. }
  187. }
  188. function send_alert(a, b){
  189. if(Queue.shouldStop) return;
  190. $("#mpAlert").html(a);
  191. $("#mpAlert").show();
  192. return;
  193. 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>";
  194. //var model = document.createElement("div");
  195. if($("#modalContainer").length <= 0){
  196. alert(a);
  197. return ;
  198. }
  199. $("#modalContainer").html(q);
  200. var modalO = $("alertModal");
  201. if(modalO.length === 0){
  202. alert(a);
  203. }else{
  204. $("#alertModelLabel").text(b);
  205. $("#message-text").text(b);
  206. modalO.modal('show');
  207. }
  208. }
  209. function getUserName(uid){
  210. var uname = null;
  211. Users.list.forEach(function(val, aid, arr){
  212. if(val.worker.userId == uid){
  213. uname = val.worker.userName;
  214. }
  215. });
  216. if(uname == null) {
  217. get_data("fcWorker/list?userId=" + uid, on_username_retn);
  218. return "[用户:"+uid+"]";
  219. }else{
  220. return uname;
  221. }
  222. }
  223. function on_username_retn(obj, sta) {
  224. var uid = this.url.substring(this.url.indexOf("userId=") + 7);
  225. let ruid = parseInt(uid);
  226. var out = first_parse(this, obj);
  227. if(out.list.length > 0){
  228. Users.list.push({
  229. uid : ruid, worker: out.list[0]
  230. });
  231. }
  232. }
  233. function getSectorName(sectorId){
  234. var secName = null;
  235. Sectors.list.forEach(function(val, aid, arr){
  236. if(val.sector.sectorId === sectorId){
  237. secName = val.sector.sectorName;
  238. }
  239. });
  240. if(secName == null) {
  241. get_data("fcSector/list?sectorId=" + sectorId, on_secname_retn);
  242. return "[工位:"+sectorId+"]";
  243. }else{
  244. return secName;
  245. }
  246. }
  247. function on_secname_retn(obj, sta) {
  248. var secid = this.url.substring(this.url.indexOf("sectorId=") + 9);
  249. let rsecid = parseInt(secid);
  250. var out = first_parse(this, obj);
  251. if(out.list.length > 0){
  252. Sectors.list.push({
  253. secid : rsecid, sector: out.list[0]
  254. });
  255. }
  256. }
  257. function getSectorId(spotId){
  258. var secId = null;
  259. Sectors.list.forEach(function(val, aid, arr){
  260. if(val.sector.spotId === spotId){
  261. secId = val.sector.sectorId;
  262. }
  263. });
  264. if(secId == null) {
  265. get_data("fcSectorSpot/list?spotId=" + spotId, on_secid_retn);
  266. return -1;
  267. }else{
  268. return secId;
  269. }
  270. }
  271. function on_secid_retn(obj, sta) {
  272. var spotId = this.url.substring(this.url.indexOf("spotId=") + 7);
  273. let rsecid = parseInt(spotId);
  274. var out = first_parse(this, obj);
  275. if(out.list.length > 0){
  276. Sectors.list.push({
  277. secid : rsecid, sector: out.list[0]
  278. });
  279. }
  280. }
  281. /**
  282. * Calc b/a+b * 100(precision = 2), when b is zero, return zero
  283. * @param a (pass)
  284. * @param b (fail)
  285. */
  286. function get_factor(a,b){
  287. if(a+b === 0){
  288. return 0;
  289. }else{
  290. return Math.floor(10000* b / (a+b) ) / 100;
  291. }
  292. }
  293. $(document).ready(on_document_load);
  294. get_server_url();