-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
918 lines (892 loc) · 32.1 KB
/
index.html
File metadata and controls
918 lines (892 loc) · 32.1 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="icon" href="images/chat/nav_logo.png"/>
<title>AI</title>
<link rel="stylesheet" href="css/home_front.css"/>
<link rel="stylesheet" href="css/chat/chat.css"/>
<link rel="stylesheet" href="css/nprogress.css">
<link rel="stylesheet" href="css/element.css"/>
<link rel="stylesheet" href="css/vant.css"/>
<style>
.e-layout-maincontent {width: 100%; height: 100%; background: #F3F5FB;}
.e-layout-content {width: 100%; height: 100%; padding-left: 80px; box-sizing: border-box;}
@media screen and (max-width: 599px) {
.e-layout-content {padding: 0 16px 0;}
}
</style>
</head>
<body>
<div id="app">
<div class="e-layout-maincontent s_flex">
<section class="e-layout-content">
<e-home
v-if="view_type == 1"
:guest_can_chat="guest_can_chat"
@checkout="checkoutChatView">
</e-home>
<e-chatview
v-if="view_type == 2"
ref="eChatViewRef"
:guest_can_chat="guest_can_chat"
:chat_welcome_message="chat_welcome_message">
</e-chatview>
</section>
</div>
</div>
<script src="js/vue-2.5.16.js"></script>
<script src="js/axios.min.js"></script>
<script src="js/nprogress.js"></script>
<script src="js/element-2.13.2.js"></script>
<script src="js/vant.js"></script>
<script src="js/clipboard.js"></script>
<script src="js/chat/eventsource.min.js"></script>
<script src="js/chat/markdown-it.min.js"></script>
<script src="js/chat/marked.min.js"></script>
<script src="js/chat/chat.js"></script>
<script src="js/jquery.min.js"></script>
<script>
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.headers.common['Access-From'] = is_pc() ? 'pc' : 'mobile'
Vue.prototype.doGet = function (url, params = {}) {
return new Promise((resolve, reject) => {
NProgress.start();
axios.get(url, {params: {...params, guest_id: getCookie('guest_id')}}).then((res) => {
if (res.status == 200) {
if (res.data.error_code == 403) {
vm.guest_can_chat = false
}
if (res.data.error_code == 401) {
vm.showToast({type: 'error', message: res.data.message});
reject(res.data);
return;
}
resolve(res.data);
} else {
vm.showToast({type: 'error', message: '请求出错了'});
}
}).catch(function (error) {
reject(error);
}).then(function () {
NProgress.done();
});
});
}
/**
* post请求
* @param url
* @param params
* @returns {Promise<any>}
*/
Vue.prototype.doPost = function (url, params = {}) {
return new Promise((resolve, reject) => {
NProgress.start();
axios.post(url, {...params, guest_id: getCookie('guest_id')}).then((res) => {
if (res.status == 200) {
if (res.data.error_code == 403) {
vm.guest_can_chat = false
}
if (res.data.error_code == 401) {
vm.showToast({type: 'error', message: res.data.message});
reject(res.data);
return;
}
resolve(res.data);
} else {
vm.showToast({type: 'error', message: '请求出错了'});
}
}).catch(function (error) {
reject(error);
}).then(function () {
NProgress.done();
});
});
}
Vue.prototype.showToast = function (params = {}) {
const status = is_phone() || window.innerWidth < 600
const {type = 'error', message} = params
if (!message) return
if (status) {
Vue.prototype.$toast(message)
} else {
Vue.prototype.$message[type](message)
}
}
</script>
<style>
.e-chatview {width: 100%; height: 100%; display: flex; position: relative;}
/* 左侧网页详情 */
.e-chatview-fixed {width: 420px; height: calc(100% - 63px); padding-right: 16px; padding-top: 50px; box-sizing: border-box;}
/* 中间主体 */
.e-chatview-content {width: 100%; height: 100%; margin: 0 auto; padding: 0 16px; box-sizing: border-box; position: relative;}
.chatview-bottom {width: 100%; padding: 0 16px; box-sizing: border-box; margin: 0 auto;}
.chatview-bottom .tips {font-size: 14px; color: rgba(61,61,61,0.5); line-height: 20px; text-align: center; margin: 13px auto 30px;}
.chatview-center {width: 100%; height: calc(100% - 192px); padding: 50px 16px; box-sizing: border-box; overflow-y: auto;}
.chatview-center::-webkit-scrollbar {display: none; width: 0; height: 0;}
/* 聊天 */
.ai-wrap {width: 100%; padding-bottom: 24px; padding-right: 48px;}
.ai-wrap .ai-head {min-width: 35px; margin-right: 13px;}
.ai-wrap .ai-content { max-width: 100%; width: fit-content; padding: 16px 20px; background-color: #fff; border-radius: 12px; }
.user-wrap {width: 100%; padding-bottom: 24px; padding-left: 48px; justify-content: flex-end;}
.user-wrap .user-head {min-width: 40px; height: 40px; margin-left: 8px; background: url(images/chat/chat_user.png) center no-repeat; background-size: {{ $member['avatar'] ? '40px auto' : '14px 18.5px' }}; border-radius: 50%;}
.user-wrap .user-content {max-width: 100%; width: fit-content; padding: 10px 20px; background-color: #0057FE; border-radius: 12px; color: #fff; word-break: break-all;}
.ai-wrap .news-head{ background: #EFF0F4; border-radius: 10px; width: fit-content; padding: 10px 18px; cursor: pointer;}
.ai-wrap .news-head-title{ margin-right: 22px; margin-left: 10px; font-size: 14px; color: rgba(51,51,51,0.8); }
.ai-wrap .operate-box{ margin-top: 15px; }
.ai-wrap .operate-box img{ width: 18px; height: 18px; cursor: pointer;}
.ai-wrap .ibi-node {width: 16px; height: 16px;vertical-align: middle;cursor: pointer;}
@media screen and (min-width: 856px) {
.e-chatview-content {max-width: 856px;}
}
@media screen and (max-width: 599px) {
.e-chatview-fixed {width: 100%; height: calc(100% - 46px); padding: 56px 0 0; position: absolute; top: 0; right: 0; z-index: 100;}
.e-chatview-content {max-width: 100%; padding: 0;}
.chatview-bottom {padding: 0;}
.chatview-bottom .tips {margin: 13px auto;}
.chatview-center {padding: 56px 0 0 0; height: calc(100% - 175px);}
}
</style>
<script type="text/x-template" id="e-chatview">
<section class="e-chatview">
<div class="e-chatview-content s_flex ai_ct jc_bt flex_dir">
<div class="chatview-center" ref="eChatScrollView">
<template v-if="show_welcome">
<div class="ai-wrap s_flex ai_fs">
<div class="ai-head s_flex ai_ct">
<img src="images/chat/chat_ai.png" width="35.59" height="33.88" class="ai-tx" alt="">
</div>
<div class="ai-content">
<div class="co_333">{{chat_welcome_message}}</div>
</div>
</div>
</template>
<template v-for="(messageItem,index) in msgList">
<section class="message-item" :id="'msg_' + messageItem.id" :key="messageItem.id">
<div class="user-wrap s_flex ai_fs">
<div class="user-content">
<template v-for="(item, qindex) in messageItem.question">
<a :href="item.value" target="_blank" :key="qindex" v-if="item.type == 'link'" style="text-decoration: underline">{{ item.value }}</a>
<span v-else :key="qindex">{{ item.value }}</span>
</template>
</div>
<div class="user-head"></div>
</div>
<div class="ai-wrap s_flex ai_fs">
<div class="ai-head s_flex ai_ct">
<img src="images/chat/chat_ai.png" width="35.59" height="33.88" class="ai-tx" alt="">
</div>
<div class="ai-content">
<template v-if="(messageItem.job_details && messageItem.job_details.length > 0) || messageItem.reply">
<div class="news-head s_flex ai_ct" v-if="messageItem.job_details && messageItem.job_details.length > 0" @click="handleClickArticleList(messageItem)" :style="{'margin-bottom': messageItem.reply || !messageItem.is_history ? '16px' : ''}">
<img src="images/chat/web_search.png" style="width: 16.5px;" alt="">
<div class="news-head-title flex_1 elli_1">已搜到{{ messageItem.job_details.length }}篇资料</div>
<img src="images/chat/web_more.png" style="width: 16px;" alt="">
</div>
<e-ai-message :id="'msg_ai_' + messageItem.id" :msg="messageItem.reply"></e-ai-message>
<p v-if="messageItem.is_early_termination" style="font-size: 12px; line-height: 30px;color: #999">(已停止)</p>
<div class="operate-box s_flex ai_ct jc_bt" v-if="messageItem.id != current_key && messageItem.reply">
<div class="s_flex ai_ct">
<img src="images/chat/chat_copy.png" alt="" class="chat-reply-copy" :data-clipboard-text="replaceIBINodes(messageItem.reply)">
</div>
</div>
</template>
<!-- <img v-if="!messageItem.is_history && ((!messageItem.reply && !messageItem.job_details.length) || (!messageItem.reply && messageItem.job_details.length)) " src="https://cdn.toodudu.com/2024/11/6/loading_1730864499486.webp" width="20" height="20" alt=""> -->
<img v-if="!messageItem.is_history && ((!messageItem.reply && !messageItem.job_details.length) || (!messageItem.reply && messageItem.job_details.length)) " src="images/chat/chat_loading.gif" width="30" height="30" alt="">
<div v-if="messageItem.is_history && !messageItem.reply && !messageItem.job_details.length && messageItem.id">
<p style="font-size: 12px; line-height: 30px;color: #999">(已停止)</p>
</div>
</div>
</div>
</section>
</template>
</div>
<div class="chatview-bottom">
<e-chat-input ref="eChatInputRef" :guest_can_chat="guest_can_chat" @sendMessage="getNewProblem"></e-chat-input>
<p class="tips">内容由AI大模型生产,请仔细甄选</p>
</div>
</div>
</section>
</script>
<script>
Vue.component('e-chatview', {
template: '#e-chatview',
props: {
chat_welcome_message: {
type: String,
default: ''
},
guest_can_chat: {
type: Boolean,
default: false
}
},
data () {
return {
msgList: [],
historyInfo:{
page:1,
number:10,
chat_conversation_id:0,
last_content_id:0
},
hisLoading: false,
historyList:[],
historyMeta:{},
eventSource: null,
disableBtn: false,
show_welcome:false,
show_news:false,
articleList: [],
is_article_detail: false,
is_abort: false,
is_scroll: false,
current_key: '',
}
},
watch: {
guest_can_chat: {
handler(value) {
this.$refs.eChatInputRef.can_chat = value
},
}
},
mounted() {
this.setupScrollListener();
document.addEventListener('click', function(event) {
const target = event.target;
if (target.tagName.toLowerCase() === 'img' && target.classList.contains('ibi-node')) {
// 处理点击事件
console.log(`output->this`,vm.$children[vm.$children.length - 1])
if (target.dataset.ibiId && vm.$children[vm.$children.length - 1] && typeof vm.$children[vm.$children.length - 1].getArticleDetail === 'function') {
vm.$children[vm.$children.length - 1].getArticleDetail(target.dataset.ibiId)
}
}
});
this.$nextTick(() => {
this.$refs.eChatInputRef.can_chat = this.guest_can_chat
})
},
beforeDestory() {
this.removeScrollListener();
},
methods: {
init() {
this.$nextTick(() => {
this.$refs.eChatInputRef.focus()
})
},
reset() {
if (this.disableBtn && this.msgList.length) {
return
}
this.historyList = []
this.historyMeta = {}
this.historyInfo = {
page:1,
number:10,
chat_conversation_id: 0,
last_content_id:0
}
this.hisLoading = false
this.current_key = ''
this.eventSource = null
this.show_welcome = true
this.is_scroll = false
this.msgList = []
this.show_news = false
this.is_article_detail = false
},
sendMessage(question) {
try {
let url = 'grok.php?content=php'
const token = localStorage.getItem('token');
// 检查是否成功获取
if (!token) {
console.error('please insert Token:');
return;
}
this.eventSource = new EventSourcePolyfill(url + `&token=${token}&prompt=${question}`,{
headers: {
'Access-From': 'pc'
}
})
this.eventSource.addEventListener('message', event => {
var jsonString = event.data;
console.log(`output->jsonString`, jsonString)
if (event.data == "\"[DONE]\"") {
this.chatSave(this.msgList[this.msgList.length - 1])
this.chatDone()
return
}
try {
var jsonObject = JSON.parse(jsonString);
this.msgList[this.msgList.length - 1].reply += jsonObject.content
this.$nextTick(() => {
this.$set(this.msgList, this.msgList.length - 1, this.msgList[this.msgList.length - 1])
this.$forceUpdate()
this.scrollToBottom()
this.chatDone()
})
} catch (error) {
console.log(error)
this.chatDone()
}
})
this.eventSource.onerror = (event) => {
event.target.close();
};
} catch (error) {
console.log(error)
this.chatDone()
}
},
getProblem(question) {
this.chatStart()
this.msgList.push({
id: 0,
question: this.processedText(question),
reply: '',
// content_key: 0,
job_details: [],
is_history: false,
})
this.scrollToBottom()
this.sendMessage(question)
// let url = '{{ route("home.chat.question_content") }}'
// this.doGet(url, {chat_conversation_id: this.historyInfo.chat_conversation_id, question}).then(res => {
// if (res.error_code == 0) {
// this.historyInfo.chat_conversation_id = res.data.chat_conversation_id
// this.current_key = res.data.chat_content_id
// this.msgList[this.msgList.length - 1] = {
// id: res.data.chat_content_id,
// question: this.msgList[this.msgList.length - 1].question,
// reply: '',
// job_details: res.data.list,
// is_early_termination: 0,
// is_history: false,
// }
// this.sendMessage({chat_content_id: res.data.chat_content_id, chat_conversation_id: res.data.chat_conversation_id})
// } else {
// this.chatDone()
// this.showToast({message: res.message})
// this.msgList.pop()
// }
// })
},
// 输入框发送数据
getNewProblem(res) {
this.getProblem(res)
},
chatSave(msg, stop) {
if (!this.historyInfo.chat_conversation_id || !msg.id) {
return
}
let params = {chat_conversation_id: this.historyInfo.chat_conversation_id, chat_content_id: msg.id, reply: msg.reply, is_early_termination: 0}
if (stop && !msg.is_history) {
params.is_early_termination = stop.is_early_termination
}
// this.doPost('{{ route("home.chat.reply.save") }}', params).then(res=>{
// if (res.error_code == 0) {
// this.msgList[this.msgList.length - 1].is_history = true
// } else {
// this.showToast({message: res.message})
// }
// })
},
chatStart() {
this.current_key = ''
this.is_scroll = false
this.scrollToBottom()
this.disableBtn = true
this.$refs.eChatInputRef.is_chat = true
},
chatDone() {
this.current_key = ''
this.is_scroll = false
this.is_abort = false
this.scrollToBottom()
this.disableBtn = false
this.$refs.eChatInputRef.is_chat = false
},
scrollToBottom() {
if (this.is_scroll) return
this.$nextTick(() => {
const chatScrollView = this.$refs.eChatScrollView;
if (chatScrollView) {
chatScrollView.scrollTop = chatScrollView.scrollHeight - 50;
}
})
},
// 获取单条历史会话
getSingleChatDetail(id) {
if (!id) return
this.historyInfo.chat_conversation_id = id
this.doGet('{{ route("home.chat.history.show") }}', this.historyInfo).then(res=>{
if (res.error_code == 0) {
this.historyList = res.data.data
this.historyMeta = {
current_page: res.data.current_page,
per_page: res.data.per_page,
total: res.data.total
}
let msgList = JSON.parse(JSON.stringify(this.historyList))
msgList.forEach(item=>{
item.question = this.processedText(item.question)
})
this.$set(this, 'msgList', msgList.reverse())
this.$nextTick(() => {
this.is_scroll = true
this.scrollToBottom()
})
} else {
this.showToast({message: res.message})
}
})
},
// 单条历史会话 加载更多
loadSingleChatDetail(){
if (this.hisLoading) return
console.log(`output->scrollChatUpper`)
if (this.historyList.length < this.historyMeta.total){
this.hisLoading = true
this.historyInfo.last_content_id = this.historyList[this.historyList.length - 1].id
this.doGet('{{ route("home.chat.history.show") }}', this.historyInfo).then(res=>{
this.hisLoading = false
if (res.error_code == 0) {
this.historyList = [...this.historyList,...res.data.data]
this.historyMeta = {
current_page: res.data.current_page,
per_page: res.data.per_page,
total: res.data.total
}
let msgList = res.data.data
msgList.forEach(item=>{
item.question = this.processedText(item.question)
})
this.$set(this, 'msgList', [...msgList.reverse(), ...this.msgList])
this.$nextTick(() => {
const chatScrollView = this.$refs.eChatScrollView;
const targetElement = this.$el.querySelector(`#msg_${this.historyInfo.last_content_id}`);
if (chatScrollView && targetElement) {
// 计算目标元素相对于滚动容器的位置
const targetRect = targetElement.getBoundingClientRect();
const containerRect = chatScrollView.getBoundingClientRect();
// 计算目标元素距离容器顶部的距离
const targetTop = targetRect.top - containerRect.top;
// 设置滚动容器的 scrollTop 属性
chatScrollView.scrollTop = targetTop + chatScrollView.scrollTop - 24;
}
})
} else {
this.showToast({message: res.message})
}
})
}
},
setupScrollListener() {
const scrollContainer = this.$refs.eChatScrollView;
if (scrollContainer) {
scrollContainer.addEventListener('scroll', debounce(this.handleChatScroll, 100));
}
},
removeScrollListener() {
const scrollContainer = this.$refs.eChatScrollView;
if (scrollContainer) {
scrollContainer.removeEventListener('scroll', this.handleChatScroll);
}
},
handleChatScroll() {
const scrollContainer = this.$refs.eChatScrollView;
if (scrollContainer) {
this.is_scroll = true
if (scrollContainer.scrollTop === 0 && !this.hisLoading) {
this.loadSingleChatDetail();
}
}
},
// 获取文章详情
getArticleDetail(article_id) {
if (!article_id) return
this.doGet('{{ route("home.chat.article.details") }}', {article_id}).then(res=>{
if (res.error_code == 0) {
this.articleList = [{
...res.data
}]
this.is_article_detail = true
this.show_news = true
} else {
this.showToast({message: res.message})
}
})
},
// 展开文章详情
handleClickArticleList(item) {
this.articleList = item.job_details
this.is_article_detail = false
this.show_news = true
},
// 关闭文章详情
handleCloseArticle() {
this.show_news = false
},
// 输入问题 校验链接
processedText(question) {
if (!question) return []
let originalText = question
let result = []
const urlPattern = /((http|https):\/\/[^\s]+)/g;
let lastIndex = 0;
let match;
while ((match = urlPattern.exec(originalText)) !== null) {
const start = match.index;
const end = start + match[0].length;
// 添加非链接部分
if (start > lastIndex) {
result.push({
type: 'string',
value: originalText.substring(lastIndex, start)
});
}
// 添加链接部分
result.push({
type: 'link',
value: match[0],
html: `<a href="${match[0]}" target="_blank">${match[0]}</a>`
});
lastIndex = end;
}
// 添加剩余的非链接部分
if (lastIndex < originalText.length) {
result.push({
type: 'string',
value: originalText.substring(lastIndex)
});
}
return result
},
// 回复内容转义
replaceIBINodes(html) {
let matchHtml = html;
const regex = /<ibi\s+([^>]+)>([\s\S]*?)<\/ibi>/g;
const nodes = [];
let match;
while ((match = regex.exec(html)) !== null) {
matchHtml = matchHtml.replace(match[0], '')
}
return matchHtml
},
}
})
</script>
<style>
.e-chat-input {width: 100%; position: relative;}
.e-chat-input .el-textarea__inner {width: 100%; height: 129px; padding: 17px 49px 17px 19px; box-sizing: border-box; border-radius: 16px; border: 1px solid rgba(0,0,0,0.1); color: #333;}
.e-chat-input .e-chat-sendbtn {width: 32px; height: 32px; border-radius: 6px; background: rgba(51,51,51,0.05) url('images/send_btn.png') center no-repeat; background-size: 16px 16px; position: absolute; right: 13px; bottom: 12px; cursor: not-allowed;}
.e-chat-input .e-chat-sendbtn.active {background: #0057FE url('images/send_btn_a.png') center no-repeat; background-size: 19px 19px; cursor: pointer;}
@media screen and (max-width: 599px) {
}
</style>
<style>
.e-home {width: 100%; height: 100%;}
.e-home-content {width: 100%; height: 100%; margin: 0 auto; padding: 0 16px; box-sizing: border-box;}
.e-home-content .home-banner {max-width: 388px; margin-bottom: 54px;}
@media screen and (min-width: 856px) {
.e-home-content {max-width: 856px;}
}
@media screen and (max-width: 599px) {
.e-home-content {max-width: 100%; padding: 0;}
.e-home-content .home-banner {max-width: 70%;}
}
</style>
<script type="text/x-template" id="e-home">
<section class="e-home">
<div class="e-home-content s_flex ai_ct jc_ct flex_dir">
<h1 style="font-size: 40px;padding: 30px">智能AI</h1>
<e-chat-input ref="eChatInputRef" :guest_can_chat="guest_can_chat" @sendMessage="checkout"></e-chat-input>
</div>
</section>
</script>
<script>
Vue.component('e-home', {
template: '#e-home',
props: {
guest_can_chat: {
type: Boolean,
default: false
}
},
watch: {
guest_can_chat: {
handler(value) {
this.$refs.eChatInputRef.can_chat = value
},
}
},
data () {
return {
}
},
mounted() {
const token = localStorage.getItem('token');
// 检查是否成功获取
if (!token) {
this.$prompt('Please input your token', 'Tip', {
confirmButtonText: 'OK',
cancelButtonText: 'Cancel',
}).then(({ value }) => {
this.$message({
type: 'success',
message: 'token save success'
});
localStorage.setItem('token', value);
}).catch(() => {
this.$message({
type: 'info',
message: 'Input canceled'
});
});
}
this.$nextTick(() => {
this.$refs.eChatInputRef.can_chat = this.guest_can_chat
})
},
methods: {
checkout(res) {
this.$emit('checkout',res)
}
}
})
</script>
<script type="text/x-template" id="e-chat-input">
<section class="e-chat-input">
<el-input type="textarea" :placeholder="placeholder" id="chat-textarea" v-model="keywords" resize="none" :disabled="is_chat || !can_chat"></el-input>
<el-tooltip effect="dark" content="enter快捷发送" placement="top" offset="" v-if="!is_mobile">
<div class="e-chat-sendbtn" :class="{'active': keywords && !is_chat}" @click="sendMessage"></div>
</el-tooltip>
<div class="e-chat-sendbtn" v-else :class="{'active': keywords && !is_chat}" @click="sendMessage"></div>
</section>
</script>
<script>
Vue.component('e-chat-input', {
template: '#e-chat-input',
props: {
// guest_can_chat: {
// type: Boolean,
// default: false,
// }
},
data () {
return {
keywords: '',
is_chat: false, // 正在回答中
is_mobile: is_phone(),
can_chat: false,
placeholder: sessionStorage.getItem('chat_welcome_message') || '尽管问',
}
},
watch: {
can_chat: function(newVal) {
if (newVal) {
this.placeholder = sessionStorage.getItem('chat_welcome_message') || '尽管问'
} else {
this.placeholder = '今日会话总数超出限制'
}
}
},
created() {
window.addEventListener('keydown', this.handleGlobalKeyDown);
},
mounted() {
},
beforeDestroy() {
window.removeEventListener('keydown', this.handleGlobalKeyDown);
},
methods: {
sendMessage() {
if (!this.can_chat || !this.keywords || this.is_chat) return
if (!this.keywords.trim()) {
uni.$toast('请输入您的问题')
return
}
this.$emit('sendMessage', this.keywords)
this.keywords = ''
},
handleGlobalKeyDown(event) {
if (event.key === 'Enter') {
this.sendMessage()
}
}
}
})
</script>
<style>
.ai-message {word-break: break-all;}
.ai-message ul, .ai-message ol {padding-inline-start: 2em; margin: .859em 0 0; list-style: auto;}
.ai-message p, .ai-message div,.ai-message ul, .ai-message ol {margin-bottom: .859em;}
.ai-message strong, .ai-message b, .ai-message h1, .ai-message h2, .ai-message h3, .ai-message h4,.ai-message h5, .ai-message h6 {font-weight: 700;}
</style>
<script type="text/x-template" id="e-ai-message">
<section class="ai-message" v-html="result">
</section>
</script>
<script>
Vue.component('e-ai-message', {
template: '#e-ai-message',
props: {
msg: {
type: String,
},
},
computed: {
msgContent() {
return this.msg
},
result() {
if (!this.msgContent) {
return //处理特殊情况,比如网络异常导致的响应的 content 的值为空
}
// return htmlToIBINodes(markdownIt.render(this.msgContent))
return htmlToIBINodes(marked.parse(this.msgContent))
},
nodesList() {
if (!this.msgContent) {
return //处理特殊情况,比如网络异常导致的响应的 content 的值为空
}
let result = []
// 判断markdown中代码块标识符的数量是否为偶数
if (this.msgContent.split("```").length % 2) {
let newContent = this.msgContent.split("```")
result = newContent.map((item, index) => {
if (index % 2 !== 0) {
return {
type: 'code',
value: markdownIt.render(`\`\`\`${item}\`\`\``),
origin: item,
};
} else {
// return htmlToIBINodes(markdownIt.render(item))
return htmlToIBINodes(marked.parse(item))
}
})
} else {
// return htmlToIBINodes(markdownIt.render(this.msgContent))
return htmlToIBINodes(marked.parse(this.msgContent))
}
return result
}
},
data () {
return {
}
},
mounted() {
},
methods: {
}
})
</script>
<script>
const markdownIt = new markdownit({
// 在源码中启用 HTML 标签
html: true,
})
const vm = new Vue({
el: '#app',
data() {
return {
init_page: true,
mobile_style: is_phone() || window.innerWidth < 600,
view_type: 1, // 1:home, 2:chatview
show_history: false,
guest_id: getCookie('guest_id'),
guest_can_chat: false,
chat_welcome_message: '',
page_loading: true,
}
},
created() {
window.addEventListener('resize', debounce(this.handleResize, 100));
},
mounted() {
this.getPageInit()
this.replyCopyToClipboard();
},
destroyed() {
},
methods: {
replyCopyToClipboard() {
const clipboard = new ClipboardJS('.chat-reply-copy');
clipboard.on('success', () => {
this.showToast({message: '复制成功!', type: 'success'})
});
clipboard.on('error', () => {
this.showToast({message: '复制失败!'})
});
},
// 导航栏 打开新会话
sendNewChat() {
if (this.page_loading) return
if (!this.guest_can_chat) {
this.showToast({message: '今日会话总数超出限制'})
return
}
this.show_history = false
if (this.view_type == 2) {
if (this.$refs.eChatViewRef.disableBtn) return
this.showToast({message: '开启新对话', type: 'success'})
this.$refs.eChatViewRef.reset()
}
if (this.view_type == 1) {
this.view_type = 2
this.$nextTick(() => {
this.$refs.eChatViewRef.show_welcome = true
})
}
},
// 导航栏 返回首页
pageBack() {
this.show_history = false
if (this.view_type == 1) return
this.view_type = 1
},
// 首页发送问题 切换聊天
checkoutChatView(message) {
this.show_history = false
this.view_type = 2
this.$nextTick(() => {
this.$refs.eChatViewRef.show_welcome = true
this.$refs.eChatViewRef.getProblem(message)
})
},
// 打开历史
checkoutHistory(value) {
value && this.$refs.eHistoryRef.getHistory()
this.show_history = value
},
// 打开历史会话详情
checkoutChatDetail(id) {
this.show_history = false
this.view_type = 2
this.$nextTick(() => {
this.$refs.eChatViewRef.getSingleChatDetail(id)
this.$refs.eChatViewRef.show_news = false
})
},
getPageInit() {
this.guest_can_chat = true
this.guest_id = 1
},
// 监听窗口变化
handleResize() {
this.mobile_style = is_phone() || window.innerWidth < 600
}
}
})
</script>
</body>
</html>