feat(client): 前端场景和HTTP工具优化
This commit is contained in:
@@ -5,30 +5,20 @@
|
||||
// ============================================
|
||||
// 环境配置(切换这里即可)
|
||||
// ============================================
|
||||
const ENV = 'local'; // 'local' = 本地后端, 'cloud' = 微信云托管
|
||||
const ENV = 'cloud'; // 'local' = 本地后端, 'cloud' = 微信云托管
|
||||
|
||||
const CONFIG = {
|
||||
local: {
|
||||
baseUrl: 'http://localhost:8001/api'
|
||||
baseUrl: 'http://localhost:8000/api',
|
||||
staticUrl: 'http://localhost:8000'
|
||||
},
|
||||
cloud: {
|
||||
env: 'prod-6gjx1rd4c40f5884',
|
||||
serviceName: 'express-fuvd'
|
||||
serviceName: 'express-fuvd',
|
||||
staticUrl: 'https://7072-prod-6gjx1rd4c40f5884-1409819450.tcb.qcloud.la'
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取存储的 Token
|
||||
*/
|
||||
function getToken() {
|
||||
try {
|
||||
const userInfo = wx.getStorageSync('userInfo');
|
||||
return userInfo?.token || '';
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送HTTP请求
|
||||
*/
|
||||
@@ -45,43 +35,21 @@ export function request(options) {
|
||||
*/
|
||||
function requestLocal(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 自动添加 Token 到请求头
|
||||
const token = getToken();
|
||||
const header = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.header
|
||||
};
|
||||
if (token) {
|
||||
header['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// 处理 URL 查询参数
|
||||
let url = CONFIG.local.baseUrl + options.url;
|
||||
if (options.params) {
|
||||
const queryString = Object.entries(options.params)
|
||||
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
||||
.join('&');
|
||||
url += (url.includes('?') ? '&' : '?') + queryString;
|
||||
}
|
||||
|
||||
wx.request({
|
||||
url,
|
||||
url: CONFIG.local.baseUrl + options.url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
timeout: options.timeout || 30000,
|
||||
header,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.header
|
||||
},
|
||||
success(res) {
|
||||
// 处理 401 未授权错误
|
||||
if (res.statusCode === 401) {
|
||||
wx.removeStorageSync('userInfo');
|
||||
reject(new Error('登录已过期,请重新登录'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.data && res.data.code === 0) {
|
||||
resolve(res.data.data);
|
||||
} else {
|
||||
reject(new Error(res.data?.message || '请求失败'));
|
||||
console.error('[HTTP-Local] 响应异常:', res.statusCode, res.data);
|
||||
reject(new Error(res.data?.message || res.data?.detail || `请求失败(${res.statusCode})`));
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
@@ -97,47 +65,26 @@ function requestLocal(options) {
|
||||
*/
|
||||
function requestCloud(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 自动添加 Token 到请求头
|
||||
const token = getToken();
|
||||
const header = {
|
||||
'X-WX-SERVICE': CONFIG.cloud.serviceName,
|
||||
'Content-Type': 'application/json',
|
||||
...options.header
|
||||
};
|
||||
if (token) {
|
||||
header['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// 处理 URL 查询参数
|
||||
let path = '/api' + options.url;
|
||||
if (options.params) {
|
||||
const queryString = Object.entries(options.params)
|
||||
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
||||
.join('&');
|
||||
path += (path.includes('?') ? '&' : '?') + queryString;
|
||||
}
|
||||
|
||||
wx.cloud.callContainer({
|
||||
config: {
|
||||
env: CONFIG.cloud.env
|
||||
},
|
||||
path,
|
||||
path: '/api' + options.url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header,
|
||||
header: {
|
||||
'X-WX-SERVICE': CONFIG.cloud.serviceName,
|
||||
'Content-Type': 'application/json',
|
||||
...options.header
|
||||
},
|
||||
success(res) {
|
||||
// 处理 401 未授权错误
|
||||
if (res.statusCode === 401) {
|
||||
wx.removeStorageSync('userInfo');
|
||||
reject(new Error('登录已过期,请重新登录'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.data && res.data.code === 0) {
|
||||
resolve(res.data.data);
|
||||
} else if (res.data) {
|
||||
reject(new Error(res.data.message || '请求失败'));
|
||||
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('响应数据异常'));
|
||||
}
|
||||
},
|
||||
@@ -152,8 +99,8 @@ function requestCloud(options) {
|
||||
/**
|
||||
* GET请求
|
||||
*/
|
||||
export function get(url, params) {
|
||||
return request({ url, method: 'GET', params });
|
||||
export function get(url, data) {
|
||||
return request({ url, method: 'GET', data });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,10 +118,63 @@ export function del(url, data) {
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
* 获取静态资源完整URL(图片等)
|
||||
* @param {string} path - 相对路径,如 /uploads/stories/1/characters/1.jpg
|
||||
* @returns {string} 完整URL
|
||||
*/
|
||||
export function put(url, data, options = {}) {
|
||||
return request({ url, method: 'PUT', data, ...options });
|
||||
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;
|
||||
}
|
||||
|
||||
export default { request, get, post, put, del };
|
||||
/**
|
||||
* 获取角色头像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 };
|
||||
|
||||
Reference in New Issue
Block a user