Compare commits
merge into: yanjiaqing:dev
yanjiaqing:4/1日二期
yanjiaqing:Hongxilin
yanjiaqing:activity_recharge_detail_audit_workspace_hxl
yanjiaqing:dev
yanjiaqing:donghaolin
yanjiaqing:donghaolin-2
yanjiaqing:feat/lhl
yanjiaqing:feat/zry
yanjiaqing:lhl
yanjiaqing:main
yanjiaqing:master
yanjiaqing:rate_recharge_consume_refund_donghaolin
yanjiaqing:test
yanjiaqing:zln
yanjiaqing:zln1
yanjiaqing:zry
pull from: yanjiaqing:zln
yanjiaqing:4/1日二期
yanjiaqing:Hongxilin
yanjiaqing:activity_recharge_detail_audit_workspace_hxl
yanjiaqing:dev
yanjiaqing:donghaolin
yanjiaqing:donghaolin-2
yanjiaqing:feat/lhl
yanjiaqing:feat/zry
yanjiaqing:lhl
yanjiaqing:main
yanjiaqing:master
yanjiaqing:rate_recharge_consume_refund_donghaolin
yanjiaqing:test
yanjiaqing:zln
yanjiaqing:zln1
yanjiaqing:zry
2 Commits
Author | SHA1 | Message | Date |
---|---|---|---|
|
07ab46dcfa |
合并dev
|
1 month ago |
|
499f0e560f |
封装请求
|
1 month ago |
8 changed files with 134 additions and 102 deletions
-
1gold-system/.env.development
-
1gold-system/.env.production
-
2gold-system/package.json
-
35gold-system/src/api/index.js
-
58gold-system/src/util/http.js
-
38gold-system/src/util/request.js
-
29gold-system/src/views/login.vue
-
32gold-system/vite.config.ts
@ -0,0 +1 @@ |
|||||
|
VITE_API_BASE='http://54.251.137.151:10704/' |
@ -0,0 +1 @@ |
|||||
|
VITE_API_BASE=http://54.251.137.151:10702/ |
@ -1,13 +1,26 @@ |
|||||
import { pa } from 'element-plus/es/locales.mjs'; |
|
||||
import http from '../util/http.js'; |
|
||||
|
import request from './request' |
||||
|
|
||||
const API={ |
|
||||
post: function(url,data){ |
|
||||
return http({url:url,method:'post',data:data}) |
|
||||
}, |
|
||||
postN: function(url,params){ |
|
||||
return http({url:url,method:'post',params:params}) |
|
||||
}, |
|
||||
}; |
|
||||
|
export default function requestHandler(options) { |
||||
|
const { method = 'get', url, data = {}, params = {}, headers = {} } = options |
||||
|
|
||||
export default API; |
|
||||
|
return request({ |
||||
|
method, |
||||
|
url, |
||||
|
data, |
||||
|
params, |
||||
|
headers |
||||
|
}) |
||||
|
.then(({ status, data, statusText }) => { |
||||
|
if (status === 200) { |
||||
|
return data |
||||
|
} else { |
||||
|
throw new Error(statusText) |
||||
|
} |
||||
|
}) |
||||
|
.catch(error => { |
||||
|
if (error?.needsLogin) { |
||||
|
return { needsLogin: true } |
||||
|
} |
||||
|
return Promise.reject(error) |
||||
|
}) |
||||
|
} |
@ -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 }) => { |
.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 { |
} else { |
||||
throw new e(statusText); |
|
||||
|
throw new Error(statusText) |
||||
} |
} |
||||
}) |
}) |
||||
.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); |
|
||||
|
.catch(error => { |
||||
|
if (error?.needsLogin) { |
||||
|
return { needsLogin: true } |
||||
} |
} |
||||
}); |
|
||||
|
return Promise.reject(error) |
||||
|
}) |
||||
} |
} |
@ -0,0 +1,38 @@ |
|||||
|
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) { |
||||
|
config.headers.Authorization = `Bearer ${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 |
Write
Preview
Loading…
Cancel
Save
Reference in new issue