service.js 3.2 KB

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