新增: 实时热卖榜(11分类/想要人数排序/热门搜索词热度)、价格监控(目标价通知/卖家信用筛选/销量下限/AI精准监控)、 一键转卖(收藏自动加载/改价直发/详情直达)、AI回复免费token(ai.88531.cn)、API密钥掩码、 推理模型兼容修复、商品本地编辑、授权系统接入(在线人数/版本/公告)、品牌更名躺赚鱼。 README 重写为项目介绍(改动清单/优势/真实截图/两种安装方式/体验地址 tzy.88531.cn/使用引导), 原版 README 完整保留为 README.upstream.md。
88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
|
|
const request: AxiosInstance = axios.create({
|
|
baseURL: '',
|
|
timeout: 30000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
request.interceptors.request.use(
|
|
(config) => {
|
|
const token = localStorage.getItem('auth_token');
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
// 实例上默认写死了 application/json,发 FormData 时必须去掉,
|
|
// 否则表单会被当成 JSON 发送,后端 Form(...) 解析不到字段而返回 422。
|
|
// 删掉后 axios 会自动补上带 boundary 的 multipart/form-data。
|
|
if (typeof FormData !== 'undefined' && config.data instanceof FormData) {
|
|
delete config.headers['Content-Type'];
|
|
}
|
|
return config;
|
|
},
|
|
(error) => Promise.reject(error),
|
|
);
|
|
|
|
request.interceptors.response.use(
|
|
(response: AxiosResponse) => response,
|
|
(error: AxiosError) => {
|
|
if (error.response?.status === 401) {
|
|
localStorage.removeItem('auth_token');
|
|
try {
|
|
window.dispatchEvent(new Event('auth:logout'));
|
|
} catch {
|
|
/* no-op (SSR) */
|
|
}
|
|
}
|
|
return Promise.reject(error);
|
|
},
|
|
);
|
|
|
|
export const get = async <T = unknown>(
|
|
url: string,
|
|
params?: Record<string, any>,
|
|
config?: AxiosRequestConfig,
|
|
): Promise<T> => {
|
|
const response = await request.get<T>(url, { params, ...config });
|
|
return response.data;
|
|
};
|
|
|
|
export const post = async <T = unknown>(
|
|
url: string,
|
|
data?: unknown,
|
|
config?: AxiosRequestConfig,
|
|
): Promise<T> => {
|
|
const response = await request.post<T>(url, data, config);
|
|
return response.data;
|
|
};
|
|
|
|
export const put = async <T = unknown>(
|
|
url: string,
|
|
data?: unknown,
|
|
config?: AxiosRequestConfig,
|
|
): Promise<T> => {
|
|
const response = await request.put<T>(url, data, config);
|
|
return response.data;
|
|
};
|
|
|
|
export const patch = async <T = unknown>(
|
|
url: string,
|
|
data?: unknown,
|
|
config?: AxiosRequestConfig,
|
|
): Promise<T> => {
|
|
const response = await request.patch<T>(url, data, config);
|
|
return response.data;
|
|
};
|
|
|
|
export const del = async <T = unknown>(
|
|
url: string,
|
|
config?: AxiosRequestConfig,
|
|
): Promise<T> => {
|
|
const response = await request.delete<T>(url, config);
|
|
return response.data;
|
|
};
|
|
|
|
export default request;
|