Blame view

src/api/axios.js 2.7 KB
8f573b82   阿宝   组卷接口联调
1
2
3
4
5
6
  import axios from "axios";
  import Cookies from "js-cookie";
  import NProgress from "nprogress";
  import { Message } from "element-ui";
  import router from "@/router/index";
  import store from "@/store";
13b58a42   梁保满   备题组卷部分前端页面基本完成
7
  import conf from "../config/index"; // 路径配置
c1b532ad   梁保满   权限配置,路由基础设置
8
  // axios默认配置
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
9
10
11
12
13
  const service = axios.create({
    baseURL: conf.baseURL, // api的base_url
    timeout: 600000000000000, // 请求超时时间
    withCredentials: true,
  });
c1b532ad   梁保满   权限配置,路由基础设置
14
  // http request 拦截器
8f573b82   阿宝   组卷接口联调
15
16
17
18
  service.interceptors.request.use(
    (config) => {
      NProgress.start();
      config.headers["Content-Type"] = "application/json;charset=UTF-8";
65f592b6   梁保满   答题卡列表页
19
  
8f573b82   阿宝   组卷接口联调
20
21
22
23
24
25
26
27
28
29
30
31
      const source = axios.CancelToken.source();
      store.commit("setTokenSources", [source.token, source.cancel]);
      config.cancelToken = source.token;
      // if (Cookies.get("access_token")) {
      //   config.headers.Authorization = "Bearer" + Cookies.get("access_token")
      // }
      return config;
    },
    (error) => {
      return Promise.reject(error.response);
    }
  );
c1b532ad   梁保满   权限配置,路由基础设置
32
33
  
  // http response 拦截器
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
34
  service.interceptors.response.use(
8f573b82   阿宝   组卷接口联调
35
    (response) => {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
36
      const res = response.data;
8f573b82   阿宝   组卷接口联调
37
      NProgress.done();
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
38
      if (response.config.cancelToken) {
8f573b82   阿宝   组卷接口联调
39
        store.commit("delTokenSources", response.config.cancelToken);
65f592b6   梁保满   答题卡列表页
40
      }
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
41
      if (response.status == 200) {
ee6e7628   梁保满   备题组卷借口数据对接调整
42
        // Cookies.set("access_token", response.data.message, { expires: 1 / 12 })
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
43
        // console.log(response.status)
8f573b82   阿宝   组卷接口联调
44
45
        if (res.status == 999) {
          // if (!location.href.includes("localhost")) {
13b58a42   梁保满   备题组卷部分前端页面基本完成
46
            if (res.data) {
8f573b82   阿宝   组卷接口联调
47
              window.location.href = res.data;
13b58a42   梁保满   备题组卷部分前端页面基本完成
48
            } else {
8f573b82   阿宝   组卷接口联调
49
50
              router.push({ path: "/login" });
              if (res.message.includes("不存在")) {
13b58a42   梁保满   备题组卷部分前端页面基本完成
51
52
                Message({
                  message: res.message,
8f573b82   阿宝   组卷接口联调
53
54
55
                  type: "error",
                  duration: 3 * 1000,
                });
13b58a42   梁保满   备题组卷部分前端页面基本完成
56
57
              }
            }
8f573b82   阿宝   组卷接口联调
58
          // }
13b58a42   梁保满   备题组卷部分前端页面基本完成
59
        } else {
ee6e7628   梁保满   备题组卷借口数据对接调整
60
          // Cookies.set("access_token", response.data.message, { expires: 1 / 12 })
13b58a42   梁保满   备题组卷部分前端页面基本完成
61
        }
c1b532ad   梁保满   权限配置,路由基础设置
62
      }
8f573b82   阿宝   组卷接口联调
63
      return Promise.resolve(res);
c1b532ad   梁保满   权限配置,路由基础设置
64
    },
8f573b82   阿宝   组卷接口联调
65
    (error) => {
13b58a42   梁保满   备题组卷部分前端页面基本完成
66
      Message({
8f573b82   阿宝   组卷接口联调
67
68
69
70
        message: error,
        type: "error",
        duration: 3 * 1000,
      });
13b58a42   梁保满   备题组卷部分前端页面基本完成
71
72
73
      if (error.response == undefined) {
        return Promise.reject(error);
      }
8f573b82   阿宝   组卷接口联调
74
      const { data, status } = error.response;
13b58a42   梁保满   备题组卷部分前端页面基本完成
75
      if (status === 403 || status === 401) {
8f573b82   阿宝   组卷接口联调
76
        Message.closeAll();
c1b532ad   梁保满   权限配置,路由基础设置
77
        Message({
8f573b82   阿宝   组卷接口联调
78
79
80
81
82
          message: data.info || "未登录或登录超时,即将跳转到登录页面",
          type: "error",
          duration: 3 * 1000,
        });
        if (!window.location.href.includes("login")) {
ee6e7628   梁保满   备题组卷借口数据对接调整
83
          router.push({
8f573b82   阿宝   组卷接口联调
84
            path: "/login",
ee6e7628   梁保满   备题组卷借口数据对接调整
85
            query: {
8f573b82   阿宝   组卷接口联调
86
87
88
              url: window.location.href,
            },
          });
ee6e7628   梁保满   备题组卷借口数据对接调整
89
        }
8f573b82   阿宝   组卷接口联调
90
        return;
c1b532ad   梁保满   权限配置,路由基础设置
91
      }
8f573b82   阿宝   组卷接口联调
92
93
94
95
      return Promise.reject(error.response); // 返回接口返回的错误信息
    }
  );
  export default service;