发布于 

vue,流式输出实现打字机效果

demo1

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
<template>
<div>
<span id="response_row" class="result-streaming">{{ messages }}</span>
<el-button @click="sss">发送</el-button>
</div>
</template>

<script>
export default {
name: 'textChat',
data() {
return {
messages: "" // 用于存储接收到的消息
};
},
methods: {
async sss() {
const data = {
"input": "你好,我叫王大锤。",
"task_name": "0910",
"prompt_id": "None",
"chat_num": 3
}
const response = await fetch('/dev-api/content/chat/chatGPT', {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
})
//获取UTF8的解码
const encode = new TextDecoder("utf-8");
//获取body的reader
const reader = response.body.getReader();
// 循环读取reponse中的内容
while (true) {
const {done, value} = await reader.read();
if (done) {
break;
}
// 解码内容
const text = encode.decode(value);
this.messages += text
// 当获取错误token时,输出错误信息
// if (text === "<ERR>") {
// output.innerText = "Error";
// break;
// } else {
// // 获取正常信息时,逐字追加输出
// output.innerText += text;
// }
}
},
},
};
</script>

<style>
.result-streaming:after {
-webkit-animation: blink 1s steps(5, start) infinite;
animation: blink 1s steps(5, start) infinite;
content: "▋";
margin-left: 0.25rem;
vertical-align: baseline;
}

</style>

demo2

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<template>
<div style="margin-top: 10px;">
<el-card class="box-card" v-loading="loading">
<div class="text item">
<div id="response_row" :class="streamIsEnd?'':'result-streaming'" v-html="messages"></div>
</div>
<div
style="display: flex;justify-content: space-between;text-align: center;margin-top: 20px;margin-bottom: -10px;">
<div style="display: flex;text-align: center;">
<el-button type="text" class="cardBtn" :disabled="!streamIsEnd" @click="sendEvent('replacementContent')"><i
class="el-icon-success"
style="margin-right: 3px;"></i>替换内容
</el-button>
<el-button type="text" class="cardBtn" :disabled="!streamIsEnd" @click="sendEvent('addContent')"><i
class="el-icon-document-add"
style="margin-right: 3px;"></i>添加到内容
</el-button>
<el-button type="text" class="cardBtn"
:disabled="!streamIsEnd"
v-clipboard:copy="messages"
v-clipboard:success="copyContent"
v-clipboard:error="copyContentError"
><i class="el-icon-document-copy" style="margin-right: 3px;"></i>复制
</el-button>
<!-- <el-button type="text" class="cardBtn" @click="handleDelete"><i class="el-icon-delete-solid"-->
<!-- style="margin-right: 3px;"></i>丢弃-->
<!-- </el-button>-->
<!-- <el-button type="text" class="cardBtn" @click="getServiceData"><i class="el-icon-success"-->
<!-- style="margin-right: 3px;"></i>重新生成-->
<!-- </el-button>-->
</div>
<div>
<el-dropdown @command="handleCommand" :disabled="!streamIsEnd">
<span class="el-dropdown-link cardBtn" v-if="status" style="color: blue;">
{{status}}<i class="el-icon-arrow-down el-icon--right" style="color: black"></i>
</span>
<span class="el-dropdown-link cardBtn" v-else>
文案翻译<i class="el-icon-arrow-down el-icon--right" style="color: black"></i>
</span>
<template #dropdown>

<el-dropdown-menu>
<!-- <el-dropdown-item :command="dict.value" v-for="dict in dict.type.content_language">-->
<!-- {{ dict.label }}-->
<!-- </el-dropdown-item>-->
<el-dropdown-item
v-for="(dict,index) in dict.type.content_language"
:key="index"
:command="dict.value"
>{{ dict.label }}</el-dropdown-item>
</el-dropdown-menu>

</template>
</el-dropdown>
</div>
</div>
</el-card>
</div>
</template>

<script>


import {translateGoogle} from "@/api/system/languageTranslate";

export default {
name: "contentCard",
dicts: ['content_language'],
props: {
demand: {
type: String,
default: []
}
},
data() {
return {
loading: false,
streamIsEnd: true,
status: undefined,
messages: ""
};
},
mounted() {
this.getServiceData()
},
methods: {
handleCommand(command) {
let obj = this.dict.type.content_language.filter(item => item.value === command);
this.status=obj[0].label
// const foundElement = this.dict.type.content_language.find(obj => obj.value === command);
// let result = this.messages.replace(/\\/g, '').slice(1, -1);
let result = this.messages.replace(/\\/g, '');
let qs = {
prompt: result,
to_language: command,
text_language: "zh-CN",
}
if (this.$isNotEmpty(result)) {
this.translateGoogle(qs)
}

},
translateGoogle(qs) {
this.loading = true
translateGoogle(qs).then(response => {
this.messages = response.msg
}).finally(() => {
this.loading = false
})
},
copyContentError() {
this.$modal.msgError("复制出错");
},
copyContent() {
this.$modal.msgSuccess("复制成功");
},
sendEvent(type) {
let rd = {
type: type,
data: this.messages
}
this.$emit('articleManipulation', rd)
},
handleDelete() {
var _this = this
this.$modal.confirm('是否确认删除该文章?').then(function () {
_this.messages = ""
});
},
async getServiceData() {
this.loading = true
this.messages = ""
let ins = JSON.parse(this.demand)
delete ins.language;
const data = {
"input": JSON.stringify(ins),
// "task_name": "0910",
// "prompt_id": "None",
"language": JSON.parse(this.demand).language,
// "chat_num": 1
}
const response = await fetch('/dev-api/content/chat/chatGPT', {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
})
//获取UTF8的解码
const encode = new TextDecoder("utf-8");
//获取body的reader
const reader = response.body.getReader();
this.loading = false
this.streamIsEnd = false
// 循环读取reponse中的内容
while (true) {
const {done, value} = await reader.read();
if (done) {
this.streamIsEnd = true
break;
}
// 解码内容
const text = encode.decode(value);
this.messages += text.replace(/\n/g, '<br>')
// 当获取错误token时,输出错误信息
// if (text === "<ERR>") {
// output.innerText = "Error";
// break;
// } else {
// // 获取正常信息时,逐字追加输出
// output.innerText += text;
// }
}
}

}
};
</script>
<style>
.selected{
color:red;
}
.result-streaming:after {
-webkit-animation: blink 1s steps(5, start) infinite;
animation: blink 1s steps(5, start) infinite;
content: "▋";
margin-left: 0.25rem;
vertical-align: baseline;
}

.cardBtn {
font-size: 16px;
background-color: #f8f8fa;
border-radius: 5px;
padding: 2px 8px;
color: rgb(128, 128, 128);
border: 1px solid #efefef
}

.text {
font-size: 14px;
}

.item {
padding: 10px 0;
}

.box-card {
width: 100%;
background-color: #ffffff;
}
</style>