181 lines
4.8 KiB
JavaScript
181 lines
4.8 KiB
JavaScript
/**
|
||
* 网络请求工具 - 支持本地/云托管切换
|
||
*/
|
||
|
||
// ============================================
|
||
// 环境配置(切换这里即可)
|
||
// ============================================
|
||
const ENV = 'cloud'; // 'local' = 本地后端, 'cloud' = 微信云托管
|
||
|
||
const CONFIG = {
|
||
local: {
|
||
baseUrl: 'http://localhost:8000/api',
|
||
staticUrl: 'http://localhost:8000'
|
||
},
|
||
cloud: {
|
||
env: 'prod-6gjx1rd4c40f5884',
|
||
serviceName: 'express-fuvd',
|
||
staticUrl: 'https://7072-prod-6gjx1rd4c40f5884-1409819450.tcb.qcloud.la'
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 发送HTTP请求
|
||
*/
|
||
export function request(options) {
|
||
if (ENV === 'local') {
|
||
return requestLocal(options);
|
||
} else {
|
||
return requestCloud(options);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 本地后端请求(wx.request)
|
||
*/
|
||
function requestLocal(options) {
|
||
return new Promise((resolve, reject) => {
|
||
wx.request({
|
||
url: CONFIG.local.baseUrl + options.url,
|
||
method: options.method || 'GET',
|
||
data: options.data || {},
|
||
timeout: options.timeout || 30000,
|
||
header: {
|
||
'Content-Type': 'application/json',
|
||
...options.header
|
||
},
|
||
success(res) {
|
||
if (res.data && res.data.code === 0) {
|
||
resolve(res.data.data);
|
||
} else {
|
||
console.error('[HTTP-Local] 响应异常:', res.statusCode, res.data);
|
||
reject(new Error(res.data?.message || res.data?.detail || `请求失败(${res.statusCode})`));
|
||
}
|
||
},
|
||
fail(err) {
|
||
console.error('[HTTP-Local] 请求失败:', err);
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 云托管请求(wx.cloud.callContainer)
|
||
*/
|
||
function requestCloud(options) {
|
||
return new Promise((resolve, reject) => {
|
||
wx.cloud.callContainer({
|
||
config: {
|
||
env: CONFIG.cloud.env
|
||
},
|
||
path: '/api' + options.url,
|
||
method: options.method || 'GET',
|
||
data: options.data || {},
|
||
header: {
|
||
'X-WX-SERVICE': CONFIG.cloud.serviceName,
|
||
'Content-Type': 'application/json',
|
||
...options.header
|
||
},
|
||
success(res) {
|
||
if (res.data && res.data.code === 0) {
|
||
resolve(res.data.data);
|
||
} else if (res.data) {
|
||
console.error('[HTTP-Cloud] 响应异常:', res.statusCode, res.data);
|
||
reject(new Error(res.data.message || res.data.detail || `请求失败(${res.statusCode})`));
|
||
} else {
|
||
console.error('[HTTP-Cloud] 响应数据异常:', res);
|
||
reject(new Error('响应数据异常'));
|
||
}
|
||
},
|
||
fail(err) {
|
||
console.error('[HTTP-Cloud] 请求失败:', err);
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* GET请求
|
||
*/
|
||
export function get(url, data) {
|
||
return request({ url, method: 'GET', data });
|
||
}
|
||
|
||
/**
|
||
* POST请求
|
||
*/
|
||
export function post(url, data, options = {}) {
|
||
return request({ url, method: 'POST', data, ...options });
|
||
}
|
||
|
||
/**
|
||
* DELETE请求
|
||
*/
|
||
export function del(url, data) {
|
||
return request({ url, method: 'DELETE', data });
|
||
}
|
||
|
||
/**
|
||
* 获取静态资源完整URL(图片等)
|
||
* @param {string} path - 相对路径,如 /uploads/stories/1/characters/1.jpg
|
||
* @returns {string} 完整URL
|
||
*/
|
||
export function getStaticUrl(path) {
|
||
if (!path) return '';
|
||
// 如果已经是完整URL,直接返回
|
||
if (path.startsWith('http://') || path.startsWith('https://')) {
|
||
return path;
|
||
}
|
||
const config = ENV === 'local' ? CONFIG.local : CONFIG.cloud;
|
||
return config.staticUrl + path;
|
||
}
|
||
|
||
/**
|
||
* 获取角色头像URL
|
||
* @param {number} storyId - 故事ID
|
||
* @param {number} characterId - 角色ID
|
||
*/
|
||
export function getCharacterAvatar(storyId, characterId) {
|
||
return getStaticUrl(`/uploads/stories/${storyId}/characters/${characterId}.jpg`);
|
||
}
|
||
|
||
/**
|
||
* 获取故事封面URL
|
||
* @param {number} storyId - 故事ID
|
||
*/
|
||
export function getStoryCover(storyId) {
|
||
return getStaticUrl(`/uploads/stories/${storyId}/cover/cover.jpg`);
|
||
}
|
||
|
||
/**
|
||
* 获取节点背景图URL
|
||
* @param {number} storyId - 故事ID
|
||
* @param {string} nodeKey - 节点key
|
||
*/
|
||
export function getNodeBackground(storyId, nodeKey) {
|
||
return getStaticUrl(`/uploads/stories/${storyId}/nodes/${nodeKey}/background.jpg`);
|
||
}
|
||
|
||
/**
|
||
* 获取节点角色立绘URL
|
||
* @param {number} storyId - 故事ID
|
||
* @param {string} nodeKey - 节点key
|
||
*/
|
||
export function getNodeCharacter(storyId, nodeKey) {
|
||
return getStaticUrl(`/uploads/stories/${storyId}/nodes/${nodeKey}/character.jpg`);
|
||
}
|
||
|
||
/**
|
||
* 获取草稿节点背景图URL
|
||
* @param {number} storyId - 故事ID
|
||
* @param {number} draftId - 草稿ID
|
||
* @param {string} nodeKey - 节点key
|
||
*/
|
||
export function getDraftNodeBackground(storyId, draftId, nodeKey) {
|
||
return getStaticUrl(`/uploads/stories/${storyId}/drafts/${draftId}/${nodeKey}/background.jpg`);
|
||
}
|
||
|
||
export default { request, get, post, del, getStaticUrl, getCharacterAvatar, getStoryCover, getNodeBackground, getNodeCharacter, getDraftNodeBackground };
|