wujingjing
2024-12-10 6d5277904cd93216154d1ae8d8d452ff01725c55
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
<template>
    <ywDialog
        class="answer-view"
        v-model="dialogIsShow"
        :title="dialogTitle"
        width="50%"
        @dlgClosed="closeDialog"
        :showHeaderIcon="false"
        :showFooter="false"
    >
        <div style="height: 600px" class="relative">
            <el-tabs v-model="activeTab" class="h-full">
                <el-tab-pane :label="sqlTabMap[SqlTabType.Answer]" :name="SqlTabType.Answer" class="h-full">
                    <codemirror
                        class="h-full overflow-auto"
                        disabled
                        style="height: 546px"
                        :modelValue="answerValue"
                        :autofocus="true"
                        :indent-with-tab="true"
                        :tab-size="2"
                        :extensions="jsonEditorExtensions"
                    />
                </el-tab-pane>
                <el-tab-pane :label="sqlTabMap[SqlTabType.Log]" :name="SqlTabType.Log" class="h-full">
                    <codemirror
                        class="h-full overflow-auto"
                        disabled
                        style="height: 546px"
                        :modelValue="logValue"
                        :autofocus="true"
                        :indent-with-tab="true"
                        :tab-size="2"
                        :extensions="jsonEditorExtensions"
                    />
                </el-tab-pane>
            </el-tabs>
 
            <i @click="copyInfo" class="ywifont ywicon-copy text-blue-400 !text-[25px] cursor-pointer absolute bottom-10 right-10"></i>
        </div>
    </ywDialog>
</template>
 
<script setup lang="ts">
import { json } from '@codemirror/lang-json';
import { vscodeDark } from '@uiw/codemirror-theme-vscode';
import { computed, ref, watch } from 'vue';
import { Codemirror } from 'vue-codemirror';
import { getChatHistoryAnswerByPost } from '/@/api/ai/chat';
import ywDialog from '/@/components/dialog/yw-dialog.vue';
import commonFunction from '/@/utils/commonFunction';
 
import JSONFormat from 'json-format';
 
const props = defineProps(['item']);
const dialogIsShow = defineModel({
    type: Boolean,
});
const closeDialog = () => {
    dialogIsShow.value = false;
};
const dialogTitle = computed(() => {
    let label = props.item?.question ?? '';
 
    let len = label.gblen();
    const maxLen = 50;
    if (len > maxLen) {
        label = label.slice(0, 20) + '...';
    }
 
    return label + '——回答内容';
});
const jsonEditorExtensions = [json(), vscodeDark];
 
const { copyText } = commonFunction();
 
const copyInfo = async () => {
    if ((activeTab.value === SqlTabType.Answer)) {
        copyText(answerValue.value);
    } else {
        copyText(logValue.value);
    }
};
 
const answerValue = ref('');
const logValue = ref('');
watch(
    () => dialogIsShow.value,
    async (val) => {
        if (!val) {
            answerValue.value = '';
            logValue.value = '';
            return;
        }
        const res = await getChatHistoryAnswerByPost({
            id: props.item.id,
        });
        answerValue.value = res?.ask_json ? JSONFormat(res.ask_json) : '';
        logValue.value = res?.ask_log;
    }
);
 
//#region ====================== 面板 ======================
 
const enum SqlTabType {
    Answer = 'answer',
    Log = 'log',
}
const activeTab = ref<SqlTabType>(SqlTabType.Answer);
 
const sqlTabMap = {
    [SqlTabType.Answer]: '回答',
    [SqlTabType.Log]: '日志',
};
//#endregion
</script>
<style scoped lang="scss">
:deep(.el-card__body) {
    position: relative;
}
 
:deep(.answer-view .el-dialog__body) {
    padding-top: 5px !important;
}
</style>