Blame view

src/api/axios.js 3.16 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
        if (res.status == 999) {
e5ff81a1   阿宝   集团管理员接口
45
          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
              }
            }
e5ff81a1   阿宝   集团管理员接口
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) => {
b21d90ef   梁保满   长水登录
66
67
      const { data, status } = error.response;
  
13b58a42   梁保满   备题组卷部分前端页面基本完成
68
      Message({
8f573b82   阿宝   组卷接口联调
69
70
71
72
        message: error,
        type: "error",
        duration: 3 * 1000,
      });
13b58a42   梁保满   备题组卷部分前端页面基本完成
73
74
75
      if (error.response == undefined) {
        return Promise.reject(error);
      }
b21d90ef   梁保满   长水登录
76
  
13b58a42   梁保满   备题组卷部分前端页面基本完成
77
      if (status === 403 || status === 401) {
b21d90ef   梁保满   长水登录
78
79
80
81
82
83
84
85
        if (data.status === 999) {
          if (!location.href.includes("localhost")) {
            if (data.data) {
              window.location.href = data.data;
            } else {
              router.push({ path: "/login" });
              if (res.message.includes("不存在")) {
                Message({
225a00b6   梁保满   飞书问题解决
86
                  message: data.info,
b21d90ef   梁保满   长水登录
87
88
89
90
91
92
93
                  type: "error",
                  duration: 3 * 1000,
                });
              }
            }
          }
        }
8f573b82   阿宝   组卷接口联调
94
        Message.closeAll();
c1b532ad   梁保满   权限配置,路由基础设置
95
        Message({
8f573b82   阿宝   组卷接口联调
96
97
98
99
          message: data.info || "未登录或登录超时,即将跳转到登录页面",
          type: "error",
          duration: 3 * 1000,
        });
b21d90ef   梁保满   长水登录
100
  
8f573b82   阿宝   组卷接口联调
101
        if (!window.location.href.includes("login")) {
ee6e7628   梁保满   备题组卷借口数据对接调整
102
          router.push({
8f573b82   阿宝   组卷接口联调
103
            path: "/login",
ee6e7628   梁保满   备题组卷借口数据对接调整
104
            query: {
8f573b82   阿宝   组卷接口联调
105
106
107
              url: window.location.href,
            },
          });
b21d90ef   梁保满   长水登录
108
  
ee6e7628   梁保满   备题组卷借口数据对接调整
109
        }
8f573b82   阿宝   组卷接口联调
110
        return;
c1b532ad   梁保满   权限配置,路由基础设置
111
      }
8f573b82   阿宝   组卷接口联调
112
113
114
115
      return Promise.reject(error.response); // 返回接口返回的错误信息
    }
  );
  export default service;