service.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * 基本服务器交互类
  3. */
  4. var ServiceOperation = {
  5. // BaseUrl: "http://10.10.0.69:10022/FBS_MES/",
  6. BaseUrl: "http://127.0.0.1:8088/FBS_MES/",
  7. // BaseUrl: "http://103.40.192.17:8086/FBS_MES/",
  8. // BaseUrl: "http://106.15.206.14:8087/FBS_MES/",
  9. MethodPost: "POST", // post请求
  10. MethodPut: "PUT", // put请求
  11. MethodGet: "GET", // get请求
  12. MethodDelete: "DELETE", // delete请求
  13. /**
  14. * 获取完整的请求路径
  15. */
  16. getFullUrl: function(url) {
  17. return this.BaseUrl + url;
  18. },
  19. /**
  20. * 发送请求
  21. */
  22. send: function(method, url, params, isAsync, callback, isMask, jsonRequest) {
  23. jQuery.support.cors = true;
  24. var strParams = "";
  25. var contentType = "";
  26. if (jsonRequest) {
  27. // strParams = JSON.stringify(params);
  28. // if (strParams == "{}") strParams = "";
  29. strParams = params;
  30. contentType = "application/json;charset=utf-8";
  31. } else {
  32. strParams = params;
  33. }
  34. if (isMask)
  35. top.layer.msg(
  36. "正在执行,请稍候……",
  37. {
  38. icon: 16,
  39. shade: 0.5
  40. },
  41. function() {
  42. $.ajax({
  43. contentType: contentType,
  44. type: method,
  45. url: url,
  46. data: strParams,
  47. dataType: "json",
  48. async: isAsync,
  49. success: function(data) {
  50. if (callback != undefined)
  51. callback(data);
  52. },
  53. complete: function(XMLHttpRequest, textStatus) {},
  54. error: function(XMLHttpRequest, textStatus) {
  55. ServiceOperation.onload(XMLHttpRequest);
  56. },
  57. beforeSend: function(XMLHttpRequest) {},
  58. headers: {
  59. "X-Access-Token": SessionOperation.getToken()
  60. }
  61. });
  62. }
  63. );
  64. else
  65. $.ajax({
  66. contentType: contentType,
  67. type: method,
  68. url: url,
  69. data: strParams,
  70. dataType: "json",
  71. async: isAsync,
  72. success: function(data) {
  73. if (callback != undefined)
  74. callback(data);
  75. },
  76. complete: function(XMLHttpRequest, textStatus) {},
  77. error: function(XMLHttpRequest, textStatus) {
  78. console.log(XMLHttpRequest);
  79. ServiceOperation.onload(XMLHttpRequest);
  80. },
  81. beforeSend: function(XMLHttpRequest) {},
  82. headers: {
  83. "X-Access-Token": SessionOperation.getToken()
  84. }
  85. });
  86. },
  87. /**
  88. * 对返回错误的结果进行预处理
  89. */
  90. onload: function(resp) {
  91. var status = resp.status;
  92. switch (status) {
  93. case 200:
  94. return resp;
  95. case 401:
  96. console.warn("未登录或登录已过期,请重新登录!");
  97. window.top.location.replace("/login.html");
  98. break;
  99. case 403:
  100. case 404:
  101. case 500:
  102. console.log(resp.responseJSON.message);
  103. if (resp.responseJSON.message === "Token失效,请重新登录") {
  104. console.warn("未登录或登录已过期,请重新登录。");
  105. // 清空 token 信息
  106. window.sessionStorage.clear();
  107. window.top.location.replace("/login.html");
  108. } else {
  109. // window.top.location.replace("/exception/" + status + ".html");
  110. }
  111. break;
  112. default:
  113. console.warn(
  114. "未可知错误,大部分是由于后端不支持CORS或无效配置引起",
  115. resp
  116. );
  117. return resp;
  118. }
  119. }
  120. };