service.js 3.3 KB

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