Compare commits

..

4 Commits

3 changed files with 80 additions and 39 deletions

View File

@@ -54,6 +54,6 @@ const localDevConfig = ({
uniacid: 115,
domain: 'https://5g.aigc-quickapp.com/',
},
})['115']; // 选择要使用的环境配置
})['1']; // 选择要使用的环境配置
export default localDevConfig;

View File

@@ -5,6 +5,17 @@ import store from '@/store/index.js'
let currentConversationId = null;
const CONVERSATION_KEY = 'ai_conversation_id';
/**
* 获取域名配置参数(避免重复代码)
*/
function getDomainParams() {
return {
imgDomain: Config.imgDomain,
h5Domain: Config.h5Domain,
baseUrl: Config.baseUrl
};
}
// 初始化时从本地读取会话ID
try {
const saved = uni.getStorageSync(CONVERSATION_KEY);
@@ -16,12 +27,28 @@ try {
}
export default {
/**
* 获取uniacid
* @returns {number} uniacid
*/
getUniacid() {
return uni.getStorageSync('uniacid') || Config.uniacid; // 从缓存中获取uniacid,或使用默认uniacid
},
/**
* 发送普通消息(blocking 模式)
*/
async sendMessage(message, options = {}) {
try {
// 调试:打印关键状态
console.log('[AI Service] sendMessage 开始执行');
console.log('[AI Service] store.state.memberInfo:', store.state.memberInfo);
console.log('[AI Service] uniacid:', this.getUniacid());
console.log('[AI Service] currentConversationId:', this.getConversationId());
const aiConfig = store.getters.globalAIKefuConfig;
console.log('[AI Service] aiConfig:', aiConfig);
const params = {
url: '/api/kefu/chat',
data: {
@@ -31,28 +58,38 @@ export default {
query: message,
response_mode: 'blocking', // 强制 blocking
user: store.state.memberInfo?.id || 'anonymous',
conversation_id: this.getConversationId() || ''
conversation_id: this.getConversationId() || '',
...getDomainParams()
},
header: {
'Content-Type': 'application/json'
}
};
console.log('Sending blocking request to:', params.url);
console.log('Request data:', params.data);
console.log('[AI Service] 发送请求到:', params.url);
console.log('[AI Service] 请求参数:', JSON.stringify(params.data, null, 2));
const response = await http.sendRequest({
...params,
async: false
});
console.log('[AI Service] 收到响应:', response);
const result = this.handleResponse(response);
if (result.conversationId) {
this.setConversationId(result.conversationId);
}
return result;
} catch (error) {
console.error('Dify API请求失败:', error);
console.error('[AI Service] Dify API请求失败,详细错误:', error);
console.error('[AI Service] 错误堆栈:', error.stack);
// 打印更多信息帮助调试
console.error('[AI Service] store 状态:', {
memberInfo: store.state.memberInfo,
uniacid: this.getUniacid(),
token: store.state.token
});
throw new Error('AI服务暂时不可用,请稍后重试');
}
},
@@ -81,7 +118,7 @@ export default {
socketTask.send({
data: JSON.stringify({
action: 'auth',
uniacid: store.state.uniacid || '1',
uniacid: this.getUniacid(),
token: store.state.token || 'test_token',
user_id: store.state.memberInfo?.id || 'anonymous'
})
@@ -99,10 +136,11 @@ export default {
socketTask.send({
data: JSON.stringify({
action: 'chat',
uniacid: store.state.uniacid || '1',
uniacid: this.getUniacid(),
query: message,
user_id: store.state.memberInfo?.id || 'anonymous',
conversation_id: this.getConversationId() || ''
conversation_id: this.getConversationId() || '',
...getDomainParams()
})
});
} else if (data.type === 'auth_failed') {
@@ -179,12 +217,14 @@ export default {
'Accept': 'text/event-stream',
},
body: JSON.stringify({
uniacid: store.state.uniacid || '1',
uniacid: this.getUniacid(),
token: store.state.token || 'test_token',
inputs: {},
query: message,
response_mode: 'streaming',
user_id: store.state.memberInfo?.id || 'anonymous',
conversation_id: this.getConversationId() || ''
conversation_id: this.getConversationId() || '',
...getDomainParams()
})
});
@@ -318,17 +358,19 @@ export default {
* 生成新会话(如需)
*/
async generateConversationId() {
const params = {
url: '/api/kefu/createConversation',
data: {
uniacid: store.state.uniacid,
user_id: store.state.memberInfo?.id || 'anonymous',
member_id: store.state.memberInfo?.id || '',
},
header: {
'Content-Type': 'application/json'
}
};
const params = {
url: '/api/kefu/createConversation',
data: {
uniacid: this.getUniacid(),
token: store.state.token || 'test_token',
user_id: store.state.memberInfo?.id || 'anonymous',
member_id: store.state.memberInfo?.id || '',
...getDomainParams()
},
header: {
'Content-Type': 'application/json'
}
};
const response = await http.sendRequest({
...params,
async: false
@@ -346,6 +388,7 @@ export default {
async: false,
data: {}
});
console.log('/api/kefu/health response', response);
const available = [response?.data?.status, response?.data?.components?.ai_service_config?.status]
.filter(item => item === 'healthy').length > 0;
return {
@@ -367,7 +410,10 @@ export default {
try {
const response = await http.sendRequest({
url: '/api/kefu/clear-conversation',
data: { conversation_id: conversationId },
data: {
conversation_id: conversationId,
...getDomainParams()
},
async: false
});
return response.success;
@@ -386,13 +432,14 @@ export default {
throw new Error('会话ID(conversation_id)是必填参数');
}
const requestData = {
uniacid: params.uniacid || store.state.uniacid || '1',
uniacid: params.uniacid || this.getUniacid(),
conversation_id: params.conversation_id,
user_id: params.user_id || store.state.memberInfo?.id || 'anonymous',
limit: params.limit || 20,
offset: params.offset || 0,
member_id: params.member_id || store.state.memberInfo?.id || '',
token: params.token || ''
token: params.token || '',
...getDomainParams()
};
const response = await http.sendRequest({
url: '/api/kefu/getHistory',
@@ -431,13 +478,15 @@ export default {
if (window.currentEventSource) {
window.currentEventSource.close();
}
const params = new URLSearchParams({
uniacid: store.state.uniacid || '1',
user_id: store.state.memberInfo?.id || '123456',
const paramsObj = {
uniacid: this.getUniacid(),
user_id: store.state.memberInfo?.id,
query: message,
conversation_id: conversationId || '',
stream: 'true'
});
stream: 'true',
...getDomainParams()
};
const params = new URLSearchParams(paramsObj);
const url = `${Config.baseUrl}/api/kefu/chat?${params.toString()}`;
try {
const eventSource = new EventSource(url);

View File

@@ -214,7 +214,7 @@
<!-- 扩大按钮 -->
<button v-if="inputLineCount > 3" class="expand-btn" @click="openExpandEditor">
<text class="iconfont icon-expand">🔍</text>
<text class="iconfont icon-expand">[ ]</text>
</button>
<!-- Emoji按钮 -->
@@ -1146,11 +1146,8 @@ async loadChatHistoryIfExist() {
// 格式化时间
formatTime(timestamp) {
console.log('【调试】formatTime 接收到的时间戳:', timestamp, typeof timestamp);
// 如果没有时间戳,就不显示时间
if (!timestamp) {
console.warn('【调试】没有时间戳');
return '';
}
@@ -1183,21 +1180,15 @@ async loadChatHistoryIfExist() {
date = new Date(timeValue);
}
console.log('【调试】最终 Date 对象:', date);
console.log('【调试】本地时间:', date.toLocaleString());
console.log('【调试】小时:', date.getHours());
console.log('【调试】分钟:', date.getMinutes());
// 如果时间无效,就不显示时间
if (isNaN(date.getTime())) {
console.warn('【调试】时间无效:', timestamp);
return '';
}
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const result = hours + ':' + minutes;
console.log('【调试】最终显示时间:', result);
return result;
},
@@ -3674,6 +3665,7 @@ $radius-lg: 36rpx;
border: none;
background-color: #f2f2f2;
padding: 0;
left:60rpx;
.iconfont {
font-size: 36rpx;