|
@@ -17,18 +17,21 @@ export function timeStamp(StatusMinute, userCardType, nextSendTime) {
|
|
|
|
|
|
|
|
|
// 封装对剪贴板的访问为异步函数
|
|
|
-export function writeToClipboard(text) {
|
|
|
+export function writeToClipboard(text, success = () => { }, error = () => { }) {
|
|
|
// 如果navigator.clipboard存在,直接使用复制功能
|
|
|
if (navigator.clipboard) {
|
|
|
try {
|
|
|
setTimeout(() => {
|
|
|
- return navigator.clipboard.writeText(text)
|
|
|
+ navigator.clipboard.writeText(text).then(() => {
|
|
|
+ success()
|
|
|
+ }).catch(() => {
|
|
|
+ error()
|
|
|
+ })
|
|
|
}, 0);
|
|
|
} catch (err) {
|
|
|
- return Promise.reject(err)
|
|
|
+ error()
|
|
|
}
|
|
|
} else {
|
|
|
- // 否则使用传统的document.execCommand('copy')方法
|
|
|
const textArea = document.createElement('textarea');
|
|
|
textArea.value = text;
|
|
|
document.body.appendChild(textArea);
|
|
@@ -36,12 +39,12 @@ export function writeToClipboard(text) {
|
|
|
try {
|
|
|
const successful = document.execCommand('copy');
|
|
|
if (successful) {
|
|
|
- return Promise.resolve()
|
|
|
+ success()
|
|
|
} else {
|
|
|
throw '复制失败'
|
|
|
}
|
|
|
} catch (err) {
|
|
|
- return Promise.reject(err)
|
|
|
+ error()
|
|
|
}
|
|
|
document.body.removeChild(textArea);
|
|
|
}
|