feat: 环境的配置
This commit is contained in:
@@ -3,11 +3,5 @@ NODE_ENV = development
|
|||||||
# 测试
|
# 测试
|
||||||
VITE_BASE_URL = https://onefeel.brother7.cn/ingress
|
VITE_BASE_URL = https://onefeel.brother7.cn/ingress
|
||||||
|
|
||||||
# 生产
|
|
||||||
# VITE_BASE_URL = https://biz.nianxx.cn
|
|
||||||
|
|
||||||
# 测试
|
# 测试
|
||||||
VITE_WSS_URL = wss://onefeel.brother7.cn/ingress/agent/ws/chat
|
VITE_WSS_URL = wss://onefeel.brother7.cn/ingress/agent/ws/chat
|
||||||
|
|
||||||
# 生产
|
|
||||||
# VITE_WSS_URL = wss://biz.nianxx.cn/agent/ws/chat
|
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
NODE_ENV = production
|
NODE_ENV = production
|
||||||
|
|
||||||
# 测试
|
# 生产
|
||||||
VITE_BASE_URL = https://onefeel.brother7.cn/ingress
|
VITE_BASE_URL = https://biz.nianxx.cn
|
||||||
|
|
||||||
# 生产
|
# 生产
|
||||||
# VITE_BASE_URL = https://biz.nianxx.cn
|
VITE_WSS_URL = wss://biz.nianxx.cn/agent/ws/chat
|
||||||
|
|
||||||
# 测试
|
|
||||||
VITE_WSS_URL = wss://onefeel.brother7.cn/ingress/agent/ws/chat
|
|
||||||
|
|
||||||
# 生产
|
|
||||||
# VITE_WSS_URL = wss://biz.nianxx.cn/agent/ws/chat
|
|
||||||
12
.env.staging
12
.env.staging
@@ -1,13 +1,7 @@
|
|||||||
NODE_ENV = staging
|
NODE_ENV = staging
|
||||||
|
|
||||||
# 测试
|
# 生产
|
||||||
VITE_BASE_URL = https://onefeel.brother7.cn/ingress
|
VITE_BASE_URL = https://biz.nianxx.cn
|
||||||
|
|
||||||
# 生产
|
# 生产
|
||||||
# VITE_BASE_URL = https://biz.nianxx.cn
|
VITE_WSS_URL = wss://biz.nianxx.cn/agent/ws/chat
|
||||||
|
|
||||||
# 测试
|
|
||||||
VITE_WSS_URL = wss://onefeel.brother7.cn/ingress/agent/ws/chat
|
|
||||||
|
|
||||||
# 生产
|
|
||||||
# VITE_WSS_URL = wss://biz.nianxx.cn/agent/ws/chat
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
const isProd = true;
|
|
||||||
|
|
||||||
console.log(import.meta.env);
|
|
||||||
|
|
||||||
export const BASE_URL = import.meta.env.VITE_BASE_URL;
|
|
||||||
|
|
||||||
// socket地址
|
|
||||||
export const WSS_URL = import.meta.env.VITE_WSS_URL;
|
|
||||||
|
|||||||
@@ -55,7 +55,10 @@
|
|||||||
},
|
},
|
||||||
/* 快应用特有相关 */
|
/* 快应用特有相关 */
|
||||||
"quickapp": {},
|
"quickapp": {},
|
||||||
/* 小程序特有相关 朵花用:wx23f86d809ae80259 测试用:wx5e79df5996572539 */
|
/* 小程序特有相关 同时更改 project.config.json文件
|
||||||
|
智念:wx5e79df5996572539
|
||||||
|
朵花:wx23f86d809ae80259
|
||||||
|
*/
|
||||||
"mp-weixin": {
|
"mp-weixin": {
|
||||||
"appid": "wx5e79df5996572539",
|
"appid": "wx5e79df5996572539",
|
||||||
"setting": {
|
"setting": {
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ import {
|
|||||||
RECOMMEND_POSTS_TITLE,
|
RECOMMEND_POSTS_TITLE,
|
||||||
SEND_COMMAND_TEXT,
|
SEND_COMMAND_TEXT,
|
||||||
} from "@/constant/constant";
|
} from "@/constant/constant";
|
||||||
import { WSS_URL } from "@/constant/base";
|
import { WSS_URL } from "@/request/base/baseUrl";
|
||||||
import { MessageRole, MessageType, CompName } from "../../model/ChatModel";
|
import { MessageRole, MessageType, CompName } from "../../model/ChatModel";
|
||||||
import ChatTopWelcome from "./ChatTopWelcome.vue";
|
import ChatTopWelcome from "./ChatTopWelcome.vue";
|
||||||
import ChatTopBgImg from "./ChatTopBgImg.vue";
|
import ChatTopBgImg from "./ChatTopBgImg.vue";
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { BASE_URL } from "../../constant/base";
|
import { BASE_URL } from "@/request/base/baseUrl";
|
||||||
import { goLogin } from "@/hooks/useGoLogin";
|
import { goLogin } from "@/hooks/useGoLogin";
|
||||||
|
|
||||||
/// 请求流式数据的API
|
/// 请求流式数据的API
|
||||||
const API = '/agent/assistant/chat';
|
const API = "/agent/assistant/chat";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取AI聊天流式信息(仅微信小程序支持)
|
* 获取AI聊天流式信息(仅微信小程序支持)
|
||||||
@@ -31,7 +31,7 @@ const stopAbortTask = () => {
|
|||||||
activeRequestId = null;
|
activeRequestId = null;
|
||||||
|
|
||||||
if (currentPromiseReject) {
|
if (currentPromiseReject) {
|
||||||
currentPromiseReject(new Error('请求已被用户终止'));
|
currentPromiseReject(new Error("请求已被用户终止"));
|
||||||
currentPromiseReject = null;
|
currentPromiseReject = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,15 +40,15 @@ const stopAbortTask = () => {
|
|||||||
const cleanupListeners = () => {
|
const cleanupListeners = () => {
|
||||||
try {
|
try {
|
||||||
if (requestTask.offChunkReceived) {
|
if (requestTask.offChunkReceived) {
|
||||||
console.log("======>offChunkReceived")
|
console.log("======>offChunkReceived");
|
||||||
requestTask.offChunkReceived();
|
requestTask.offChunkReceived();
|
||||||
}
|
}
|
||||||
if (requestTask.offHeadersReceived) {
|
if (requestTask.offHeadersReceived) {
|
||||||
console.log("======>offHeadersReceived")
|
console.log("======>offHeadersReceived");
|
||||||
requestTask.offHeadersReceived();
|
requestTask.offHeadersReceived();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('清理事件监听器失败:', e);
|
console.error("清理事件监听器失败:", e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -57,22 +57,22 @@ const stopAbortTask = () => {
|
|||||||
// 终止请求
|
// 终止请求
|
||||||
try {
|
try {
|
||||||
if (requestTask.abort) {
|
if (requestTask.abort) {
|
||||||
console.log("======>abort")
|
console.log("======>abort");
|
||||||
requestTask.abort();
|
requestTask.abort();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('🛑 终止网络请求失败:', e);
|
console.log("🛑 终止网络请求失败:", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
requestTask = null;
|
requestTask = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('🛑 请求强制终止完成');
|
console.log("🛑 请求强制终止完成");
|
||||||
}
|
};
|
||||||
|
|
||||||
const agentChatStream = (params, onChunk) => {
|
const agentChatStream = (params, onChunk) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const token = uni.getStorageSync('token');
|
const token = uni.getStorageSync("token");
|
||||||
const requestId = Date.now().toString(); // 生成唯一请求ID
|
const requestId = Date.now().toString(); // 生成唯一请求ID
|
||||||
|
|
||||||
// 重置状态
|
// 重置状态
|
||||||
@@ -87,21 +87,23 @@ const agentChatStream = (params, onChunk) => {
|
|||||||
|
|
||||||
// 检查数据块是否来自已终止的请求
|
// 检查数据块是否来自已终止的请求
|
||||||
const isStaleData = (dataRequestId) => {
|
const isStaleData = (dataRequestId) => {
|
||||||
return dataRequestId === lastRequestId || dataRequestId !== activeRequestId;
|
return (
|
||||||
|
dataRequestId === lastRequestId || dataRequestId !== activeRequestId
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// #ifdef MP-WEIXIN
|
// #ifdef MP-WEIXIN
|
||||||
requestTask = uni.request({
|
requestTask = uni.request({
|
||||||
url: BASE_URL + API, // 替换为你的接口地址
|
url: BASE_URL + API, // 替换为你的接口地址
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
data: params,
|
data: params,
|
||||||
enableChunked: true,
|
enableChunked: true,
|
||||||
header: {
|
header: {
|
||||||
Accept: 'text/event-stream',
|
Accept: "text/event-stream",
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${token}`, // 如需token可加
|
Authorization: `Bearer ${token}`, // 如需token可加
|
||||||
},
|
},
|
||||||
responseType: 'arraybuffer',
|
responseType: "arraybuffer",
|
||||||
success(res) {
|
success(res) {
|
||||||
if (!isAborted && !isStaleData(requestId)) {
|
if (!isAborted && !isStaleData(requestId)) {
|
||||||
console.log(`✅ 请求 [${requestId}] 成功`);
|
console.log(`✅ 请求 [${requestId}] 成功`);
|
||||||
@@ -122,30 +124,39 @@ const agentChatStream = (params, onChunk) => {
|
|||||||
// 使用 requestId 来验证请求有效性
|
// 使用 requestId 来验证请求有效性
|
||||||
if (!isAborted && activeRequestId === requestId) {
|
if (!isAborted && activeRequestId === requestId) {
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode !== 200) {
|
||||||
console.log(`❌ 请求 [${requestId}] 完成但状态错误,状态:`, res.statusCode);
|
console.log(
|
||||||
|
`❌ 请求 [${requestId}] 完成但状态错误,状态:`,
|
||||||
|
res.statusCode
|
||||||
|
);
|
||||||
if (res.statusCode === 424) {
|
if (res.statusCode === 424) {
|
||||||
uni.setStorageSync('token', '');
|
uni.setStorageSync("token", "");
|
||||||
goLogin();
|
goLogin();
|
||||||
}
|
}
|
||||||
if (onChunk) {
|
if (onChunk) {
|
||||||
onChunk({
|
onChunk({
|
||||||
error: true,
|
error: true,
|
||||||
message: '服务器错误',
|
message: "服务器错误",
|
||||||
detail: res
|
detail: res,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
reject(res);
|
reject(res);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`❌ 请求 [${requestId}] ${isAborted ? '已终止' : '已过期'},忽略complete回调`);
|
console.log(
|
||||||
}
|
`❌ 请求 [${requestId}] ${
|
||||||
|
isAborted ? "已终止" : "已过期"
|
||||||
|
},忽略complete回调`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
requestTask.onHeadersReceived(res => {
|
requestTask.onHeadersReceived((res) => {
|
||||||
// 使用 requestId 验证请求有效性
|
// 使用 requestId 验证请求有效性
|
||||||
if (isAborted || activeRequestId !== requestId) {
|
if (isAborted || activeRequestId !== requestId) {
|
||||||
console.log(`🚫 Headers [${requestId}] ${isAborted ? '已终止' : '已过期'},忽略`);
|
console.log(
|
||||||
|
`🚫 Headers [${requestId}] ${isAborted ? "已终止" : "已过期"},忽略`
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +169,7 @@ const agentChatStream = (params, onChunk) => {
|
|||||||
onChunk({
|
onChunk({
|
||||||
error: true,
|
error: true,
|
||||||
message: `服务器错误(${status})`,
|
message: `服务器错误(${status})`,
|
||||||
detail: res
|
detail: res,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// 终止异常请求
|
// 终止异常请求
|
||||||
@@ -168,7 +179,7 @@ const agentChatStream = (params, onChunk) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
requestTask.onChunkReceived(res => {
|
requestTask.onChunkReceived((res) => {
|
||||||
// 立即验证请求有效性
|
// 立即验证请求有效性
|
||||||
if (isAborted || isStaleData(requestId)) {
|
if (isAborted || isStaleData(requestId)) {
|
||||||
console.log(`🚫 数据块 [${requestId}] 已终止或过期,忽略`);
|
console.log(`🚫 数据块 [${requestId}] 已终止或过期,忽略`);
|
||||||
@@ -176,15 +187,15 @@ const agentChatStream = (params, onChunk) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const base64 = uni.arrayBufferToBase64(res.data);
|
const base64 = uni.arrayBufferToBase64(res.data);
|
||||||
let data = '';
|
let data = "";
|
||||||
try {
|
try {
|
||||||
data = decodeURIComponent(escape(weAtob(base64)));
|
data = decodeURIComponent(escape(weAtob(base64)));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Base64解码失败:', e);
|
console.error("Base64解码失败:", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("📦 onChunkReceived,res:", data)
|
console.log("📦 onChunkReceived,res:", data);
|
||||||
|
|
||||||
// 再次验证请求有效性
|
// 再次验证请求有效性
|
||||||
if (isAborted || activeRequestId !== requestId) {
|
if (isAborted || activeRequestId !== requestId) {
|
||||||
@@ -193,7 +204,7 @@ const agentChatStream = (params, onChunk) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const messages = parseSSEChunk(data);
|
const messages = parseSSEChunk(data);
|
||||||
messages.forEach(msg => {
|
messages.forEach((msg) => {
|
||||||
if (!isAborted && !isStaleData(requestId) && onChunk) {
|
if (!isAborted && !isStaleData(requestId) && onChunk) {
|
||||||
onChunk(msg);
|
onChunk(msg);
|
||||||
}
|
}
|
||||||
@@ -201,14 +212,16 @@ const agentChatStream = (params, onChunk) => {
|
|||||||
});
|
});
|
||||||
// #endif
|
// #endif
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// window.atob兼容性处理
|
// window.atob兼容性处理
|
||||||
const weAtob = (string) => {
|
const weAtob = (string) => {
|
||||||
const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
|
const b64re =
|
||||||
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
/^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
|
||||||
|
const b64 =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||||
// 去除空白字符
|
// 去除空白字符
|
||||||
string = String(string).replace(/[\t\n\f\r ]+/g, '');
|
string = String(string).replace(/[\t\n\f\r ]+/g, "");
|
||||||
// 验证 Base64 编码
|
// 验证 Base64 编码
|
||||||
if (!b64re.test(string)) {
|
if (!b64re.test(string)) {
|
||||||
throw new TypeError(
|
throw new TypeError(
|
||||||
@@ -218,10 +231,10 @@ const weAtob = (string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 填充字符
|
// 填充字符
|
||||||
string += '=='.slice(2 - (string.length & 3));
|
string += "==".slice(2 - (string.length & 3));
|
||||||
|
|
||||||
let bitmap,
|
let bitmap,
|
||||||
result = '',
|
result = "",
|
||||||
r1,
|
r1,
|
||||||
r2,
|
r2,
|
||||||
i = 0;
|
i = 0;
|
||||||
@@ -236,10 +249,7 @@ const weAtob = (string) => {
|
|||||||
if (r1 === 64) {
|
if (r1 === 64) {
|
||||||
result += String.fromCharCode((bitmap >> 16) & 255);
|
result += String.fromCharCode((bitmap >> 16) & 255);
|
||||||
} else if (r2 === 64) {
|
} else if (r2 === 64) {
|
||||||
result += String.fromCharCode(
|
result += String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255);
|
||||||
(bitmap >> 16) & 255,
|
|
||||||
(bitmap >> 8) & 255
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
result += String.fromCharCode(
|
result += String.fromCharCode(
|
||||||
(bitmap >> 16) & 255,
|
(bitmap >> 16) & 255,
|
||||||
@@ -251,7 +261,6 @@ const weAtob = (string) => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 解析SSE分段数据
|
// 解析SSE分段数据
|
||||||
const parseSSEChunk = (raw) => {
|
const parseSSEChunk = (raw) => {
|
||||||
const results = [];
|
const results = [];
|
||||||
@@ -264,7 +273,7 @@ const parseSSEChunk = (raw) => {
|
|||||||
let dataLines = [];
|
let dataLines = [];
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (line.startsWith('data:')) {
|
if (line.startsWith("data:")) {
|
||||||
// 提取data:后面的内容并去除首尾空格
|
// 提取data:后面的内容并去除首尾空格
|
||||||
dataLines.push(line.slice(5).trim());
|
dataLines.push(line.slice(5).trim());
|
||||||
}
|
}
|
||||||
@@ -272,18 +281,18 @@ const parseSSEChunk = (raw) => {
|
|||||||
|
|
||||||
if (dataLines.length > 0) {
|
if (dataLines.length > 0) {
|
||||||
// 拼接多行数据
|
// 拼接多行数据
|
||||||
const fullData = dataLines.join('\n');
|
const fullData = dataLines.join("\n");
|
||||||
try {
|
try {
|
||||||
const obj = JSON.parse(fullData);
|
const obj = JSON.parse(fullData);
|
||||||
results.push(obj);
|
results.push(obj);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('⚠️ SSE数据解析失败:', e, '原始数据:', fullData);
|
console.warn("⚠️ SSE数据解析失败:", e, "原始数据:", fullData);
|
||||||
// 解析失败忽略
|
// 解析失败忽略
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
};
|
||||||
|
|
||||||
export { agentChatStream, stopAbortTask }
|
export { agentChatStream, stopAbortTask };
|
||||||
|
|||||||
19
request/base/baseUrl.js
Normal file
19
request/base/baseUrl.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
const isProd = false;
|
||||||
|
|
||||||
|
// 测试
|
||||||
|
const VITE_BASE_URL_TEST = "https://onefeel.brother7.cn/ingress";
|
||||||
|
const VITE_WSS_URL_TEST = "wss://onefeel.brother7.cn/ingress/agent/ws/chat";
|
||||||
|
|
||||||
|
// 生产
|
||||||
|
const VITE_BASE_URL_PRO = "https://biz.nianxx.cn";
|
||||||
|
const VITE_WSS_URL_PRO = "wss://biz.nianxx.cn/agent/ws/chat";
|
||||||
|
|
||||||
|
// 环境配置
|
||||||
|
export const BASE_URL = isProd ? VITE_BASE_URL_PRO : VITE_BASE_URL_TEST;
|
||||||
|
export const WSS_URL = isProd ? VITE_WSS_URL_PRO : VITE_WSS_URL_TEST;
|
||||||
|
|
||||||
|
// =====================================
|
||||||
|
// 环境配置文件使用方法
|
||||||
|
// console.log("当前环境:", import.meta.env);
|
||||||
|
// export const BASE_URL = import.meta.env.VITE_BASE_URL;
|
||||||
|
// export const WSS_URL = import.meta.env.VITE_WSS_URL;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { BASE_URL } from "../../constant/base";
|
import { BASE_URL } from "./baseUrl";
|
||||||
import { goLogin } from "@/hooks/useGoLogin";
|
import { goLogin } from "@/hooks/useGoLogin";
|
||||||
|
|
||||||
const defaultConfig = {
|
const defaultConfig = {
|
||||||
|
|||||||
Reference in New Issue
Block a user