279 lines
5.8 KiB
JavaScript
279 lines
5.8 KiB
JavaScript
/**
|
|
* API配置文件
|
|
* 统一管理所有API地址和配置
|
|
*/
|
|
|
|
// API基础地址配置
|
|
const API_CONFIG = {
|
|
// 开发环境 - 本地服务器
|
|
development: {
|
|
baseURL: 'http://115.190.167.176:20002',
|
|
timeout: 30000
|
|
},
|
|
// 生产环境 - 云服务器
|
|
production: {
|
|
baseURL: 'https://fhapp.ddn-ai.cloud',
|
|
timeout: 30000
|
|
}
|
|
};
|
|
|
|
// 当前环境(可以根据需要切换)
|
|
const ENV = 'production'; // 'development' | 'production'
|
|
|
|
// 导出当前环境的配置
|
|
export const API_BASE = API_CONFIG[ENV].baseURL;
|
|
export const API_TIMEOUT = API_CONFIG[ENV].timeout;
|
|
|
|
// 导出配置对象(供环境检测使用)
|
|
export { API_CONFIG };
|
|
|
|
/**
|
|
* API端点定义
|
|
*/
|
|
export const API_ENDPOINTS = {
|
|
// 复活视频相关
|
|
revival: {
|
|
// 获取视频列表
|
|
getList: '/api/revival/list',
|
|
// 上传照片生成视频
|
|
createVideo: '/api/revival/create',
|
|
// 删除视频
|
|
deleteVideo: (id) => `/api/revival/delete/${id}`,
|
|
// 获取视频详情
|
|
getDetail: (id) => `/api/revival/detail/${id}`
|
|
},
|
|
|
|
// 火山引擎视频生成
|
|
volcengine: {
|
|
// 图生视频
|
|
generateVideo: '/api/photo-revival/volcengine-video'
|
|
},
|
|
|
|
// 音色相关
|
|
voice: {
|
|
// 获取音色列表
|
|
getList: '/api/voice/list',
|
|
// 创建音色
|
|
create: '/api/voice/create',
|
|
// 删除音色
|
|
delete: (id) => `/api/voice/delete/${id}`
|
|
},
|
|
|
|
// 对话相关
|
|
conversation: {
|
|
// 发送对话
|
|
send: '/api/conversation/send',
|
|
// 获取音频文件
|
|
getAudio: (filename) => `/api/conversation/audio/${filename}`,
|
|
// 清空历史
|
|
clearHistory: '/api/conversation/clear-history'
|
|
},
|
|
|
|
// 静态资源
|
|
static: {
|
|
// 视频文件
|
|
video: (filename) => `/static/videos/${filename}`,
|
|
// 图片文件
|
|
image: (filename) => `/static/images/${filename}`
|
|
},
|
|
|
|
// 应用配置
|
|
config: {
|
|
// 获取应用配置
|
|
getAppConfig: '/api/config/app'
|
|
},
|
|
complaint: {
|
|
create: '/api/complaints'
|
|
},
|
|
works: {
|
|
plaza: '/api/works/plaza',
|
|
findBySource: '/api/works/source',
|
|
publishRevivalVideo: '/api/works/publish/revival-video',
|
|
unpublish: '/api/works/unpublish'
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 构建完整的API URL
|
|
* @param {string} endpoint - API端点
|
|
* @returns {string} 完整的URL
|
|
*/
|
|
export function buildURL(endpoint) {
|
|
if (endpoint.startsWith('http://') || endpoint.startsWith('https://')) {
|
|
return endpoint;
|
|
}
|
|
return `${API_BASE}${endpoint}`;
|
|
}
|
|
|
|
/**
|
|
* 通用请求方法
|
|
* @param {Object} options - 请求配置
|
|
* @returns {Promise}
|
|
*/
|
|
export function request(options) {
|
|
const {
|
|
url,
|
|
method = 'GET',
|
|
data = {},
|
|
header = {},
|
|
timeout = API_TIMEOUT
|
|
} = options;
|
|
|
|
// 获取用户ID与Token
|
|
const userId = uni.getStorageSync('userId') || '';
|
|
const token = uni.getStorageSync('token') || '';
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: buildURL(url),
|
|
method,
|
|
data,
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
'X-User-Id': userId || '',
|
|
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
|
...header
|
|
},
|
|
timeout,
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
resolve(res.data);
|
|
} else if (res.statusCode === 401) {
|
|
// 未授权,清理并跳转登录
|
|
uni.removeStorageSync('token');
|
|
uni.removeStorageSync('userId');
|
|
uni.removeStorageSync('userPhone');
|
|
uni.removeStorageSync('userNickname');
|
|
uni.removeStorageSync('username');
|
|
|
|
uni.showToast({
|
|
title: '登录已过期,请重新登录',
|
|
icon: 'none'
|
|
});
|
|
setTimeout(() => {
|
|
uni.reLaunch({
|
|
url: '/pages/login/login'
|
|
});
|
|
}, 1500);
|
|
reject(new Error('未授权'));
|
|
} else {
|
|
reject(new Error(`请求失败: ${res.statusCode}`));
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 上传文件
|
|
* @param {Object} options - 上传配置
|
|
* @returns {Promise}
|
|
*/
|
|
export function uploadFile(options) {
|
|
const {
|
|
url,
|
|
filePath,
|
|
name = 'file',
|
|
formData = {},
|
|
timeout = API_TIMEOUT
|
|
} = options;
|
|
|
|
// 获取用户ID与Token
|
|
const userId = uni.getStorageSync('userId') || '';
|
|
const token = uni.getStorageSync('token') || '';
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: buildURL(url),
|
|
filePath,
|
|
name,
|
|
formData: {
|
|
...formData,
|
|
userId: userId || ''
|
|
},
|
|
header: {
|
|
'X-User-Id': userId || '',
|
|
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
|
},
|
|
timeout,
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
try {
|
|
const data = JSON.parse(res.data);
|
|
resolve(data);
|
|
} catch (e) {
|
|
resolve(res.data);
|
|
}
|
|
} else if (res.statusCode === 401) {
|
|
// 未授权,清理并跳转登录
|
|
uni.removeStorageSync('token');
|
|
uni.removeStorageSync('userId');
|
|
uni.removeStorageSync('userInfo');
|
|
uni.removeStorageSync('userPhone');
|
|
uni.removeStorageSync('userNickname');
|
|
uni.removeStorageSync('username');
|
|
|
|
uni.showToast({
|
|
title: '登录已过期,请重新登录',
|
|
icon: 'none'
|
|
});
|
|
setTimeout(() => {
|
|
uni.reLaunch({
|
|
url: '/pages/login/login'
|
|
});
|
|
}, 1500);
|
|
reject(new Error('Token expired'));
|
|
} else {
|
|
reject(new Error(`上传失败: ${res.statusCode}`));
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 下载文件
|
|
* @param {Object} options - 下载配置
|
|
* @returns {Promise}
|
|
*/
|
|
export function downloadFile(options) {
|
|
const {
|
|
url,
|
|
timeout = API_TIMEOUT
|
|
} = options;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.downloadFile({
|
|
url: buildURL(url),
|
|
timeout,
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
resolve(res.tempFilePath);
|
|
} else {
|
|
reject(new Error(`下载失败: ${res.statusCode}`));
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// 默认导出
|
|
export default {
|
|
API_BASE,
|
|
API_TIMEOUT,
|
|
API_ENDPOINTS,
|
|
buildURL,
|
|
request,
|
|
uploadFile,
|
|
downloadFile
|
|
};
|