basic.js 8.2 KB

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