basic.js 7.5 KB

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