basic.js 7.7 KB

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