Blame view

HHECS.Web/src/utils/request.js 2.76 KB
胡菁 authored
1
2
3
4
import store from "@/store";
import { getRefreshToken, getToken } from "@/utils/auth";
import axios from "axios";
import { Message } from "element-ui";
胡菁 authored
5
6
7

// create an axios instance
const service = axios.create({
胡菁 authored
8
  baseURL:
胡菁 authored
9
    process.env.NODE_ENV === "development"
胡菁 authored
10
11
      ? window.appConfig.baseUrlOffLine
      : window.appConfig.baseUrlOnLine, // process.env.VUE_APP_BASE_API, // url = base url + request url
胡菁 authored
12
13
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
胡菁 authored
14
15
  timeout: 5000, // request timeout
});
胡菁 authored
16
17
18

// request interceptor
service.interceptors.request.use(
胡菁 authored
19
  (config) => {
胡菁 authored
20
21
22
23
24
    // do something before request is sent
    // console.log(config);
    // if (config.url.indexOf("/user/") > -1) {
    //     config.baseURL = process.env.VUE_APP_BASE_API;
    // }
胡菁 authored
25
胡菁 authored
26
    // console.log(store.getters.token)
胡菁 authored
27
    if (store.getters.token) {
胡菁 authored
28
29
30
31
32
33
34
35
36
37
      config.headers["Authorization"] = "Bearer " + getToken();
      config.headers["x-accesstoken"] = getRefreshToken();
      var lang = localStorage.getItem("lang");
      var language = "zh";
      if (lang === "zh") {
        language = "zh-CN";
      } else if (lang === "en") {
        language = "en-US";
      }
      config.headers["Accept-Language"] = language;
胡菁 authored
38
    }
胡菁 authored
39
    return config;
胡菁 authored
40
  },
胡菁 authored
41
  (error) => {
胡菁 authored
42
    // do something with request error
胡菁 authored
43
44
    console.log(error); // for debug
    return Promise.reject(error);
胡菁 authored
45
  }
胡菁 authored
46
);
胡菁 authored
47
48
49

// response interceptor
service.interceptors.response.use(
胡菁 authored
50
  (response) => {
胡菁 authored
51
52
53
54
    const res = response.data;
    if (res.code !== "Success") {
      if (res.msg === "" || res.msg === null || res.msg === undefined) {
        return;
胡菁 authored
55
      }
胡菁 authored
56
      Message({
胡菁 authored
57
58
59
60
61
        message: res.msg || "Error",
        type: "error",
        duration: 5 * 1000,
      });
      return Promise.reject(new Error(res.msg || "Error"));
胡菁 authored
62
    } else {
胡菁 authored
63
      return res;
胡菁 authored
64
    }
胡菁 authored
65
  },
胡菁 authored
66
  (error) => {
胡菁 authored
67
    // console.log(error.response)
胡菁 authored
68
    let errorMessage = "请求错误";
胡菁 authored
69
    if (error.response) {
胡菁 authored
70
      if (error.response.data.code === "401") {
胡菁 authored
71
        Message({
胡菁 authored
72
73
74
75
76
77
78
          message: "用户超时,请重新登录",
          type: "error",
          duration: 5 * 1000,
        });
        store.dispatch("user/logout");
        location.reload();
        return;
胡菁 authored
79
      }
胡菁 authored
80
81
      errorMessage = `请求失败:${error.response.status} - ${
        error.response.data?.msg || error.response.statusText
胡菁 authored
82
      }`;
胡菁 authored
83
84
      // console.log(errorMessage);
    } else if (error.request) {
胡菁 authored
85
      errorMessage = "服务器未响应,请检查网络连接";
胡菁 authored
86
    } else {
胡菁 authored
87
      errorMessage = error.message || "未知错误";
胡菁 authored
88
89
90
    }
    Message({
      message: errorMessage,
胡菁 authored
91
92
93
94
      type: "error",
      duration: 5 * 1000,
    });
    return Promise.reject(error);
胡菁 authored
95
  }
胡菁 authored
96
);
胡菁 authored
97
胡菁 authored
98
export default service;