Blame view

src/api/axios.js 3.04 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
  service.interceptors.request.use(
    (config) => {
      NProgress.start();
255e2506   梁保满   飞书bug及优化
18
      // 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) {
236b1f0e   梁保满   周末-飞书bug
42
        // Cookies.set("access_token", response.data.info, { 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
              router.push({ path: "/login" });
255e2506   梁保满   飞书bug及优化
50
              if (res.info.includes("不存在")) {
13b58a42   梁保满   备题组卷部分前端页面基本完成
51
                Message({
255e2506   梁保满   飞书bug及优化
52
                  info: res.info,
8f573b82   阿宝   组卷接口联调
53
54
55
                  type: "error",
                  duration: 3 * 1000,
                });
13b58a42   梁保满   备题组卷部分前端页面基本完成
56
57
              }
            }
e5ff81a1   阿宝   集团管理员接口
58
          }
13b58a42   梁保满   备题组卷部分前端页面基本完成
59
        } else {
236b1f0e   梁保满   周末-飞书bug
60
          // Cookies.set("access_token", response.data.info, { expires: 1 / 12 })
13b58a42   梁保满   备题组卷部分前端页面基本完成
61
        }
c1b532ad   梁保满   权限配置,路由基础设置
62
      }
8f573b82   阿宝   组卷接口联调
63
      return Promise.resolve(res);
c1b532ad   梁保满   权限配置,路由基础设置
64
    },
8f573b82   阿宝   组卷接口联调
65
    (error) => {
b21d90ef   梁保满   长水登录
66
      const { data, status } = error.response;
13b58a42   梁保满   备题组卷部分前端页面基本完成
67
68
69
      if (error.response == undefined) {
        return Promise.reject(error);
      }
b21d90ef   梁保满   长水登录
70
  
13b58a42   梁保满   备题组卷部分前端页面基本完成
71
      if (status === 403 || status === 401) {
b21d90ef   梁保满   长水登录
72
        if (data.status === 999) {
6d7bd862   梁保满   飞书bug
73
74
          console.log(data.data)
          if (data.data) {
255e2506   梁保满   飞书bug及优化
75
            window.location.href = data.data;
6d7bd862   梁保满   飞书bug
76
77
78
79
80
81
82
          } else {
            Message({
              message: data.info,
              type: "error",
              duration: 3 * 1000,
            });
            router.push({ path: "/login" });
b21d90ef   梁保满   长水登录
83
          }
6d7bd862   梁保满   飞书bug
84
          return
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
      }
6d7bd862   梁保满   飞书bug
104
105
106
107
108
      Message({
        message: data.info || error,
        type: "error",
        duration: 3 * 1000,
      });
8f573b82   阿宝   组卷接口联调
109
110
111
112
      return Promise.reject(error.response); // 返回接口返回的错误信息
    }
  );
  export default service;