xieb
2025-03-06 8b23471d428d7d0a6444726cb5c1603a8b3d4010
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
/*
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2022-07-29 15:19:13
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2023-04-21 10:16:58
 * @FilePath: \web\bigScreen\src\router\axios.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * 全站http配置
 *
 * axios参数说明
 * isSerialize是否开启form表单提交
 * isToken是否需要token
 */
import axios from "axios";
import { getToken } from "@/utils/auth.js";
import Vue from "vue";
import Vant from "vant";
import router from "../router/page";
import "vant/lib/index.css";
import store from "../store/index";
import { Loginto } from "@/api/hfiveget/index.js";
Vue.use(Vant);
 
// 调用后台管理的
const service = axios.create({
  timeout: 600000, // request timeout
});
 
// 返回其他状态码
service.defaults.validateStatus = function (status) {
  return status >= 200 && status <= 500;
};
// 跨域请求,允许保存cookie
// service.defaults.withCredentials = true
 
// http request拦截
service.interceptors.request.use(
  (config) => {
    if (!config.data) {
      config.data = true // 解决请求没有参数时添加不上Content-Type问题
    }
    store.commit('updateoverlayshow');
    if (config.apiKey) {
      config.headers["Authorization"] = "Basic c2FiZXI6c2FiZXJfc2VjcmV0";
      if (getToken() != undefined) {
        config.headers["Blade-Auth"] = "bearer " + getToken();
      }
    }
    config.cancelToken = new axios.CancelToken((cancel) => {
      window._axiosPromiseArr.push({ cancel });
    });
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
 
// http response 拦截
service.interceptors.response.use(
  (res) => {
    store.commit('reoverlayshow');
    if (res.data.code == 401) {
      Vue.prototype.$toast.fail(res.data.msg)
      localStorage.clear()
      setTimeout(() => {
        router.push("/discrimination");
      }, 1000);
    }
    return res;
  },
  (error) => {
    return Promise.reject(new Error(error));
  }
);
 
export default service;