4.9更新
This commit is contained in:
parent
71aba3f929
commit
b95d85b229
@ -1 +1 @@
|
|||||||
VITE_API_BASE_URL = 'https://ai.atvoe.com/'
|
VITE_API_BASE_URL = 'https://opencomputing.ai'
|
||||||
|
|||||||
54
src/utlis/cookie.ts
Normal file
54
src/utlis/cookie.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
// utils/cookie.ts
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定名称的 Cookie 值
|
||||||
|
*/
|
||||||
|
export function getCookie(name: string): string | null {
|
||||||
|
const value = `; ${document.cookie}`;
|
||||||
|
const parts = value.split(`; ${name}=`);
|
||||||
|
if (parts.length === 2) {
|
||||||
|
const raw = parts.pop()?.split(';').shift();
|
||||||
|
return raw ? decodeURIComponent(raw) : null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置 Cookie
|
||||||
|
* @param name 名称
|
||||||
|
* @param value 值
|
||||||
|
* @param days 有效期(天),不传则为会话 Cookie
|
||||||
|
*/
|
||||||
|
export function setCookie(name: string, value: string, days?: number): void {
|
||||||
|
let expires = '';
|
||||||
|
if (days) {
|
||||||
|
const date = new Date();
|
||||||
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||||
|
expires = `; expires=${date.toUTCString()}`;
|
||||||
|
}
|
||||||
|
// 注意:为了安全,实际项目中可根据需要加上 Secure / SameSite 等属性
|
||||||
|
document.cookie = `${name}=${encodeURIComponent(value)}${expires}; path=/`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除 Cookie
|
||||||
|
*/
|
||||||
|
export function deleteCookie(name: string): void {
|
||||||
|
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调试:打印所有可读 Cookie(非 HttpOnly)
|
||||||
|
*/
|
||||||
|
export function logAllCookies(): void {
|
||||||
|
console.log('当前可读 Cookie 原始字符串:', document.cookie || '(空)');
|
||||||
|
if (!document.cookie) return;
|
||||||
|
const cookies = document.cookie.split('; ').reduce((acc, item) => {
|
||||||
|
const eqIndex = item.indexOf('=');
|
||||||
|
const key = item.slice(0, eqIndex);
|
||||||
|
const value = item.slice(eqIndex + 1);
|
||||||
|
acc[key] = decodeURIComponent(value);
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<string, string>);
|
||||||
|
console.table(cookies);
|
||||||
|
}
|
||||||
@ -2,29 +2,35 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { ElMessage } from 'element-plus'
|
import { ElMessage } from 'element-plus'
|
||||||
// import { ElMessage } from 'element-plus'
|
// import { ElMessage } from 'element-plus'
|
||||||
import { useLoginStore } from '@/store/LoginStore'
|
|
||||||
|
const apiBaseURL = import.meta.env.VITE_API_BASE_URL
|
||||||
|
|
||||||
|
const shouldIncludeCredentials = (url?: string, baseURL?: string) => {
|
||||||
|
if (!url) return true
|
||||||
|
|
||||||
|
try {
|
||||||
|
const apiOrigin = new URL(apiBaseURL).origin
|
||||||
|
const requestOrigin = new URL(url, baseURL || apiBaseURL).origin
|
||||||
|
return requestOrigin === apiOrigin
|
||||||
|
} catch {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 请求拦截器
|
// 请求拦截器
|
||||||
// 添加请求拦截器
|
// 添加请求拦截器
|
||||||
axios.interceptors.request.use(
|
axios.interceptors.request.use(
|
||||||
function (config) {
|
function (config) {
|
||||||
// console.log('=== 请求拦截器 ===')
|
// console.log('=== 请求拦截器 ===')
|
||||||
// console.log('请求 URL:', config.url)
|
console.log('请求 URL:', config)
|
||||||
// console.log('请求方法:', config.method)
|
// console.log('请求方法:', config.method)
|
||||||
// console.log('请求参数 (params):', config.params)
|
// console.log('请求参数 (params):', config.params)
|
||||||
// console.log('请求数据 (data):', config.data)
|
// console.log('请求数据 (data):', config.data)
|
||||||
// console.log('请求头:', config.headers)
|
console.log('请求头:', config.headers)
|
||||||
|
config.baseURL = apiBaseURL
|
||||||
config.baseURL = import.meta.env.VITE_API_BASE_URL
|
config.withCredentials = shouldIncludeCredentials(config.url, config.baseURL)
|
||||||
|
|
||||||
|
|
||||||
const Store = useLoginStore()
|
|
||||||
|
|
||||||
if (Store.userInfo && Store.userInfo.user && Store.userInfo.user.id) {
|
|
||||||
config.headers.Authorization = Store.userInfo.user.id
|
|
||||||
|
|
||||||
}
|
|
||||||
return config
|
return config
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
// 对请求错误做些什么
|
// 对请求错误做些什么
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
@ -59,8 +65,8 @@ axios.interceptors.response.use(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
});
|
});
|
||||||
const http = <T, D, P>(url: string, data?: D, params?: P, method: 'get' | 'PUT' |'Delete'|'post' = 'get'): Promise<T> => {
|
const http = <T, D, P>(url: string, data?: D, params?: P, method: 'get' | 'PUT' | 'Delete' | 'post' = 'get'): Promise<T> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
axios({
|
axios({
|
||||||
method,
|
method,
|
||||||
@ -100,7 +106,7 @@ export const PUT = <T, D>(url: string, data?: D): Promise<T> => {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const uploadFile=async(file:File, uploadUrl:string, additionalData: Record<string, string> = {})=> {
|
export const uploadFile = async (file: File, uploadUrl: string, additionalData: Record<string, string> = {}) => {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', file); // 'file'是后端接收文件的字段名
|
formData.append('file', file); // 'file'是后端接收文件的字段名
|
||||||
|
|
||||||
@ -116,7 +122,7 @@ export const uploadFile=async(file:File, uploadUrl:string, additionalData: Recor
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("000",response.data);
|
console.log("000", response.data);
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user