123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import Vue from "vue";
- import axios from "axios";
- import { loadProgressBar } from "./axiosProgress";
- import cachingGet from "./axiosCache";
- import { Message } from "iview";
- import router from "../router";
- //axios配置 start
- const qmInstance = axios.create({});
- //请求拦截
- /**
- * 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;
- qmInstance.interceptors.request.use(
- config => {
- if (config.url.includes("/login") === false) {
- if (!wk_token) {
- wk_token = window.sessionStorage.getItem("token");
- wk_key = window.localStorage.getItem("key");
- }
- if (wk_token && config.headers.common["token"] == null) {
- config.headers.common["token"] = wk_token;
- // Axios.defaults.headers.common["key"] = window.localStorage.getItem("key");
- config.headers.common["key"] = wk_key;
- }
- } else {
- wk_token = null;
- }
- return config;
- },
- error => {
- Message.error({
- content: error,
- duration: 10,
- closable: true
- });
- return Promise.resolve(error);
- }
- );
- //响应拦截
- qmInstance.interceptors.response.use(
- response => {
- return response;
- },
- error => {
- if (!error.response) {
- // "Network Error" 网络不通,直接返回
- Message.error({
- content: "网络连接异常,请检查网络设置。",
- duration: 6,
- closable: true
- });
- return Promise.reject(error);
- }
- // 这里是返回状态码不为200时候的错误处理
- let status = error.response.status;
- if (status != 200) {
- const data = error.response.data;
- if (data && data.desc) {
- Message.error({
- content: data.desc,
- duration: 6,
- closable: true
- });
- } else {
- Message.error({
- content: "未定义异常: " + JSON.stringify(data, 2),
- duration: 6,
- closable: true
- });
- }
- }
- // 登录失效 跳转登录页面
- if (status == 403 || status == 401) {
- wk_token = null;
- router.push("/login/" + localStorage.getItem("domain"));
- }
- return Promise.reject(error);
- }
- );
- qmInstance.defaults.withCredentials = true; //允许跨域携带cookie
- qmInstance.defaults.timeout = 10000; //超时时间
- qmInstance.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"; //标识这是一个 ajax 请求
- qmInstance.get = cachingGet(qmInstance.get, [
- /\/api\/exam_question\/question\/\?question_id/,
- /\/api\/exam_question\/paper_struct\/\?exam_record_id=/,
- /\/api\/ecs_exam_work\/exam\/\d+$/,
- /\/api\/ecs_oe_student_face\/upyun$/
- ]);
- loadProgressBar(qmInstance);
- const upyunInstance = axios.create({});
- // FIXME: axios bug. wait 0.19 release. https://github.com/axios/axios/issues/385
- upyunInstance.defaults.headers.common = {};
- // upyunInstance.defaults.headers.common["Authorization"] = UPYUN_HEADER_AUTH;
- upyunInstance.interceptors.request.use(
- config => {
- return qmInstance
- .get("/api/ecs_oe_student_face/upyun")
- .then(res => {
- config.baseURL = res.data.bucketUrl;
- const authorization =
- "Basic " +
- btoa(atob(res.data.upyunOperator) + ":" + atob(res.data.upyunCred));
- config.headers.common["Authorization"] = authorization;
- return config;
- })
- .catch(err => {
- console.log(err);
- });
- },
- error => {
- Message.error({
- content: error,
- duration: 10,
- closable: true
- });
- return Promise.resolve(error);
- }
- );
- loadProgressBar(upyunInstance);
- Vue.prototype.$http = qmInstance;
- Vue.prototype.$upyunhttp = upyunInstance;
- export default {
- install: function(Vue) {
- Object.defineProperty(Vue.prototype, "$http", {
- value: qmInstance
- });
- Object.defineProperty(Vue.prototype, "$upyunhttp", {
- value: upyunInstance
- });
- }
- };
|