basic.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 = {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. if(Queue.list.length > 0){
  78. let one = Queue.list.pop();
  79. if(one.gid === -1) {
  80. request_data(one.url, one.callback);
  81. }else{
  82. g_request_data(one.url, one.callback, one.gid);
  83. }
  84. }
  85. if(Queue.shouldStop == false) {
  86. setTimeout(parse_queue, 50);
  87. }
  88. }
  89. function request_data(url, callback){
  90. $.ajax({
  91. url: Queue.SVR_URL + url,
  92. apiName: url,
  93. sendTime: new Date(),
  94. callme: callback
  95. }).done(on_dat_success).fail(on_fail);
  96. }
  97. function g_request_data(url, callback, gid){
  98. $.ajax({
  99. url: Queue.SVR_URL + url,
  100. apiName: url,
  101. sendTime: new Date(),
  102. gid: gid,
  103. callme: callback
  104. }).done(on_gp_success).fail(on_fail);
  105. }
  106. function on_gp_success(a,b) {
  107. var gid = this.gid;
  108. if(check_if_success(a,b)) {
  109. this.callme(a,b);
  110. Queue.groups.forEach(function(v,i,a){
  111. if(v.gid === gid){
  112. v.counter ++;
  113. v.callme(v);
  114. }
  115. });
  116. }
  117. }
  118. function on_dat_success(a,b){
  119. if(check_if_success(a,b)) {
  120. this.callme(a,b);
  121. }
  122. }
  123. function check_if_success(a,b){
  124. if(typeof a === "string"){
  125. try {
  126. a = JSON.parse(a);
  127. }catch (e) {
  128. console.warn("=---------- 异常 -----------=");
  129. console.warn(a);
  130. console.warn("=----------------------------=");
  131. send_alert("信息失败 : "+e);
  132. return;
  133. }
  134. }else if(typeof a === "undefined"){
  135. console.warn("=---------- 异常 -----------=");
  136. console.warn(a);
  137. console.warn("=------------------------------=");
  138. send_alert("信息失败 : "+e);
  139. return;
  140. }
  141. if(a.ret === "10000") {
  142. return true;
  143. }else{
  144. console.trace();
  145. console.error(b);
  146. console.error(JSON.stringify(a));
  147. send_alert("查询失败 : 请参阅控制台输出! ");
  148. }
  149. return false;
  150. }
  151. function on_fail(msg) {
  152. send_alert("很抱歉,网络错误,请查看Console");
  153. console.warn("----- 网络请求失败 ---------");
  154. console.warn(JSON.stringify(this));
  155. console.warn("----------------------------");
  156. Queue.shouldStop = true;
  157. }
  158. function first_parse(ajax, obj) {
  159. if (typeof obj === "string") {
  160. obj = JSON.parse(obj);
  161. }
  162. if (typeof obj !== "object") {
  163. console.warn(ajax);
  164. throw "Error ! Parsing JSON failed ." + JSON.stringify(obj);
  165. }
  166. if (obj.ret === "10000") {
  167. return obj.model;
  168. } else {
  169. console.warn(ajax);
  170. throw "Error ! Server returned error." + JSON.stringify(obj);
  171. }
  172. }
  173. function on_document_load() {
  174. if(typeof (page_document_load) !== "undefined"){
  175. page_document_load();
  176. }
  177. if(typeof (resizeAll) !== "undefined"){
  178. resizeAll();
  179. }
  180. }
  181. function send_alert(a, b){
  182. if(Queue.shouldStop) return;
  183. $("#mpAlert").html(a);
  184. $("#mpAlert").show();
  185. return;
  186. 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>";
  187. //var model = document.createElement("div");
  188. if($("#modalContainer").length <= 0){
  189. alert(a);
  190. return ;
  191. }
  192. $("#modalContainer").html(q);
  193. var modalO = $("alertModal");
  194. if(modalO.length === 0){
  195. alert(a);
  196. }else{
  197. $("#alertModelLabel").text(b);
  198. $("#message-text").text(b);
  199. modalO.modal('show');
  200. }
  201. }
  202. function getUserName(uid){
  203. var uname = null;
  204. Users.list.forEach(function(val, aid, arr){
  205. if(val.worker.userId == uid){
  206. uname = val.worker.userName;
  207. }
  208. });
  209. if(uname == null) {
  210. get_data("fcWorker/list?userId=" + uid, on_username_retn);
  211. return "[用户:"+uid+"]";
  212. }else{
  213. return uname;
  214. }
  215. }
  216. function on_username_retn(obj, sta) {
  217. var uid = this.url.substring(this.url.indexOf("userId=") + 7);
  218. let ruid = parseInt(uid);
  219. var out = first_parse(this, obj);
  220. if(out.list.length > 0){
  221. Users.list.push({
  222. uid : ruid, worker: out.list[0]
  223. });
  224. }
  225. }
  226. $(document).ready(on_document_load);
  227. get_server_url();