Files
ApiServer-Web-admin_dashboa…/src/utils/request.js
T
lin f6dcec75d7
Build and Deploy Vue3 / build (push) Successful in 1m6s
Build and Deploy Vue3 / deploy (push) Successful in 10m44s
feate:添加拼单类型接口
2025-12-31 13:07:05 +08:00

161 lines
4.7 KiB
JavaScript

import axios from 'axios'
import { ElMessage } from 'element-plus'
import router from '@/router'
// 基础URL
const baseUrl = 'https://apiservertest.s1f.ren' // SSL证书有问题
// const baseUrl = 'http://apiservertest.s1f.ren' // HTTP版本
// const baseUrl = 'https://cloudapi.007yjs.com' // 尝试备用地址
// 检查URL是否需要认证
const urlNeedAuth = (url) => {
// 这里可以添加不需要认证的URL列表
const noAuthUrls = ['/v1/user/login', '/v1/user/check/get_code_img', '/v1/user/register']
return !noAuthUrls.some(noAuthUrl => url.includes(noAuthUrl))
}
// 检查token是否过期
const isTokenExpired = () => {
const token = localStorage.getItem('token')
if (!token) return true
// 这里可以添加token过期检查逻辑,如果有JWT可以解析它
// 简单实现,仅检查token是否存在
return false
}
class Request {
constructor(config = {}) {
// 创建 axios 实例
this.instance = axios.create({
baseURL: config.baseURL || '',
timeout: config.timeout || 5000,
headers: config.headers || {}
})
// 请求拦截器
this.instance.interceptors.request.use(
(config) => {
// 在发送请求之前做些什么
// 例如:添加 token
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// 响应拦截器
this.instance.interceptors.response.use(
(response) => {
// 对响应数据做点什么
return response.data
},
(error) => {
// 对响应错误做点什么
// if (error.response) {
// switch (error.response.status) {
// case 401:
// // 未授权,可以在这里处理登出逻辑
// break
// case 403:
// // 禁止访问
// break
// case 404:
// // 未找到
// break
// case 500:
// // 服务器错误
// break
// }
// }
return error.response
}
)
}
// GET 请求
get(url, params = {}, config = {}) {
return this.instance.get(url, { params, ...config })
}
// POST 请求
post(url, data = {}, config = {}) {
return this.instance.post(url, data, config)
}
// PUT 请求
put(url, data = {}, config = {}) {
return this.instance.put(url, data, config)
}
// DELETE 请求
delete(url, config = {}) {
return this.instance.delete(url, config)
}
// PATCH 请求
patch(url, data = {}, config = {}) {
return this.instance.patch(url, data, config)
}
}
// 创建默认实例
const request = new Request({
baseURL: baseUrl,
timeout: 50000,
headers: {
'Content-Type': 'multipart/form-data'
}
})
export const mainUrl = baseUrl + '/acs'
export const baseURL = baseUrl
export const http2 = axios.create({
baseURL: baseUrl,
timeout: 30000,
headers: {},
});
http2.interceptors.request.use(config => {
const token = localStorage.getItem('token'); // 假设 token 存储在 localStorage
if(urlNeedAuth(config.url) && isTokenExpired()){
if (token){
localStorage.removeItem('token');
ElMessage.warning('登陆过期,请重新登陆')
}
router.push('/login')
return Promise.reject();
}
config.headers.Authorization = `Bearer ${token}`;
config.url = config.url
return config
})
http2.interceptors.response.use(
response => response, // 正常响应时直接返回
error => {
console.log('出现错误', error.response);
if (error.response == undefined) {
ElMessage.error("服务器错误,请稍后再试");
return Promise.reject(error);
}
const { status } = error.response;
if (status === 401) {
localStorage.removeItem('token');
ElMessage.warning('登陆过期,请重新登陆')
router.push('/login')
return Promise.reject();
}
// 其他错误处理(可选)
return Promise.reject(error);
}
);
export default request