skyouc
2024-12-21 c635d78b479510ebe2556a420948effcd30a0731
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<script setup>
import { getCurrentInstance, ref, watch, nextTick } from 'vue';
import { useRouter } from "vue-router";
import { get, post, postForm } from '@/utils/request.js'
import { message, Modal } from 'ant-design-vue';
import { logout } from '@/config.js';
import { formatMessage } from '@/utils/localeUtils.js';
import useTableSearch from '@/utils/tableUtils.jsx';
const context = getCurrentInstance()?.appContext.config.globalProperties;
 
const open = ref(false)
const showWidth = ref("70%")
const chatContainer = ref(null);
const aiLoading = ref(false)
const messageValue = ref(null)
const messageList = ref([
    {
        'author': 'AI',
        'message': '欢迎使用中扬AI大模型智能助手',
        'type': 'str'
    }
])
 
const handleOk = () => {
 
}
 
const handleCancel = () => {
 
}
 
const openModal = () => {
    open.value = true
}
 
const handleSubmit = () => {
    let message = messageValue.value;
    let tmp = {
        'author': 'Me',
        'message': message,
        'type': 'str'
    }
 
    messageList.value.push(tmp)
    messageValue.value = ''
    aiLoading.value = true
 
    get("http://192.168.4.61:5000/chat?message=" + message, {}).then(resp => {
        let result = resp.data;
        if (result.code == 200) {
            let aiTmp = {
                'author': 'AI',
                'message': result.data,
                'img': result.img,
                'type': result.type
            }
            messageList.value.push(aiTmp)
            aiLoading.value = false
        } else {
            let aiTmp = {
                'author': 'AI',
                'message': '抱歉,我暂时不能回答这个问题',
                'img': '',
                'type': 'str'
            }
            messageList.value.push(aiTmp)
            aiLoading.value = false
        }
 
        scrollToBottom()
    }).catch(resp => {
        let aiTmp = {
            'author': 'AI',
            'message': '抱歉,我暂时不能回答这个问题',
            'img': '',
            'type': 'str'
        }
        messageList.value.push(aiTmp)
        aiLoading.value = false
 
        scrollToBottom()
    })
}
 
const scrollToBottom = () => {
    if (chatContainer.value) {
        nextTick(() => {
            setTimeout(() => {
                chatContainer.value.scrollTop = chatContainer.value.scrollHeight;
            }, 100);
        });
    }
}
 
 
</script>
 
<script>
export default {
    name: 'aiComponent'
}
</script>
 
<template>
    <div>
        <a-modal v-model:open="open" :width="showWidth" @ok="handleOk" @cancel="handleCancel">
            <div class="box" ref="chatContainer">
                <a-comment v-for="(item, index) in messageList" :key="index">
                    <template #author><a>{{ item.author }}</a></template>
                    <template #avatar>
                        <a-avatar src="/public/img/logo.png" :alt="item.author" />
                    </template>
                    <template #content>
                        <div v-if="item.type == 'str'">
                            {{ item.message }}
                        </div>
                        <div v-else-if="item.type == 'html'" v-html="item.message"></div>
                        <div v-else-if="item.type == 'img'">
                            <img :src="'data:image/png;base64,' + item.img" />
                        </div>
                    </template>
                </a-comment>
 
                <a-comment v-if="aiLoading">
                    <template #author><a>AI</a></template>
                    <template #content>
                        <a-spin tip="Loading...">
                            <a-alert message="AI is Loading..."></a-alert>
                        </a-spin>
                    </template>
                </a-comment>
            </div>
 
            <a-comment>
                <template #content>
                    <a-form-item>
                        <a-textarea v-model:value="messageValue" :rows="4" />
                    </a-form-item>
                    <a-form-item>
                        <a-button html-type="submit" type="primary" @click="handleSubmit">
                            发送
                        </a-button>
                    </a-form-item>
                </template>
            </a-comment>
        </a-modal>
 
        <a-float-button type="primary" :style="{
            right: '24px',
        }" @click="openModal">
            <template #icon>
                AI
            </template>
        </a-float-button>
    </div>
</template>
 
<style>
.box {
    height: 768px;
    overflow-y: auto;
}
</style>