zhaoxiaoqiang1
2026-01-04 f1d30d03186c79ca2cbcfe60d6d2ce7d73fba97b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/** axios封装
 * 请求拦截、相应拦截、错误统一处理
 */
import axios from "axios";
// import QS from 'qs'
// import store from '../store/index'
import router from "@/router/index";
import { Message } from "element-ui";
// console.log('VUE_PRODUCT_API_HOST', process.env, process.env.VUE_APP_PRODUCT_API_HOST)
let baseUrl = ''
if (location.hostname === "fin.pengyouph.com") {
  baseUrl = process.env.VUE_APP_PRODUCT_XXXFINTECH_API_HOST;
} else if (location.host === '10.1.1.163' || location.host === '10.1.1.149') {
  baseUrl = process.env.VUE_APP_API_ORIGIN;
} else {
  baseUrl = window.location.origin;
}
axios.defaults.baseURL = baseUrl;
 
axios.defaults.withCredentials = true;
 
// 请求超时时间
axios.defaults.timeout = 30000;
 
// post请求头
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
 
// 响应拦截器
axios.interceptors.response.use(
  response => {
    Message.closeAll();
    // 您的登录已失效
    if (typeof response.data === "object") {
      const { errorCode, code, data, ext } = response.data;
      const eCode = code || errorCode;
      const eData = ext || data;
      if (eCode === "003" && typeof eData === "object") {
        // parent.location.reload();
        return Promise.reject(response.data);
      }
    }
    if (response.status === 200) {
      if (response.data.code !== "00") {
        Message({
          message: response.data.msg,
          type: "error",
          duration: 6000
        });
      }
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  // 服务器状态码不是200的情况
  error => {
    if (error.response.status) {
      switch (error.response.status) {
        // 401: 未登录
        // 未登录则跳转登录页面,并携带当前页面的路径
        // 在登录成功后返回当前页面,这一步需要在登录页操作。
        case 401:
          router.replace({
            path: "/login",
            query: {
              redirect: router.currentRoute.fullPath
            }
          });
          break;
        // 403 token过期
        // 登录过期对用户进行提示
        // 清除本地token和清空vuex中token对象
        // 跳转登录页面
        case 403:
          Message({
            message: "登录过期,请重新登录",
            type: "error",
            duration: 6000
          });
          // 清除token
          // localStorage.removeItem('token')
          // store.commit('loginSuccess', null)
          // 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
          setTimeout(() => {
            router.replace({
              path: "/login",
              query: {
                redirect: router.currentRoute.fullPath
              }
            });
          }, 1000);
          break;
        // 404请求不存在
        case 404:
          Message({
            message: "网络请求不存在",
            type: "error",
            duration: 6000
          });
          break;
        // 其他错误,直接抛出错误提示
        default:
          error.response &&
            error.response.data &&
            error.response.data.message &&
            Message({
              message: error.response.data.message,
              type: "error",
              duration: 6000
            });
      }
      return Promise.reject(error.response);
    }
  }
);
 
/**
 * addMenuId api地址添加全局参数menuId
 * @param {String} url 接口地址
 */
function addMenuId(url) {
  // 本地调试使用
  const menuId = parent.g_menu_id || "";
  const contactStr = url.includes("?") ? "&" : "?";
  if (url.includes("menuId=")) {
    return url;
  }
  return `${url}${contactStr}menuId=${menuId}`;
}
 
/**
 * get方法,对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .get(addMenuId(url), {
        params: params
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
/**
 * post方法,对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params, responseType) {
  return new Promise((resolve, reject) => {
    axios
      .post(addMenuId(url), params, responseType)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        // console.log(err)
        reject(err.data);
      });
  });
}
// 文件下载
export function downFile(url, params) {
  return axios({
    headers: {
      "Content-Type": "application/json"
    },
    responseType: "blob", // 一定要写
    method: "post",
    url: url,
    params: params
  }).then(res => {
    if (!res || !res.data) {
      return;
    }
    Message.closeAll();
    const blob = new Blob([res.data]); // 处理文档流
    const elink = document.createElement("a");
    elink.download = params.fileName;
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  });
}