u-copy.vue
1.62 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
<template>
<view @click="handleClick">
<slot>{{ t("up.common.copy") }}</slot>
</view>
</template>
<script>
import { t } from '../../libs/i18n'
export default {
name: "up-copy",
props: {
content: {
type: String,
default: ''
},
alertStyle: {
type: String,
default: 'toast'
},
notice: {
type: String,
default: t("up.common.copy") + t("up.common.success")
}
},
emits: ['success'],
methods: {
t,
handleClick() {
let content = this.content;
if (!content) {
uni.showToast({
title: t("up.common.none"),
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: "up.common.tip",
content: that.notice
});
} else {
uni.showToast({
title: that.notice,
icon: 'none'
});
}
that.$emit('success');
},
fail:function(){
uni.showToast({
title: t("up.common.copy") + t("up.common.fail"),
icon: 'none',
duration:3000,
});
}
});
}
}
}
</script>
<style lang="scss" scoped>
</style>