basic.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. let Queue = {IS_LOADED : false, nowuse : 0, SVR_URL:"/server/", groupsCount : 0, groups:[], shouldStop : false, backup:["/server/","http://localhost:8803/server/","http://172.30.84.3:8803/server/","https://1.niimei.com/server/"] ,list:[]};
  2. function get_server_url() {
  3. let output = $.ajax({url: Queue.SVR_URL + "/fcWorker/list?userId=1", timeout:1000 }).fail(on_check_fail).done(function(){
  4. Queue.IS_LOADED = true;
  5. console.warn("Using SERVER : ");
  6. console.warn(Queue.SVR_URL);
  7. setTimeout(parse_queue, 50);
  8. });
  9. }
  10. function on_check_fail(){
  11. if(Queue.nowuse === Queue.backup.length){
  12. var out = "";
  13. Queue.backup.forEach(function(val,index,arr){
  14. out += val + "\n";
  15. });
  16. send_alert("抱歉, 服务器连接失败! \n 请确认 " + out + "其中之一可用");
  17. }else{
  18. Queue.SVR_URL = Queue.backup[Queue.nowuse];
  19. Queue.nowuse ++;
  20. $.ajax({url: Queue.SVR_URL + "/fcWorker/list?userId=1", timeout:1000 }).fail(on_check_fail).done(function(){
  21. Queue.IS_LOADED = true;
  22. console.warn("Using SERVER : ");
  23. console.warn(Queue.SVR_URL);
  24. setTimeout(parse_queue, 50);
  25. });
  26. }
  27. }
  28. function get_data(url, callback) {
  29. Queue.list.push({url:url, callback:callback, gid:-1});
  30. }
  31. function new_data_group(groupName, callback){
  32. var gid = -1;
  33. Queue.groups.forEach(function(val,index,arr){
  34. if(val.groupName === groupName){
  35. gid = val.gid;
  36. }
  37. });
  38. if(gid === -1) {
  39. gid = Queue.groupsCount;
  40. Queue.groupsCount++;
  41. Queue.groups.push({
  42. gid: gid, groupName:groupName, callme: callback, counter:0
  43. });
  44. }
  45. return gid;
  46. }
  47. function group_get_data(groupName, url, callback){
  48. var gid = -1;
  49. Queue.groups.forEach(function(val,index,arr){
  50. if(val.groupName === groupName){
  51. gid = val.gid;
  52. }
  53. });
  54. if(gid === -1) {
  55. send_alert("Group 方法使用错误!!!")
  56. }
  57. Queue.list.push({url:url, callback:callback, gid:gid});
  58. }
  59. function parse_queue(){
  60. if(Queue.list.length > 0){
  61. let one = Queue.list.pop();
  62. if(one.gid === -1) {
  63. request_data(one.url, one.callback);
  64. }else{
  65. g_request_data(one.url, one.callback, one.gid);
  66. }
  67. }
  68. if(Queue.shouldStop == false) {
  69. setTimeout(parse_queue, 100);
  70. }
  71. }
  72. function request_data(url, callback){
  73. $.ajax({
  74. url: Queue.SVR_URL + url,
  75. apiName: url,
  76. sendTime: new Date(),
  77. callme: callback
  78. }).done(on_dat_success).fail(on_fail);
  79. }
  80. function g_request_data(url, callback, gid){
  81. $.ajax({
  82. url: Queue.SVR_URL + url,
  83. apiName: url,
  84. sendTime: new Date(),
  85. gid: gid,
  86. callme: callback
  87. }).done(on_gp_success).fail(on_fail);
  88. }
  89. function on_gp_success(a,b) {
  90. var gid = this.gid;
  91. if(check_if_success(a,b)) {
  92. this.callme(a,b);
  93. Queue.groups.forEach(function(v,i,a){
  94. if(v.gid === gid){
  95. v.counter ++;
  96. v.callme(v);
  97. }
  98. });
  99. }
  100. }
  101. function on_dat_success(a,b){
  102. if(check_if_success(a,b)) {
  103. this.callme(a,b);
  104. }
  105. }
  106. function check_if_success(a,b){
  107. if(typeof a === "string"){
  108. try {
  109. a = JSON.parse(a);
  110. }catch (e) {
  111. console.warn("=---------- 异常 -----------=");
  112. console.warn(a);
  113. console.warn("=----------------------------=");
  114. send_alert("信息失败 : "+e);
  115. return;
  116. }
  117. }else if(typeof a === "undefined"){
  118. console.warn("=---------- 异常 -----------=");
  119. console.warn(a);
  120. console.warn("=------------------------------=");
  121. send_alert("信息失败 : "+e);
  122. return;
  123. }
  124. if(a.ret === "10000") {
  125. return true;
  126. }else{
  127. console.error(JSON.stringify(a));
  128. send_alert("查询失败 : 请参阅控制台输出! ");
  129. }
  130. return false;
  131. }
  132. function on_fail(msg) {
  133. send_alert("很抱歉,网络错误,请查看Console");
  134. console.warn("----- 网络请求失败 ---------");
  135. console.warn(JSON.stringify(this));
  136. console.warn("----------------------------");
  137. Queue.shouldStop = true;
  138. }
  139. function first_parse(ajax, obj) {
  140. if (typeof obj === "string") {
  141. obj = JSON.parse(obj);
  142. }
  143. if (typeof obj !== "object") {
  144. console.warn(ajax);
  145. throw "Error ! Parsing JSON failed ." + JSON.stringify(obj);
  146. }
  147. if (obj.ret === "10000") {
  148. return obj.model;
  149. } else {
  150. console.warn(ajax);
  151. throw "Error ! Server returned error." + JSON.stringify(obj);
  152. }
  153. }
  154. function on_document_load() {
  155. if(typeof (page_document_load) !== "undefined"){
  156. page_document_load();
  157. }
  158. if(typeof (resizeAll) !== "undefined"){
  159. resizeAll();
  160. }
  161. }
  162. function send_alert(a, b){
  163. 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>";
  164. //var model = document.createElement("div");
  165. if($("#modalContainer").length <= 0){
  166. alert(a);
  167. return ;
  168. }
  169. $("#modalContainer").html(q);
  170. var modalO = $("alertModal");
  171. if(modalO.length === 0){
  172. alert(a);
  173. }else{
  174. $("#alertModelLabel").text(b);
  175. $("#message-text").text(b);
  176. modalO.modal('show');
  177. }
  178. }
  179. $(document).ready(on_document_load);
  180. get_server_url();