Blame view

src/api/axios.js 2.82 KB
8f573b82   阿宝   组卷接口联调
1
  import axios from "axios";
8f573b82   阿宝   组卷接口联调
2
3
4
5
  import NProgress from "nprogress";
  import { Message } from "element-ui";
  import router from "@/router/index";
  import store from "@/store";
13b58a42   梁保满   备题组卷部分前端页面基本完成
6
  import conf from "../config/index"; // 路径配置
0bb1a617   梁保满   兼容长水登录错误信息隐藏
7
8
  import { getURLParams } from "@/utils";
  
d06cf631   梁保满   兼容长水登录错误
9
  let code = getURLParams("code") || getURLParams("dockkey") || localStorage.getItem("csCode") || "";
0bb1a617   梁保满   兼容长水登录错误信息隐藏
10
11
  
  
c1b532ad   梁保满   权限配置,路由基础设置
12
  // axios默认配置
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
13
14
15
16
17
  const service = axios.create({
    baseURL: conf.baseURL, // api的base_url
    timeout: 600000000000000, // 请求超时时间
    withCredentials: true,
  });
c1b532ad   梁保满   权限配置,路由基础设置
18
  // http request 拦截器
8f573b82   阿宝   组卷接口联调
19
20
21
  service.interceptors.request.use(
    (config) => {
      NProgress.start();
255e2506   梁保满   飞书bug及优化
22
      // config.headers["Content-Type"] = "application/json;charset=UTF-8";
65f592b6   梁保满   答题卡列表页
23
  
8f573b82   阿宝   组卷接口联调
24
25
26
      const source = axios.CancelToken.source();
      store.commit("setTokenSources", [source.token, source.cancel]);
      config.cancelToken = source.token;
8f573b82   阿宝   组卷接口联调
27
28
29
30
31
32
      return config;
    },
    (error) => {
      return Promise.reject(error.response);
    }
  );
c1b532ad   梁保满   权限配置,路由基础设置
33
34
  
  // http response 拦截器
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
35
  service.interceptors.response.use(
8f573b82   阿宝   组卷接口联调
36
    (response) => {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
37
      const res = response.data;
8f573b82   阿宝   组卷接口联调
38
      NProgress.done();
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
39
      if (response.config.cancelToken) {
8f573b82   阿宝   组卷接口联调
40
        store.commit("delTokenSources", response.config.cancelToken);
65f592b6   梁保满   答题卡列表页
41
      }
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
42
      if (response.status == 200) {
8f573b82   阿宝   组卷接口联调
43
        if (res.status == 999) {
bbc51d4b   梁保满   组卷添加修改分数,打开新开页面跳转...
44
          // if (!location.href.includes("localhost")) {
0bb1a617   梁保满   兼容长水登录错误信息隐藏
45
46
47
48
49
50
51
52
53
54
          if (res.data) {
            window.location.href = res.data;
          } else {
            router.push({ path: "/login" });
            if (res.info.includes("不存在")) {
              Message({
                info: res.info,
                type: "error",
                duration: 3 * 1000,
              });
13b58a42   梁保满   备题组卷部分前端页面基本完成
55
            }
0bb1a617   梁保满   兼容长水登录错误信息隐藏
56
          }
bbc51d4b   梁保满   组卷添加修改分数,打开新开页面跳转...
57
          // }
d06cf631   梁保满   兼容长水登录错误
58
        }
c1b532ad   梁保满   权限配置,路由基础设置
59
      }
8f573b82   阿宝   组卷接口联调
60
      return Promise.resolve(res);
c1b532ad   梁保满   权限配置,路由基础设置
61
    },
8f573b82   阿宝   组卷接口联调
62
    (error) => {
b21d90ef   梁保满   长水登录
63
      const { data, status } = error.response;
13b58a42   梁保满   备题组卷部分前端页面基本完成
64
65
66
      if (error.response == undefined) {
        return Promise.reject(error);
      }
b21d90ef   梁保满   长水登录
67
  
13b58a42   梁保满   备题组卷部分前端页面基本完成
68
      if (status === 403 || status === 401) {
b21d90ef   梁保满   长水登录
69
        if (data.status === 999) {
6d7bd862   梁保满   飞书bug
70
          if (data.data) {
255e2506   梁保满   飞书bug及优化
71
            window.location.href = data.data;
6d7bd862   梁保满   飞书bug
72
73
74
75
76
77
78
          } else {
            Message({
              message: data.info,
              type: "error",
              duration: 3 * 1000,
            });
            router.push({ path: "/login" });
b21d90ef   梁保满   长水登录
79
          }
0bb1a617   梁保满   兼容长水登录错误信息隐藏
80
81
82
83
84
          if (code) {
            return
          } else {
            return Promise.resolve(data);
          }
b21d90ef   梁保满   长水登录
85
        }
8f573b82   阿宝   组卷接口联调
86
        Message.closeAll();
c1b532ad   梁保满   权限配置,路由基础设置
87
        Message({
8f573b82   阿宝   组卷接口联调
88
89
90
91
          message: data.info || "未登录或登录超时,即将跳转到登录页面",
          type: "error",
          duration: 3 * 1000,
        });
b21d90ef   梁保满   长水登录
92
  
8f573b82   阿宝   组卷接口联调
93
        if (!window.location.href.includes("login")) {
ee6e7628   梁保满   备题组卷借口数据对接调整
94
          router.push({
8f573b82   阿宝   组卷接口联调
95
            path: "/login",
ee6e7628   梁保满   备题组卷借口数据对接调整
96
            query: {
8f573b82   阿宝   组卷接口联调
97
98
99
              url: window.location.href,
            },
          });
b21d90ef   梁保满   长水登录
100
  
ee6e7628   梁保满   备题组卷借口数据对接调整
101
        }
8f573b82   阿宝   组卷接口联调
102
        return;
c1b532ad   梁保满   权限配置,路由基础设置
103
      }
29319cfb   梁保满   设备。教师管理
104
105
106
107
108
109
110
  
      return Promise.resolve(
        {
          status: status,
          info: data.info || error,
        }
      ); // 返回接口返回的错误信息
8f573b82   阿宝   组卷接口联调
111
112
113
    }
  );
  export default service;