fix: 在 craco.config.js 中将 /bytedesk 代理移出 Mock 模式条件判断

现在 /bytedesk 代理始终启用,指向 https://valuefrontier.cn
This commit is contained in:
zdl
2025-11-21 18:06:21 +08:00
parent 34bae35858
commit 9669d5709e
2 changed files with 43 additions and 24 deletions

View File

@@ -284,9 +284,19 @@ module.exports = {
}, },
// 代理配置:将 /api 请求代理到后端服务器 // 代理配置:将 /api 请求代理到后端服务器
// 注意Mock 模式下禁用 proxy,让 MSW 拦截请求 // 注意Mock 模式下禁用 /api 和 /concept-api,让 MSW 拦截请求
...(isMockMode() ? {} : { // 但 /bytedesk 始终启用(客服系统不走 Mock
proxy: { proxy: {
'/bytedesk': {
target: 'https://valuefrontier.cn', // 统一使用生产环境 Nginx 代理
changeOrigin: true,
secure: false, // 开发环境禁用 HTTPS 严格验证
logLevel: 'debug',
ws: true, // 支持 WebSocket
// 不使用 pathRewrite保留 /bytedesk 前缀,让生产 Nginx 处理
},
// Mock 模式下禁用其他代理
...(isMockMode() ? {} : {
'/api': { '/api': {
target: 'http://49.232.185.254:5001', target: 'http://49.232.185.254:5001',
changeOrigin: true, changeOrigin: true,
@@ -300,15 +310,7 @@ module.exports = {
logLevel: 'debug', logLevel: 'debug',
pathRewrite: { '^/concept-api': '' }, pathRewrite: { '^/concept-api': '' },
}, },
'/bytedesk': { }),
target: 'https://valuefrontier.cn', // 统一使用生产环境 Nginx 代理 },
changeOrigin: true,
secure: false, // 开发环境禁用 HTTPS 严格验证
logLevel: 'debug',
ws: true, // 支持 WebSocket
// 不使用 pathRewrite保留 /bytedesk 前缀,让生产 Nginx 处理
},
},
}),
}, },
}; };

View File

@@ -78,26 +78,43 @@ const BytedeskWidget = ({
document.body.appendChild(script); document.body.appendChild(script);
scriptRef.current = script; scriptRef.current = script;
// 清理函数 // 清理函数 - 增强错误处理,防止 React 18 StrictMode 双重清理报错
return () => { return () => {
console.log('[Bytedesk] 清理Widget'); console.log('[Bytedesk] 清理Widget');
// 移除脚本 // 移除脚本
if (scriptRef.current && document.body.contains(scriptRef.current)) { try {
document.body.removeChild(scriptRef.current); if (scriptRef.current && scriptRef.current.parentNode) {
scriptRef.current.parentNode.removeChild(scriptRef.current);
}
scriptRef.current = null;
} catch (error) {
console.warn('[Bytedesk] 移除脚本失败(可能已被移除):', error.message);
} }
// 移除Widget DOM元素 // 移除Widget DOM元素
const widgetElements = document.querySelectorAll('[class*="bytedesk"], [id*="bytedesk"]'); try {
widgetElements.forEach(el => { const widgetElements = document.querySelectorAll('[class*="bytedesk"], [id*="bytedesk"]');
if (el && el.parentNode) { widgetElements.forEach(el => {
el.parentNode.removeChild(el); try {
} if (el && el.parentNode && el.parentNode.contains(el)) {
}); el.parentNode.removeChild(el);
}
} catch (err) {
// 忽略单个元素移除失败(可能已被移除)
}
});
} catch (error) {
console.warn('[Bytedesk] 清理Widget DOM元素失败:', error.message);
}
// 清理全局对象 // 清理全局对象
if (window.BytedeskWeb) { try {
delete window.BytedeskWeb; if (window.BytedeskWeb) {
delete window.BytedeskWeb;
}
} catch (error) {
console.warn('[Bytedesk] 清理全局对象失败:', error.message);
} }
}; };
}, [config, autoLoad, onLoad, onError]); }, [config, autoLoad, onLoad, onError]);