李林
2023-08-09 1615528985f30b496d3c294e82136434ee36397c
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
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'
 
Vue.prototype.LOCATIONVUE = "http://127.0.0.1:80/"; //前端本地端口
 
Vue.use(VueAxios, axios)
Vue.config.productionTip = false
Vue.use(VueRouter);
Vue.use(ElementUI);
Vue.use(qs);
Vue.use(api);
 
const javaApi = 'http://localhost:8001/'
 
axios.defaults.baseURL = javaApi
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// x-www-form-urlencoded
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'
  }
  // console.log(config)
  if (config.method === 'post' || config.method === 'put') {
    
    config.data = qs.stringify(config.data)
  }
  if(config.headers['Content-Type'] =='application/json'){
    config.data = qs.parse(config.data)
    // console.log(config.data)
  }
  
  return config
}, function(error) {
  return Promise.reject(error)
})
 
axios.interceptors.response.use(res => {
  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(javaApi + "user/refresh", {
      reToken: sessionStorage.getItem('reToken')
    }).then(res => {
      if (res.data.code==201) {
        Message.error('认证失败,需要重新登录')
        localStorage.removeItem('autoenter')
        window.location.href = '/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) => {
//   // 路径为product时验证是否登录,没有跳转至登录页面
//   if (to.path.indexOf('/') > -1 && 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)
});