axiosApp.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import Vue from "vue";
  2. // import Store from "@/store";
  3. import axios from "axios";
  4. import { loadProgressBar } from "axios-progress-bar";
  5. import cachingGet from "./axiosCache";
  6. import { notifyInvalidTokenThrottled } from "./axiosNotice";
  7. import { getToken, removeToken, getSessionId } from "../auth/auth";
  8. import axiosRetry from "axios-retry";
  9. import { PLATFORM, DEVICE_ID } from "@/constant/constants";
  10. import { Notification } from "element-ui";
  11. import CryptoJS from "crypto-js";
  12. // console.log(btoa(CryptoJS.SHA1("pWWQ0qyaXL8QHni4ig9YiWYTKr8UVQd4")));
  13. // console.log(
  14. // CryptoJS.enc.Base64.stringify(
  15. // CryptoJS.SHA1("pWWQ0qyaXL8QHni4ig9YiWYTKr8UVQd4")
  16. // )
  17. // );
  18. //QpVbMSbQrMVCxQEKqks8+E34+W8=
  19. // Full config: https://github.com/axios/axios#request-config
  20. // axios.defaults.baseURL = process.env.BASE_URL || process.env.apiUrl || '';
  21. // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
  22. const config = {
  23. // baseURL: process.env.baseURL || process.env.apiUrl || ""
  24. timeout: 60 * 1000, // Timeout
  25. withCredentials: true, // Check cross-site Access-Control
  26. };
  27. const cacheGetUrls = [];
  28. const _axiosApp = axios.create(config);
  29. axiosRetry(_axiosApp);
  30. function gToken(method, url, token, now) {
  31. // const now = Date.now();
  32. // console.log(`${getSessionId()} ${method}&${url}&${now}&${token}`);
  33. // const a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  34. // let randomStr = "";
  35. // for (let i = 0; i < 6; i++) {
  36. // const idx = Math.round(Math.random() * 100) % a.length;
  37. // randomStr += a[idx];
  38. // }
  39. const Authorization = `Token ${getSessionId()}:${CryptoJS.enc.Base64.stringify(
  40. CryptoJS.SHA1("post&" + url + "&" + now + "&" + token)
  41. )}`;
  42. return Authorization;
  43. }
  44. _axiosApp.interceptors.request.use(
  45. function (config) {
  46. const wk_token = getToken();
  47. const now = Date.now();
  48. if (wk_token) {
  49. const completeURL = new URL("http://nasty.com" + config.url);
  50. const path = completeURL.pathname;
  51. config.headers.common["Authorization"] = gToken(
  52. config.method,
  53. path,
  54. wk_token,
  55. now
  56. );
  57. // config.headers.common["Authorization"] = wk_token;
  58. }
  59. config.headers.common["platform"] = PLATFORM;
  60. config.headers.common["deviceId"] = DEVICE_ID;
  61. config.headers.common["time"] = now;
  62. return config;
  63. },
  64. function (error) {
  65. // Do something with request error
  66. Vue.prototype.$notify({
  67. showClose: true,
  68. message: error,
  69. type: "error",
  70. });
  71. return Promise.reject(error);
  72. }
  73. );
  74. // Add a response interceptor
  75. _axiosApp.interceptors.response.use(
  76. (response) => {
  77. return response;
  78. },
  79. (error) => {
  80. const showErrorMessage = !error.config?.noErrorMessage;
  81. if (!error.response) {
  82. if (showErrorMessage) {
  83. // "Network Error" 网络不通,直接返回
  84. Notification({
  85. showClose: true,
  86. message: "网络连接异常,请检查网络设置。",
  87. type: "error",
  88. });
  89. }
  90. return Promise.reject(error);
  91. }
  92. // 这里是返回状态码不为200时候的错误处理
  93. let status = error.response.status;
  94. // 登录失效 跳转登录页面
  95. if (status == 403 || status == 401) {
  96. notifyInvalidTokenThrottled();
  97. removeToken();
  98. return Promise.reject(error);
  99. } else if (status == 405) {
  100. if (showErrorMessage) {
  101. Notification({
  102. showClose: true,
  103. message: "没有权限!",
  104. type: "error",
  105. });
  106. }
  107. return Promise.reject(error);
  108. } else if (status == 502) {
  109. if (showErrorMessage) {
  110. Notification({
  111. showClose: true,
  112. message: "服务器异常(502)!",
  113. type: "error",
  114. });
  115. }
  116. return Promise.reject(error);
  117. }
  118. if (status != 200) {
  119. const data = error.response.data;
  120. if (data && data.message) {
  121. if (showErrorMessage) {
  122. Notification({
  123. showClose: true,
  124. message: data.message,
  125. type: "error",
  126. });
  127. }
  128. } else {
  129. if (showErrorMessage) {
  130. Notification({
  131. showClose: true,
  132. message: "未定义异常: " + JSON.stringify(data, 2),
  133. type: "error",
  134. });
  135. }
  136. }
  137. return Promise.reject(error);
  138. }
  139. }
  140. );
  141. _axiosApp.get = cachingGet(_axiosApp.get, cacheGetUrls);
  142. loadProgressBar(_axiosApp);
  143. Plugin.install = function (Vue) {
  144. Object.defineProperties(Vue.prototype, {
  145. $http: {
  146. get() {
  147. return _axiosApp;
  148. },
  149. },
  150. });
  151. };
  152. Vue.use(Plugin);
  153. export default Plugin;
  154. export const httpApp = _axiosApp;