spring
2025-03-19 07a41ade45c962e93a9d449ce1be0eec52e66a6a
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
import Vue from "vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import VueAxios from "vue-axios";
import $ from "jquery";
import qs from "qs";
import App from "./App";
import VueRouter from "vue-router";
import router from "./router/index";
import axios from "axios";
import api from "./assets/api/controller.js";
import swal from "sweetalert";
import Big from "big.js";
import VueBarcode from "vue-barcode";
import Moment from "moment";
 
Vue.prototype.$Big = Big;
 
// 项目切换
Vue.prototype.PROJECT = "检测中心";
// Vue.prototype.PROJECT = '装备电缆'
//本地
Vue.prototype.LOCATIONVUE = "http://127.0.0.1:80";
const javaApi = "http://127.0.0.1:8001";
// const javaApi = "http://192.168.0.170:8001";
 
//通信测试库
// Vue.prototype.LOCATIONVUE = "http://10.1.13.77:8080";
// const javaApi = "http://10.1.13.77:8001";
 
// 通信正式库
// Vue.prototype.LOCATIONVUE = "http://192.168.22.29:8080";
// const javaApi = 'https://ztwxlims.ztt.cn:7443/lims/';
// const javaApi = 'http://192.168.22.29:8001/lims/';
 
// //云
// Vue.prototype.LOCATIONVUE = "http://114.132.189.42:8080";
// const javaApi = 'http://114.132.189.42:1234';
 
// //检测中心正式库
// Vue.prototype.LOCATIONVUE = "http://10.1.200.86:8080";
// const javaApi = 'http://10.1.200.86:8001';
 
//装备电缆测试库
// Vue.prototype.LOCATIONVUE = "http://10.16.173.59";
// const javaApi = 'http://10.16.173.59:8001';
// const javaApi = 'http://192.168.92.249:8001';
 
Vue.prototype.HaveJson = val => {
  return JSON.parse(JSON.stringify(val));
};
Vue.prototype.javaApi = javaApi;
Vue.prototype.$moment = Moment;
 
Vue.use(VueAxios, axios);
Vue.config.productionTip = false;
Vue.config.performance = true;
Vue.use(VueRouter);
Vue.use(ElementUI);
Vue.component("barcode", VueBarcode);
Vue.use(qs);
Vue.use(api);
 
axios.defaults.baseURL = javaApi;
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8";
axios.defaults.withCredentials = true;
axios.defaults.crossDomain = true;
Vue.prototype.$axios = axios;
 
import { Message } from "element-ui";
axios.interceptors.request.use(
  function(config) {
    let tk = sessionStorage.getItem("token");
    let token;
    if (tk != undefined && tk != "") {
      token = tk;
    }
    if (token) {
      config.headers["token"] = "" + token;
      // config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
      // config.headers['Content-Type'] = 'application/json'
    }
    if ((config.method === "post" || config.method === "put") && !config.noQs) {
      config.data = qs.stringify(config.data);
    }
    if (config.headers["Content-Type"] == "application/json" && !config.noQs) {
      config.data = qs.parse(config.data);
    }
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);
 
axios.interceptors.response.use(
  res => {
    if (res.data.code === 201) {
      Message({
        type: "error",
        dangerouslyUseHTMLString: true,
        message: `${res.data.message}`
      });
    }
    return res.data;
  },
  async function(err) {
    if (JSON.stringify(err).indexOf("timeout of") > -1) {
      Message.error("请求超时,请检查网络设置");
    } else if (
      JSON.stringify(err).indexOf("ERR_CONNECTION_RESET") > -1 ||
      JSON.stringify(err).indexOf("Network Error") > -1
    ) {
      Message.error("网络连接错误");
    } else if (err.response.status == "503") {
      Message.error("服务未响应");
    } else if (err.response.status == "404") {
      Message.error("请求失败,链接地址不存在");
    } else if (err.response.status == "403") {
      Message.error("token不存在");
    } else if (err.response.status == "402") {
      Message.error("无效签名,请重新登录");
      localStorage.removeItem("autoenter");
      window.location.href = "/enter";
    } else if (err.response.status == "401") {
      await axios
        .post("/user/refresh", {
          reToken: sessionStorage.getItem("reToken")
        })
        .then(res => {
          if (res.data.code == 201) {
            Message.error("认证失败,需要重新登录");
            localStorage.removeItem("autoenter");
            next({
              path: "/enter"
            });
            return Promise.reject(err);
          }
          sessionStorage.setItem("token", res.data.token);
          sessionStorage.setItem("reToken", res.data.reToken);
        });
      return axios(err.config);
    } else if (err.response.status == "500") {
      Message.error("服务端出现错误");
    }
    return Promise.reject(err);
  }
);
 
router.beforeEach((to, from, next) => {
  if (to.path.indexOf("/test") == 0) {
    next();
    return;
  }
  if (to.path.indexOf("/enter") != 0) {
    if (
      sessionStorage.getItem("token") == null ||
      sessionStorage.getItem("token") == "" ||
      sessionStorage.getItem("token") == undefined
    ) {
      next({
        path: "/enter"
      });
    }
  }
  next();
});
 
new Vue({
  el: "#app",
  router,
  render: h => h(App),
  beforeCreate() {
    // 需要在全局添加一个属性
    Vue.prototype.$bus = this;
  }
});