wujingjing
2024-12-17 c0b572e397be7d1832bbc8ac58ebae20aae5df92
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
<template>
    <el-dialog title="分享链接" v-model="shareCodeIsShow" width="12%" modal-append-to-body lock-scroll :before-close="closeShareClick">
        <div class="w100 h100 flex justify-center items-center flex-col text-center">
            <div class="h100" ref="qrcodeRef"></div>
        </div>
    </el-dialog>
</template>
 
<script setup lang="ts" name="ShareLinkDlg">
import { unrefElement } from '@vueuse/core';
import QRCode from 'qrcodejs2-fixes';
import { nextTick, ref } from 'vue';
import type { ChatMessage } from '../../model/types';
import { shareChatHistoryByPost } from '/@/api/ai/chat';
import { SHARE_URL } from '/@/constants';
import { ElMessage } from 'element-plus';
import useClipboard from 'vue-clipboard3';
 
const shareCodeIsShow = ref(false);
 
const closeShareClick = (item: ChatMessage) => {
    shareCodeIsShow.value = false;
};
const qrcodeRef = ref<HTMLDivElement>(null);
 
const generateShareUrl = async (item: ChatMessage) => {
    const historyId = item.historyId;
 
    const res = await shareChatHistoryByPost({
        history_ids: historyId,
        share_days: 1,
    });
 
    if (!res.values) return;
    const shareId = Object.values(res.values)[0];
    if (!shareId) return;
    const shareLink = `${SHARE_URL}?id=${shareId}`;
    return shareLink;
};
const url = ref('');
const openShare = async (item: ChatMessage) => {
    shareCodeIsShow.value = true;
    url.value = await generateShareUrl(item);
    copyShareCodeClick(url.value);
    const qrCodeEle = unrefElement(qrcodeRef);
    nextTick(() => {
        qrCodeEle.innerHTML = '';
 
        new QRCode(qrCodeEle, {
            text: url,
            width: 100,
            height: 100,
            colorDark: '#000000',
            colorLight: '#ffffff',
        });
    });
};
const { toClipboard } = useClipboard();
 
const copyShareCodeClick = (url) => {
    if (!url) return;
    toClipboard(url);
    ElMessage.success('已复制分享链接');
};
 
defineExpose({
    openShare,
});
</script>
<style scoped lang="scss"></style>