service.js 3.2 KB

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