123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- import Vue from "vue";
- import axios from "axios";
- import router from "../router";
- import { loadProgressBar } from "axios-progress-bar";
- import networkInformationHint from "./networkInformationHint.js";
- const ERROR_MSG_CONFIG = require("./errorMsgConfig").default;
- // Full config: https://github.com/axios/axios#request-config
- // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
- // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
- // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
- let config = {
- // baseURL: process.env.baseURL || process.env.apiUrl || ""
- timeout: 60 * 1000, // Timeout
- withCredentials: true, // Check cross-site Access-Control
- };
- const _$httpWith500Msg = axios.create(config);
- const _$http = axios.create(config); // no auto 500 error UI
- const _$httpWithoutBar = axios.create(config);
- const noauthUrls = ["/auth/login", "/auth/version", "/auth/thirdPartyAccess"];
- /**
- * A. token lifecycle
- * 1. /login UI => localStorage.removeItem('token') && localStorage.setItem('token')
- * 2. non /login UI => axios if(!wk_token) wk_token = window.sessionStorage.getItem("token"), send request
- * 3. if axios request fail with 401/403, wk_token = null, redirect to /login removeItem('token')
- * 4. logout to /login, before send request, invalidate wk_token
- * */
- let wk_token, wk_key;
- let wk_orgId;
- function getRootOrgId() {
- if (location.hostname.includes("qmth.com.cn")) {
- return "";
- } else {
- return "?orgId=" + (wk_orgId === undefined ? "" : wk_orgId);
- }
- }
- function returnLogin() {
- const returnUrl = window.sessionStorage.getItem("returnUrl");
- if (returnUrl) {
- window.location.href = returnUrl;
- return;
- }
- router.push("/login/" + getRootOrgId());
- }
- _$httpWith500Msg.interceptors.request.use(
- function (config) {
- networkInformationHint();
- // Do something before request is sent
- if (!noauthUrls.some((url) => config.url.includes(url))) {
- if (!wk_token) {
- const user = JSON.parse(window.sessionStorage.getItem("user"));
- if (!user) {
- if (
- window.___lastInvalidDate === undefined ||
- window.___lastInvalidDate < Date.now() - 300
- ) {
- Vue.prototype.$alert("登录失效,请重新登录!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- // if (process.env.NODE_ENV !== "production") {
- // router.push("/login/" + "?orgId=" + wk_orgId);
- // } else {
- // router.push("/login");
- // }
- returnLogin();
- },
- });
- window.___lastInvalidDate = Date.now();
- }
- return;
- }
- wk_token = user.token;
- wk_key = user.key;
- wk_orgId = user.rootOrgId;
- }
- if (wk_token && config.headers.common["token"] == null) {
- config.headers.common["token"] = wk_token;
- config.headers.common["key"] = wk_key;
- }
- } else {
- wk_token = null;
- }
- return config;
- },
- function (error) {
- // Do something with request error
- Vue.prototype.$notify({
- showClose: true,
- message: error,
- type: "error",
- });
- return Promise.reject(error);
- }
- );
- _$http.interceptors.request.use(
- // no auto 500 error UI
- function (config) {
- networkInformationHint();
- // Do something before request is sent
- if (!noauthUrls.some((url) => config.url.includes(url))) {
- if (!wk_token) {
- const user = JSON.parse(window.sessionStorage.getItem("user"));
- if (!user) {
- if (
- window.___lastInvalidDate === undefined ||
- window.___lastInvalidDate < Date.now() - 300
- ) {
- Vue.prototype.$alert("登录失效,请重新登录!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- returnLogin();
- },
- });
- window.___lastInvalidDate = Date.now();
- }
- return;
- }
- wk_token = user.token;
- wk_key = user.key;
- wk_orgId = user.rootOrgId;
- }
- if (wk_token && config.headers.common["token"] == null) {
- config.headers.common["token"] = wk_token;
- config.headers.common["key"] = wk_key;
- }
- } else {
- wk_token = null;
- }
- return config;
- },
- function (error) {
- // Do something with request error
- Vue.prototype.$notify({
- showClose: true,
- message: error,
- type: "error",
- });
- return Promise.reject(error);
- }
- );
- const recordRequest = () => {
- let matchedRoutePath;
- try {
- const matched = router.resolve(location).route.matched;
- const exactMatched = matched[matched.length - 1];
- matchedRoutePath = exactMatched.path;
- } catch (error) {
- console.log(error);
- window._hmt.push([
- "_trackEvent",
- `页面-${location.pathname}`,
- "网络请求-响应",
- "解析出错",
- ]);
- }
- window._hmt.push([
- "_trackEvent",
- `页面-${matchedRoutePath || location.pathname}`,
- "网络请求-响应",
- ]);
- };
- // Add a response interceptor
- _$httpWith500Msg.interceptors.response.use(
- (response) => {
- recordRequest(response);
- return response;
- },
- (error) => {
- console.dir(error);
- if (!error.response) {
- // "Network Error" 网络不通,直接返回
- Vue.prototype.$notify({
- showClose: true,
- message: "网络连接异常,请检查网络设置。",
- type: "error",
- });
- return Promise.reject(error);
- }
- if (error.config.noToast) {
- return Promise.reject(error);
- }
- // 这里是返回状态码不为200时候的错误处理
- let status = error.response.status;
- // 登录失效 跳转登录页面
- if (status == 403 || status == 401) {
- if (
- window.___lastInvalidDate === undefined ||
- window.___lastInvalidDate < Date.now() - 300
- ) {
- Vue.prototype.$alert("登录失效,请重新登录!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- returnLogin();
- },
- });
- window.___lastInvalidDate = Date.now();
- }
- return Promise.reject(error);
- } else if (status == 405) {
- Vue.prototype.$alert("没有权限!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- returnLogin();
- },
- });
- return Promise.reject(error);
- } else if (status == 502) {
- Vue.prototype.$alert("服务器异常!", "提示", {
- confirmButtonText: "确定",
- });
- return Promise.reject(error);
- }
- // 下载数据类型为blob时,错误提示直接抛给通用下载逻辑
- if (error.response.config.responseType === "blob") {
- return Promise.reject(error);
- }
- if (status != 200) {
- const data = error.response.data;
- if (ERROR_MSG_CONFIG.map((v) => v.code).includes(data.code)) {
- const MSG = ERROR_MSG_CONFIG.find((v) => v.code === data.code);
- if (MSG.display) {
- Vue.prototype.$notify({
- showClose: true,
- message: MSG.message,
- type: "error",
- });
- }
- } else {
- if (data && data.desc) {
- Vue.prototype.$notify({
- showClose: true,
- message: data.desc,
- type: "error",
- });
- } else {
- Vue.prototype.$notify({
- showClose: true,
- message: "未定义异常: " + JSON.stringify(data, 2),
- type: "error",
- });
- }
- }
- return Promise.reject(error);
- }
- }
- );
- // Add a response interceptor
- _$http.interceptors.response.use(
- // no auto 500 error UI
- (response) => {
- recordRequest(response);
- return response;
- },
- (error) => {
- if (!error.response) {
- Vue.prototype.$notify({
- showClose: true,
- message: "网络连接异常,请检查网络设置。",
- type: "error",
- });
- return Promise.reject(error);
- }
- if (error.config.noToast) {
- return Promise.reject(error);
- }
- // 这里是返回状态码不为200时候的错误处理
- let status = error.response.status;
- // 登录失效 跳转登录页面
- if (status == 403 || status == 401) {
- if (
- window.___lastInvalidDate === undefined ||
- window.___lastInvalidDate < Date.now() - 300
- ) {
- Vue.prototype.$alert("登录失效,请重新登录!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- returnLogin();
- },
- });
- window.___lastInvalidDate = Date.now();
- }
- return Promise.reject(error);
- } else if (status == 405) {
- Vue.prototype.$alert("没有权限!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- returnLogin();
- },
- });
- return Promise.reject(error);
- } else if (status == 502) {
- Vue.prototype.$alert("服务器异常!", "提示", {
- confirmButtonText: "确定",
- });
- return Promise.reject(error);
- } else if (status == 503) {
- Vue.prototype.$alert("服务器繁忙,请稍后重试!", "提示", {
- confirmButtonText: "确定",
- });
- return Promise.reject(error);
- }
- if (status != 200) {
- return Promise.reject(error);
- }
- }
- );
- _$httpWithoutBar.interceptors.request.use(
- // no auto 500 error UI
- function (config) {
- networkInformationHint();
- // Do something before request is sent
- if (!noauthUrls.some((url) => config.url.includes(url))) {
- if (!wk_token) {
- const user = JSON.parse(window.sessionStorage.getItem("user"));
- if (!user) {
- if (
- window.___lastInvalidDate === undefined ||
- window.___lastInvalidDate < Date.now() - 300
- ) {
- Vue.prototype.$alert("登录失效,请重新登录!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- returnLogin();
- },
- });
- window.___lastInvalidDate = Date.now();
- }
- return;
- }
- wk_token = user.token;
- wk_key = user.key;
- wk_orgId = user.rootOrgId;
- }
- if (wk_token && config.headers.common["token"] == null) {
- config.headers.common["token"] = wk_token;
- config.headers.common["key"] = wk_key;
- }
- } else {
- wk_token = null;
- }
- return config;
- },
- function (error) {
- // Do something with request error
- Vue.prototype.$notify({
- showClose: true,
- message: error,
- type: "error",
- });
- return Promise.reject(error);
- }
- );
- _$httpWithoutBar.interceptors.response.use(
- // no auto 500 error UI
- (response) => {
- recordRequest(response);
- return response;
- },
- (error) => {
- if (!error.response) {
- Vue.prototype.$notify({
- showClose: true,
- message: "网络连接异常,请检查网络设置。",
- type: "error",
- });
- return Promise.reject(error);
- }
- if (error.config.noToast) {
- return Promise.reject(error);
- }
- // 这里是返回状态码不为200时候的错误处理
- let status = error.response.status;
- // 登录失效 跳转登录页面
- if (status == 403 || status == 401) {
- if (
- window.___lastInvalidDate === undefined ||
- window.___lastInvalidDate < Date.now() - 300
- ) {
- window.___lastInvalidDate = Date.now();
- }
- return Promise.reject(error);
- } else if (status == 405) {
- Vue.prototype.$alert("没有权限!", "提示", {
- confirmButtonText: "确定",
- callback: () => {
- returnLogin();
- },
- });
- return Promise.reject(error);
- } else if (status == 502) {
- Vue.prototype.$alert("服务器异常!", "提示", {
- confirmButtonText: "确定",
- });
- return Promise.reject(error);
- }
- if (status != 200) {
- return Promise.reject(error);
- }
- }
- );
- Plugin.install = function (Vue) {
- Vue.$http = _$http; // no auto 500 error UI
- Object.defineProperties(Vue.prototype, {
- $http: {
- get() {
- return _$http; // no auto 500 error UI
- },
- },
- });
- Vue.$httpWithMsg = _$httpWith500Msg;
- Object.defineProperties(Vue.prototype, {
- $httpWithMsg: {
- get() {
- return _$httpWith500Msg;
- },
- },
- });
- // for below request
- // config.url.includes("/api/ecs_ques/paper/") === false &&
- // config.url.includes("/api/ecs_ques/questionAudio") === false
- const _a = axios.create(config);
- Vue.$httpWithoutAuth = _a;
- Object.defineProperties(Vue.prototype, {
- $httpWithoutAuth: {
- get() {
- return _a;
- },
- },
- });
- Vue.$httpWithoutBar = _$httpWithoutBar;
- Object.defineProperties(Vue.prototype, {
- $httpWithoutBar: {
- get() {
- return _$httpWithoutBar;
- },
- },
- });
- };
- Vue.use(Plugin);
- loadProgressBar({}, Vue.$http);
- loadProgressBar({}, Vue.$httpWithMsg);
- loadProgressBar({}, Vue.$httpWithoutAuth);
- // const update = (type, e) => {
- // // debugger;
- // console.log(type);
- // console.log(
- // "e.target.url: ",
- // e.target.responseURL,
- // " timeStamp: ",
- // e.timeStamp.toFixed(2),
- // " loaded:",
- // e.loaded,
- // " total: ",
- // e.total
- // );
- // console.log(e);
- // };
- // Vue.$httpWithMsg.defaults.onDownloadProgress = e => {
- // update("下载", e);
- // };
- // Vue.$httpWithMsg.defaults.onUploadProgress = e => {
- // update("上传", e);
- // };
- import "axios-progress-bar/dist/nprogress.css";
- export default Plugin;
- export const $httpWithMsg = _$httpWith500Msg;
|