axios.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import Vue from "vue";
  2. import axios from "axios";
  3. import { loadProgressBar } from "./axiosProgress";
  4. import cachingGet from "./axiosCache";
  5. import { Message } from "iview";
  6. import router from "../router";
  7. //axios配置 start
  8. const qmInstance = axios.create({});
  9. //请求拦截
  10. /**
  11. * A. token lifecycle
  12. * 1. /login UI => localStorage.removeItem('token') && localStorage.setItem('token')
  13. * 2. non /login UI => axios if(!wk_token) wk_token = window.sessionStorage.getItem("token"), send request
  14. * 3. if axios request fail with 401/403, wk_token = null, redirect to /login removeItem('token')
  15. * 4. logout to /login, before send request, invalidate wk_token
  16. * */
  17. let wk_token, wk_key;
  18. qmInstance.interceptors.request.use(
  19. config => {
  20. if (config.url.includes("/login") === false) {
  21. if (!wk_token) {
  22. wk_token = window.sessionStorage.getItem("token");
  23. wk_key = window.localStorage.getItem("key");
  24. }
  25. if (wk_token && config.headers.common["token"] == null) {
  26. config.headers.common["token"] = wk_token;
  27. // Axios.defaults.headers.common["key"] = window.localStorage.getItem("key");
  28. config.headers.common["key"] = wk_key;
  29. }
  30. } else {
  31. wk_token = null;
  32. }
  33. return config;
  34. },
  35. error => {
  36. Message.error({
  37. content: error,
  38. duration: 10,
  39. closable: true
  40. });
  41. return Promise.resolve(error);
  42. }
  43. );
  44. //响应拦截
  45. qmInstance.interceptors.response.use(
  46. response => {
  47. return response;
  48. },
  49. error => {
  50. if (!error.response) {
  51. // "Network Error" 网络不通,直接返回
  52. Message.error({
  53. content: "网络连接异常,请检查网络设置。",
  54. duration: 6,
  55. closable: true
  56. });
  57. return Promise.reject(error);
  58. }
  59. // 这里是返回状态码不为200时候的错误处理
  60. let status = error.response.status;
  61. if (status != 200) {
  62. const data = error.response.data;
  63. if (data && data.desc) {
  64. Message.error({
  65. content: data.desc,
  66. duration: 6,
  67. closable: true
  68. });
  69. } else {
  70. Message.error({
  71. content: "未定义异常: " + JSON.stringify(data, 2),
  72. duration: 6,
  73. closable: true
  74. });
  75. }
  76. }
  77. // 登录失效 跳转登录页面
  78. if (status == 403 || status == 401) {
  79. wk_token = null;
  80. router.push("/login/" + localStorage.getItem("domain"));
  81. }
  82. return Promise.reject(error);
  83. }
  84. );
  85. qmInstance.defaults.withCredentials = true; //允许跨域携带cookie
  86. qmInstance.defaults.timeout = 10000; //超时时间
  87. qmInstance.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"; //标识这是一个 ajax 请求
  88. qmInstance.get = cachingGet(qmInstance.get, [
  89. /\/api\/exam_question\/question\/\?question_id/,
  90. /\/api\/exam_question\/paper_struct\/\?exam_record_id=/,
  91. /\/api\/ecs_exam_work\/exam\/\d+$/,
  92. /\/api\/ecs_oe_student_face\/upyun$/
  93. ]);
  94. loadProgressBar(qmInstance);
  95. const upyunInstance = axios.create({});
  96. // FIXME: axios bug. wait 0.19 release. https://github.com/axios/axios/issues/385
  97. upyunInstance.defaults.headers.common = {};
  98. // upyunInstance.defaults.headers.common["Authorization"] = UPYUN_HEADER_AUTH;
  99. upyunInstance.interceptors.request.use(
  100. config => {
  101. return qmInstance
  102. .get("/api/ecs_oe_student_face/upyun")
  103. .then(res => {
  104. config.baseURL = res.data.bucketUrl;
  105. const authorization =
  106. "Basic " +
  107. btoa(atob(res.data.upyunOperator) + ":" + atob(res.data.upyunCred));
  108. config.headers.common["Authorization"] = authorization;
  109. return config;
  110. })
  111. .catch(err => {
  112. console.log(err);
  113. });
  114. },
  115. error => {
  116. Message.error({
  117. content: error,
  118. duration: 10,
  119. closable: true
  120. });
  121. return Promise.resolve(error);
  122. }
  123. );
  124. loadProgressBar(upyunInstance);
  125. Vue.prototype.$http = qmInstance;
  126. Vue.prototype.$upyunhttp = upyunInstance;
  127. export default {
  128. install: function(Vue) {
  129. Object.defineProperty(Vue.prototype, "$http", {
  130. value: qmInstance
  131. });
  132. Object.defineProperty(Vue.prototype, "$upyunhttp", {
  133. value: upyunInstance
  134. });
  135. }
  136. };