提交代码

This commit is contained in:
zlwu committed 2025-08-08 03:59:16 +08:00
1 parent d9f760fe1c
commit 01588bf87d
484 files changed
+7228 -104711

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
// request.js 的 TypeScript 声明文件
export interface ApiResponse<T = any> {
code: number
msg: string
data: T
rows?: T[]
total?: number
}
export interface RequestOptions {
url: string
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
data?: any
header?: Record<string, any>
timeout?: number
clientId?: string
}
export interface LoginData {
username: string
password: string
tenantId?: string
clientId?: string
grantType?: string
}
export interface UploadResponse {
code: number
msg: string
data: {
url: string
ossId: string
fileName: string
}
}
export interface DownloadResponse {
statusCode: number
tempFilePath: string
errMsg: string
}
// 主要导出函数
export function request(options: RequestOptions): Promise<ApiResponse>
// 便捷请求方法
export function get<T = any>(url: string, data?: any, header?: Record<string, any>): Promise<ApiResponse<T>>
export function post<T = any>(url: string, data?: any, header?: Record<string, any>): Promise<ApiResponse<T>>
export function put<T = any>(url: string, data?: any, header?: Record<string, any>): Promise<ApiResponse<T>>
export function del<T = any>(url: string, data?: any, header?: Record<string, any>): Promise<ApiResponse<T>>
// 文件操作方法
export function uploadFile(url: string, filePath: string, formData?: Record<string, any>): Promise<UploadResponse>
export function downloadFile(url: string): Promise<DownloadResponse>
export function download(url: string, params: any, fileName: string): Promise<any>
// API对象
export const api: {
login: (data: LoginData) => Promise<ApiResponse>
getUserInfo: () => Promise<ApiResponse>
getTenantList: (isToken?: boolean) => Promise<ApiResponse>
logout: () => Promise<ApiResponse>
}
+141 -3
View File
@@ -89,6 +89,7 @@ export const request = (options) => {
method: options.method || 'GET',
data: options.data || {},
timeout: options.timeout || REQUEST_CONFIG.timeout,
responseType: options.responseType || 'text', // 支持responseType参数
header: {
'Content-Type': 'application/json',
authorization: token,
@@ -200,8 +201,12 @@ export const request = (options) => {
hasEncryptHeader: !!res.header[ENCRYPT_CONFIG.ENCRYPT_HEADER],
});
// 解密处理
if (REQUEST_CONFIG.encryptEnabled && res.header[ENCRYPT_CONFIG.ENCRYPT_HEADER]) {
// 解密处理 - 只对JSON响应进行解密
if (
REQUEST_CONFIG.encryptEnabled &&
res.header[ENCRYPT_CONFIG.ENCRYPT_HEADER] &&
config.responseType === 'text'
) {
try {
console.log('🔓 开始响应解密处理...');
@@ -236,6 +241,12 @@ export const request = (options) => {
// 处理响应
if (res.statusCode === 200) {
// 对于arraybuffer类型的响应,直接返回数据
if (config.responseType === 'arraybuffer') {
resolve(res.data);
return;
}
// 检查业务状态码
if (res.data && res.data.code !== undefined) {
if (res.data.code === 200) {
@@ -356,6 +367,132 @@ export const api = {
}),
};
// 便捷请求方法封装
export const get = (url, data, header) => {
return request({
url,
method: 'GET',
data,
header,
});
};
export const post = (url, data, header) => {
return request({
url,
method: 'POST',
data,
header,
});
};
export const put = (url, data, header) => {
return request({
url,
method: 'PUT',
data,
header,
});
};
export const del = (url, data, header) => {
return request({
url,
method: 'DELETE',
data,
header,
});
};
// 文件上传方法
export const uploadFile = (url, filePath, formData) => {
return new Promise((resolve, reject) => {
const token = getToken();
const clientid = uni.getStorageSync('clientid') || REQUEST_CONFIG.clientId;
// 构建完整URL - 在开发环境下使用localhost:8080
let fullUrl;
if (REQUEST_CONFIG.baseURL.startsWith('/')) {
// 开发环境,baseURL是'/api',需要使用完整的localhost URL
fullUrl = `http://localhost:8080${url}`;
} else {
// 生产环境,直接拼接
fullUrl = REQUEST_CONFIG.baseURL + url;
}
console.log('🚀 上传文件URL:', fullUrl);
console.log('🗂️ 文件路径:', filePath);
console.log('📊 表单数据:', formData);
const headers = {
// 移除Content-Type设置,让uni.uploadFile自动处理
authorization: token,
clientid: clientid,
};
uni.uploadFile({
url: fullUrl,
filePath,
name: 'file',
formData,
header: headers,
success: (res) => {
console.log('✅ 上传成功响应:', res);
try {
const data = JSON.parse(res.data);
resolve(data);
} catch (error) {
console.error('❌ 解析响应数据失败:', error);
reject(new Error('解析响应数据失败'));
}
},
fail: (err) => {
console.error('❌ 上传失败:', err);
reject(new Error(err.errMsg || '上传失败'));
},
});
});
};
// 文件下载方法
export const downloadFile = (url) => {
return new Promise((resolve, reject) => {
const token = getToken();
const clientid = uni.getStorageSync('clientid') || REQUEST_CONFIG.clientId;
console.log('REQUEST_CONFIG.baseURL:', REQUEST_CONFIG.baseURL);
// 构建完整URL - 在开发环境下使用localhost:8080
let fullUrl;
if (REQUEST_CONFIG.baseURL.startsWith('/')) {
// 开发环境,baseURL是'/api',需要使用完整的localhost URL
fullUrl = `http://localhost:8080${url}`;
} else {
// 生产环境,直接拼接
fullUrl = REQUEST_CONFIG.baseURL + url;
}
console.log('📥 下载文件URL:', fullUrl);
const headers = {
authorization: token,
clientid: clientid,
};
uni.downloadFile({
url: fullUrl,
header: headers,
success: (res) => {
console.log('✅ 下载成功:', res);
resolve(res);
},
fail: (err) => {
console.error('❌ 下载失败:', err);
reject(new Error(err.errMsg || '下载失败'));
},
});
});
};
// 通用下载方法
export const download = (url, params, fileName) => {
// 显示加载提示
@@ -376,12 +513,12 @@ export const download = (url, params, fileName) => {
})
.then((resp) => {
// 处理下载响应
// 注意:uni-app中文件下载需要特殊处理
console.log('文件下载完成:', fileName);
uni.hideLoading();
// 这里需要根据平台进行不同的文件保存处理
// 具体实现可能需要平台特定的代码
return resp;
})
.catch((error) => {
console.error('下载失败:', error);
@@ -390,5 +527,6 @@ export const download = (url, params, fileName) => {
title: '下载文件出现错误,请联系管理员!',
icon: 'none',
});
throw error;
});
};