-
zhangwei
2025-03-05 16213c0f85aa3ac8317797bf4a05fd12940e16d3
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>
    <view @click="handleClick">
        <slot>复制</slot>
    </view>
</template>
<script>
export default {
    name: "up-copy",
    props: {
        content: {
            type: String,
            default: ''
        },
        alertStyle: {
            type: String,
            default: 'toast'
        },
        notice: {
            type: String,
            default: '复制成功'
        }
    },
    emits: ['success'],
    methods: {
        handleClick() {
            let content = this.content;
            if (!content) {
                uni.showToast({
                    title: '暂无',
                    icon: 'none',
                    duration: 2000,
                });
                return false;
            }
            content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
            /**
            * 小程序端 和 app端的复制逻辑
            */
            let that = this;
            uni.setClipboardData({
                data: content,
                success: function() {
                    if (that.alertStyle == 'modal') {
                        uni.showModal({
                            title: '提示',
                            content: that.notice
                        });
                    } else {
                        uni.showToast({
                            title: that.notice,
                            icon: 'none'
                        });
                    }
                    that.$emit('success');
                },
                fail:function(){
                    uni.showToast({
                        title: '复制失败',
                        icon: 'none',
                        duration:3000,
                    });
                }
            });
        }
    }
}
</script>
 
<style lang="scss" scoped>
</style>