diff --git a/gold-system/.env.development b/gold-system/.env.development new file mode 100644 index 0000000..8f4a152 --- /dev/null +++ b/gold-system/.env.development @@ -0,0 +1 @@ +VITE_API_BASE='http://54.251.137.151:10704/' diff --git a/gold-system/.env.production b/gold-system/.env.production new file mode 100644 index 0000000..70913ad --- /dev/null +++ b/gold-system/.env.production @@ -0,0 +1 @@ +VITE_API_BASE='http://54.251.137.151:10702/' diff --git a/gold-system/src/util/http.js b/gold-system/src/util/http.js index 27d1b32..31d1d3f 100644 --- a/gold-system/src/util/http.js +++ b/gold-system/src/util/http.js @@ -1,48 +1,26 @@ -import axios from 'axios'; +import request from './request' +export default function requestHandler(options) { + const { method = 'get', url, data = {}, params = {}, headers = {} } = options -export default function (options) { - //配置每次发送请求都从localStorage中获取名字叫token的数据, - //添加到请求头部的Authorization属性中 - const token = localStorage.getItem('token'); - //Object.assign用于合并对象的数据 - - options.data.token = token; - // options.headers = Object.assign( - // { token: token }, - // options.headers || {} - // ); - - //axios() 返回一个promise对象,用于异步请求 - //options是一个对象,其中包含了许多用于配置请求的参数, - //例如请求的url、请求方法(GET、POST等)、请求头等 - return axios(options) + return request({ + method, + url, + data, + params, + headers + }) .then(({ status, data, statusText }) => { - //该函数在请求成功并返回数据时被调用 - //status:HTTP状态码,例如200表示请求成功。 - //data:服务器返回的数据。 - // statusText:HTTP状态文本,例如"OK"表示请求成功。 - // console.log(data); - if (status == 200) { - return data; + if (status === 200) { + return data } else { - throw new e(statusText); + throw new Error(statusText) + } + }) + .catch(error => { + if (error?.needsLogin) { + return { needsLogin: true } } + return Promise.reject(error) }) - .catch(e=>{ - // 检查是否是因为token过期导致的401错误 - if (e.response && e.response.status === 401) { - // 获取机器码 - const machineId = localStorage.getItem('machineId'); - // 清除localStorage中的token - localStorage.removeItem('token'); - // // 执行重新登录的逻辑,例如跳转到登录页面 - router.push("/login?machineId=" + machineId); - // 可以在这里返回一个特定的值或者对象,以便调用者知道需要重新登录 - return { needsLogin: true }; - } else { - // 其他类型的错误,直接抛出 - return Promise.reject(e); - } - }); -} \ No newline at end of file +} diff --git a/gold-system/src/util/request.js b/gold-system/src/util/request.js new file mode 100644 index 0000000..65659e8 --- /dev/null +++ b/gold-system/src/util/request.js @@ -0,0 +1,43 @@ +import axios from 'axios' +const service = axios.create({ + baseURL: import.meta.env.VITE_API_BASE, + timeout: 10000, + headers: { + 'Content-Type': 'application/json' + } +}) + +// 请求拦截器 +service.interceptors.request.use(config => { + const token = localStorage.getItem('token') + if (token) { + if (config.data) { + config.data.token = token; + } else { + config.data = { token }; + } + // config.headers.Authorization = `${token}` + } + return config +}, error => { + return Promise.reject(error) +}) + +// 响应拦截器 +service.interceptors.response.use( + response => { + return response + }, + error => { + // const { response } = error + // if (response && response.status === 401) { + // const machineId = localStorage.getItem('machineId') + // localStorage.removeItem('token') + // window.location.href = `/login?machineId=${machineId}` + // return Promise.resolve({ needsLogin: true }) + // } + return Promise.reject(error) + } +) + +export default service diff --git a/gold-system/src/views/login.vue b/gold-system/src/views/login.vue index 37803ca..90bb527 100644 --- a/gold-system/src/views/login.vue +++ b/gold-system/src/views/login.vue @@ -1,71 +1,72 @@