feat: 添加产品助手对话功能

This commit is contained in:
huangzhe
2025-07-29 10:26:24 +08:00
parent dced4d6ca5
commit b873e378d2
12 changed files with 225 additions and 198 deletions

View File

@@ -1,81 +0,0 @@
<template>
<div class="hot-products">
<h2>热销产品</h2>
<ul>
<li v-for="(product, index) in hotProducts" :key="index">
<div class="product-card">
<img :src="product.image" :alt="product.name" />
<h3>{{ product.name }}</h3>
<p>{{ product.description }}</p>
<button>查看详情</button>
</div>
</li>
</ul>
</div>
</template>
<script>
</script>
<style scoped lang="scss">
.hot-products {
background: #fff;
padding: 10px;
border-radius: 5px;
h2 {
color: #2E5CA9;
font-size: 24px;
margin-bottom: 20px;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
gap: 20px;
overflow-x: auto; /* 添加横向滚动 */
white-space: nowrap; /* 防止换行 */
}
.product-card {
width: 200px;
border: 1px solid #ddd;
border-radius: 8px;
padding: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
img {
width: 100%;
height: auto;
border-radius: 8px;
}
h3 {
color: #333;
font-size: 18px;
margin: 10px 0;
}
p {
color: #666;
font-size: 14px;
margin-bottom: 15px;
}
button {
background-color: #2E5CA9;
color: #fff;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
&:hover {
background-color: #1E4082;
}
}
}
}
</style>

View File

@@ -37,6 +37,14 @@ export function chat(data) {
return getUrl('/hasl/chat')
}
/**
* 2025 7 28 国华接口
* @returns
*/
export function gwcsChat() {
return getUrl('/chat/message')
}
export function audioToText(data) {
//聊天获取产品百宝箱
// 聊天框输入内容以“百宝箱”结尾时,调用这个接口

31
src/assets/sass/card.css Normal file
View File

@@ -0,0 +1,31 @@
.card {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
}
.card > div {
border-radius: 10px;
overflow: hidden;
}
.card > div > img {
width: 80px;
height: 80px;
}
.card > section > :nth-child(1) {
font-weight: bold;
}
.card > section > :nth-child(2) {
display: flex;
gap: 5px;
color: white;
margin-top: 10px;
}
.card > section > :nth-child(2) > div {
background-color: #0065ff;
padding: 5px;
}

View File

@@ -1,10 +1,9 @@
<template>
<div class="hot-products">
<div class="hot-products" :class="[!messagesList.length ? 'show' : 'hide']">
<h4>你可以这样问我</h4>
<ul>
<li v-for="(product, index) in commendList" :key="index" :title="product.title"
@click="cellClick(product)">
<li v-for="(product, index) in commendList" :key="index" :title="product.title" @click="cellClick(product)">
<a href="javascript:void(0)">{{ product.title }}</a>
</li>
</ul>
@@ -41,6 +40,11 @@ export default {
action: () => {
console.log('普通办公室职员适合投保的产品有哪些?');
}
}, {
title: "年龄: 26岁, 性别: 男, 主要理财目标: 养老保障",
action: () => {
console.log('年龄: 26岁, 性别: 男, 主要理财目标: 养老保障');
}
}],
showAll: false, // 控制是否展示全部数据
}
@@ -79,6 +83,14 @@ export default {
padding: 10px;
border-radius: 5px;
overflow: scroll;
transition: all .3s ease-in-out;
&.hide {
opacity: 0;
}
&.show {
opacity: 1;
}
h4 {
color: grey;

View File

@@ -1,62 +1,62 @@
<template>
<div>
<div class="new-session">
<button @click="startNewConversation" class="active">
<svg-icon icon-class="add" class-name="chat-icon" style="color: #000;"></svg-icon>
开启新会话
</button>
</div>
<div v-if="isVoiceMode && newMessage" class="isVoiceModeText">
<textarea class="textarea" placeholder="请输入内容" v-model="newMessage"></textarea>
</div>
<footer class="chat-footer">
<!-- 输入框 or 按住说话提示 -->
<div class="input-wrapper ml10">
<input v-if="!isVoiceMode" type="text" v-model="newMessage" placeholder="请简短描述您的问题"
@keyup.enter="sendMessage" />
<div v-else class="voice-hint-container" :class="{ disabled: messageStatus === 'send' }"
@mousedown="startRecording" @selectstart="() => false" @mouseup="stopRecording" @mouseleave="stopRecording"
@touchend="stopRecording" @touchstart="startRecording">
<div class="waveform" :class="{ active: isRecording }">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
<!-- <div class="hint-text" v-if='!isRecording'>按住说话</div>-->
</div>
<div class="chat-container">
<div class="mb15">
<div v-if="isVoiceMode && newMessage" class="isVoiceModeText">
<textarea class="textarea" placeholder="请输入内容" v-model="newMessage"></textarea>
</div>
<!-- 语音按钮按住说话 -->
<button @click="isVoiceMode = !isVoiceMode" class="mic-button ml10 mr10">
<!-- <svg-icon v-if="!isVoiceMode" icon-class="voice" class-name="chat-icon ml10 wh25"></svg-icon>
<div class="new-session ">
<button @click="startNewConversation">
<svg-icon icon-class="add" class-name="chat-icon" style="color: #000;"></svg-icon>
开启新会话
</button>
</div>
<footer class="chat-footer pt10 pb10">
<!-- 输入框 or 按住说话提示 -->
<div class="input-wrapper">
<input v-if="!isVoiceMode" type="text" v-model="newMessage" placeholder="你有什么想知道的?"
@keyup.enter="sendMessage" />
<div v-else class="voice-hint-container" :class="{ disabled: messageStatus === 'send' }"
@mousedown="startRecording" @selectstart="() => false" @mouseup="stopRecording" @mouseleave="stopRecording"
@touchend="stopRecording" @touchstart="startRecording">
<div class="waveform" :class="{ active: isRecording }">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
<!-- <div class="hint-text" v-if='!isRecording'>按住说话</div>-->
</div>
<!-- 发送按钮 -->
<button @click="sendMessage" :disabled="messageStatus === 'send'"
:class="{ disabled: messageStatus === 'send' }" class="active send">
发送
</button>
</div>
<!-- 语音按钮按住说话 -->
<button @click="isVoiceMode = !isVoiceMode" class="mic-button ml10 mr10" v-if="false">
<!-- <svg-icon v-if="!isVoiceMode" icon-class="voice" class-name="chat-icon ml10 wh25"></svg-icon>
<span v-else class="ml15 mr5 input-icon"></span> -->
</button>
</button>
</footer>
<!-- <section class="section pb10" > -->
<!-- <button @click="deepInternet" :class="{ active: isDeep }">
<svg-icon icon-class="think" class-name="chat-icon"></svg-icon>
深度思考
</button> -->
<!-- 发送按钮 -->
<button @click="sendMessage" :disabled="messageStatus === 'send'" :class="{ disabled: messageStatus === 'send' }"
class="mr10 fs16">发送</button>
</footer>
<section class="section pb10">
<!-- <button @click="searchInternet" :class="{ active: isSearching }" class="ml10">
<svg-icon icon-class="earth" class-name="chat-icon"></svg-icon>
联网搜索
</button> -->
<button @click="deepInternet" :class="{ active: isDeep }">
<svg-icon icon-class="think" class-name="chat-icon"></svg-icon>
深度思考
</button>
<!-- <button @click="startNewConversation" class="active">
<svg-icon icon-class="add" class-name="chat-icon"></svg-icon>
新建会话
</button> -->
</section>
<!-- <button @click="sendMessage" :disabled="messageStatus === 'send'"
:class="{ disabled: messageStatus === 'send' }" class="mr10 active send fs16">
发送
</button> -->
<!-- </section> -->
</div>
</div>
</template>
<script>
import SvgIcon from '@/components/svg-icon/index.vue'
import { audioToText, chat, chatProduct } from '@/api/generatedApi'
import { audioToText, gwcsChat, chatProduct } from '@/api/generatedApi'
export default {
components: {
@@ -98,6 +98,7 @@ export default {
},
data() {
return {
requestSingle: undefined,
isThink: false,
newMessage: '',
isRecording: false,
@@ -205,26 +206,32 @@ export default {
},
axiosGetAiChat() {
const abortController = new AbortController()
this.currentMessage = JSON.parse(
JSON.stringify({
type: 'bot',
text: '',
isThink: this.isDeep,
showThink: true,
think: '',
isLike: false,
isDisLike: false,
})
)
this.messages.push(this.currentMessage)
let params = {
query: this.newMessage,
isDeep: this.isDeep ? 1 : 0,
isOnline: this.isSearching ? 1 : 0,
user: 'chenyuda',
conversationId: this.conversationId,
productName: this.productName,
this.requestSingle = abortController
let message = {
is_complete: false.toString(),
information: this.newMessage,
}
this.currentMessage = JSON.parse(JSON.stringify(message))
let params = {
appType: "gwcsHelper",
conversationId: "",
message: JSON.stringify(message),
user: "gwcs-test",
inputs: {}
}
this.currentMessage = {
...message,
type: 'bot',
text: '',
think: '',
isThink: false,
showThink: false,
isLike: false,
isDisLike: false,
}
this.messages.push(this.currentMessage)
// 如果有自定义参数
if (this.chatData) {
for (let k in this.chatData) {
@@ -234,7 +241,7 @@ export default {
if (this.$route.query.compareId) {
params.compareResult = JSON.parse(sessionStorage.getItem('results'))
}
fetch(chat(), {
fetch(gwcsChat(), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
signal: abortController.signal,
@@ -278,8 +285,14 @@ export default {
const cleanLine = line.replace(/^data:\s*/, '')
if (!cleanLine) return null
const data = JSON.parse(cleanLine)
// console.log(data)
if (data.answer) {
this.answerMap += data.answer
const match = /<is_complete>([^<]*)(?:<\/is_complete>)?/.exec(data.answer)
if (match && match[1] === 'true') {
this.requestSingle.abort()
this.axiosGetAiChat()
}
}
return this.updateConversationState(data)
@@ -315,12 +328,13 @@ export default {
this.$emit('update:messageStatus', 'stop')
}
if (!this.currentMessage || !answer) return
if (event !== 'message') return
// console.log(parse);
const mode = isThink ? 'think' : 'text'
const chars = {
answer: answer,
isThink: isThink,
}
this.typingQueue.push(chars)
if (!this.isTyping) {
this.startTypingAnimation(mode)
@@ -337,7 +351,11 @@ export default {
// 取出一个完整文本块
const chunk = this.typingQueue.shift()
const chars = chunk.answer.split('')
// console.log(this.messages);
const chars = /* chunk.answer.split('') */ Array.from(chunk.answer)
// console.log(chars);
const isThink = chunk.isThink
// 内部递归函数,用于逐字输出当前块
const outputChar = () => {
@@ -346,8 +364,8 @@ export default {
setTimeout(typeNextChar, 10)
return
}
const char = chars.shift()
this.currentMessage[isThink ? 'think' : 'text'] += char
const char = chars.shift() || ''
this.$set(this.currentMessage, isThink ? 'think' : 'text', this.currentMessage[isThink ? 'think' : 'text'] + char)
const delay = this.getTypingDelay(char)
setTimeout(outputChar, delay)
}
@@ -377,9 +395,8 @@ export default {
this.axiosGetAiChat()
},
cellClick(item) {
console.log(item);
const productName = item.title
this.messages.push({ type: 'user', text: productName })
this.newMessage = item.title
this.messages.push({ type: 'user', text: this.newMessage })
this.$emit('update:messageStatus', 'send')
this.$emit('update:autoScrollEnabled', true)
this.axiosGetAiChat()
@@ -388,10 +405,22 @@ export default {
}
</script>
<style lang="scss" scoped>
$primary-color: #2e5ca9;
$primary-color: #57a6fc;
$primary-text-color: #f6aa21;
$primary-trans-color: rgba(135, 162, 208, 0.5);
.chat-container {
background-color: #fafafa;
display: flex;
&>div {
margin: 0 auto;
background-color: #fff;
width: 90%;
border-radius: 10px;
}
}
.input-icon {
font-size: 20px;
color: #707070;
@@ -409,7 +438,6 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
flex: 1;
max-height: 80px;
resize: none;
background: #fff;
outline: none;
overflow-y: auto;
padding: 10px;
@@ -421,11 +449,13 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
font-size: 13px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 10px 0 10px;
background-color: #fff;
gap: 10px;
button {
transition: all 0.3s ease-in-out;
padding: 4px 8px;
border: none;
background-color: $primary-trans-color;
@@ -435,41 +465,62 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
cursor: pointer;
}
.send {}
.active {
background-color: $primary-color;
color: #fff;
}
}
.new-session {
position: absolute;
color: white;
margin-left: 10px;
margin-top: 10px;
background-color: $primary-color;
border-radius: 10px;
padding: 2px 4px;
width: fit-content;
background-color: transparent;
left: 50%;
transform: translateX(-50%);
bottom: 80px;
button{
height: fit-content;
button {
border-radius: 10px;
border: 1px solid #090909;
margin-bottom: 15px;
padding: 5px 10px;
background-color: transparent;
border: none;
svg {
color: #090909;
}
}
}
.chat-footer {
display: flex;
align-items: center;
padding: 10px 10px 0 10px;
justify-content: space-between;
padding: 5px 5px 0 5px;
//padding-bottom: constant(safe-area-inset-bottom);
//padding-bottom: env(safe-area-inset-bottom);
background-color: #fff;
// background-color: #fff;
.input-wrapper {
flex: 1;
margin-right: 10px;
// display: flex;
// align-items: center;
// justify-content: space-between;
display: grid;
grid-template-columns: 1fr 50px;
&>button {
// width: 50px;
color: $primary-color;
}
input {
width: 100%;
width: 110%;
padding: 10px;
border: none;
background: #f5f5f5;
background: #fff;
border-radius: 5px;
font-size: 14px;
}
@@ -504,11 +555,6 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
&.disabled {
cursor: not-allowed;
background: red;
}
&.active .bar {
animation: wave-animation 1s infinite ease-in-out;
}
.bar {

View File

@@ -1,6 +1,8 @@
<template>
<section>
{{ messagesList }}
<div v-for="(message, index) in messagesList" :key="index" class="message-item">
<!-- {{ message }} -->
<!--用户消息-->
<div v-if="message.type === 'user'" class="user-message mb10">
<p v-html="message.text"></p>
@@ -10,7 +12,7 @@
<span v-if="message.isThink" style="width: 100%">
<!-- loading-->
<span class="speakLoadingToast pv10">
<van-loading type="spinner" color="#2e5ca9" size="20px" v-if="!message.text" />
<van-loading type="spinner" :color="primaryColor" size="20px" v-if="!message.text" />
<span class="loading-text">{{ message.text ? '思考完成' : '思考中...' }}</span>
<van-icon name="arrow-up" :class="message.showThink ? 'rate360' : 'rate180'" @click="showThink(message)"
v-if="message.think" />
@@ -19,9 +21,9 @@
<p v-html="md.render(message.think)" v-if="message.think && message.showThink" class="thinkText" />
</span>
<div style="width: 100%">
<p v-html="md.render(message.text)" v-if="message.text"></p>
<span class="speakLoadingToast pv10" v-else-if="!message.text && !thinkOk && !message.isThink">
<van-loading type="spinner" color="#2e5ca9" size="20px" v-if="!message.text" />
<p v-html="render(message)"></p>
<span class="speakLoadingToast pv10" v-if="!filterVisible(message)">
<van-loading type="spinner" :color="primaryColor" size="20px" />
</span>
<div class="text-right fs12 mb5 mr10" style="font-size: 10px; color: #f6aa21">
<!-- 新增点赞和踩按钮 -->
@@ -36,11 +38,6 @@
</div>
</div>
</div>
<!--百宝箱-->
<div v-else class="mb10">
<treasure-box :item="message" @setProductName="setProductName"></treasure-box>
</div>
</div>
</section>
</template>
@@ -56,6 +53,7 @@ const md = new MarkdownIt({
linkify: true,
typographer: true,
}).use(markdownItKatex)
export default {
name: 'message',
components: { TreasureBox, [Icon.name]: Icon },
@@ -76,13 +74,27 @@ export default {
data() {
return {
md,
primaryColor: '#57a6fc',
}
},
methods: {
render(message) {
// this.filterVisible(message);
// console.log(md.render(message.text));
return md.render(this.filterVisible(message))
},
setProductName(e) {
this.$emit('setProductName', e)
},
filterVisible(message) {
if (!message.text.startsWith('<')) {
return message.text
}
// 只把 text 标签内容渲染
const match = /<text>([^<]*)(?:<\/text>)?/.exec(message.text)
return match ? match[1] : ''
},
showThink(message) {
this.$set(message, 'showThink', !message.showThink)
@@ -106,7 +118,7 @@ export default {
<style scoped lang="scss">
// 主题颜色定义
$primary-color: #2e5ca9; // 修复了 SCSS 变量的定义方式
$primary-color: #57a6fc; // 修复了 SCSS 变量的定义方式
$primary-text-color: #f6aa21; // 修复了 SCSS 变量的定义方式
$primary-trans-color: rgba(135, 162, 208, 0.5); // 使用rgba定义颜色透明度为0.8
@@ -205,7 +217,7 @@ $primary-trans-color: rgba(135, 162, 208, 0.5); // 使用rgba定义颜色
background-color: #fff;
padding: 10px 10px 2px 10px;
color: $primary-color;
color: #000;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -24,9 +24,9 @@
</div>
</div>
<!-- 滚动到顶部按钮 -->
<div class="button-container" style="background: #fff">
<!-- <div class="button-container" style="background: #fff">
<van-icon name="upgrade" size="2em" @click="scrollToTop" />
</div>
</div> -->
</main>
<chatMessage ref="chatMessage" :messages.sync="messages" :messageStatus.sync="messageStatus" :is-deep.sync="isDeep"
@@ -139,7 +139,6 @@ export default {
watch: {
messages: {
handler() {
console.log(this.messages, 'messages');
this.$nextTick(() => this.scrollToBottom())
},
deep: true,