写入剪贴板实现方式更改为document.execCommand

This commit is contained in:
yezian 2024-02-05 16:29:17 +08:00
parent 3333823400
commit f7d6d7767a
5 changed files with 40 additions and 39 deletions

View File

@ -12,6 +12,7 @@ import verification from "@/public/icon/verification.png";
import icon_border from "@/public/images/icon_border.png";
import Link from "next/link";
import { setCookie } from "cookies-next";
import copy from "@/utils/copy";
export default function StreamerDetail({ params }) {
//
@ -53,15 +54,11 @@ export default function StreamerDetail({ params }) {
}, []);
//cookie
const copyAndSetCookieInviter = async () => {
try {
setCookie("inviter", data?.user_id);
await navigator.clipboard.writeText(
`${data?.name}】『ID${data?.user_id}复制此条消息打开铁粉空间APP查看详情https://tiefen.fun/${data?.user_id}`
);
} catch (error) {
console.error("复制到剪贴板时出现错误:", error);
}
const copyAndSetCookieInviter = () => {
setCookie("inviter", data?.user_id);
copy(
`${data?.name}】『ID${data?.user_id}复制此条消息打开铁粉空间APP查看详情https://tiefen.fun/${data?.user_id}`
);
};
return (

View File

@ -15,6 +15,7 @@ import baseRequest from "@/utils/baseRequest";
import { generateSignature } from "@/utils/crypto";
import { Toast } from "antd-mobile";
import icon_border from "@/public/images/icon_border.png";
import copy from "@/utils/copy";
export default function Download({ params }) {
const [deviceType, setDeviceType] = useState("");
@ -31,14 +32,10 @@ export default function Download({ params }) {
}, []);
//
const copyInviter = async () => {
try {
await navigator.clipboard.writeText(
`复制此条消息打开铁粉空间APP查看详情https://tiefen.fun/${params.user_id}`
);
} catch (error) {
console.error("复制到剪贴板时出现错误:", error);
}
const copyInviter = () => {
copy(
`${data?.name}】『ID${data?.user_id}复制此条消息打开铁粉空间APP查看详情https://tiefen.fun/${data?.user_id}`
);
};
//

View File

@ -6,6 +6,7 @@ import { checkAuth } from "@/utils/auth";
import { useRouter } from "next/navigation";
import baseRequest from "@/utils/baseRequest";
import { generateSignature } from "@/utils/crypto";
import copy from "@/utils/copy";
export default function Purchased() {
//
@ -159,7 +160,7 @@ export default function Purchased() {
const GetWechatModal = () => {
const handleCopy = () => {
navigator.clipboard.writeText(currentWechat);
copy(currentWechat);
Toast.show({
content: "复制成功",
});

View File

@ -8,6 +8,7 @@ import { generateSignature } from "@/utils/crypto";
import Image from "next/image";
import verification from "@/public/icon/verification.png";
import Link from "next/link";
import copy from "@/utils/copy";
export default function Weibo({ params }) {
//
@ -49,27 +50,10 @@ export default function Weibo({ params }) {
}, []);
//
const copyInviter = async () => {
let input = document.createElement("input");
input.style.position = "fixed";
input.style.top = "-10000px";
input.style.zIndex = "-999";
document.body.appendChild(input);
input.value = `${data?.name}】『ID${data?.user_id}复制此条消息打开铁粉空间APP查看详情https://tiefen.fun/${data?.user_id}`;
input.focus();
input.select();
try {
let result = document.execCommand("copy");
document.body.removeChild(input);
if (!result || result === "unsuccessful") {
console.log("复制失败");
} else {
console.log("复制成功");
}
} catch (e) {
document.body.removeChild(input);
console.log("当前浏览器不支持复制功能,请检查更新或更换其他浏览器操作");
}
const copyInviter = () => {
copy(
`${data?.name}】『ID${data?.user_id}复制此条消息打开铁粉空间APP查看详情https://tiefen.fun/${data?.user_id}`
);
};
return (

22
utils/copy.js Normal file
View File

@ -0,0 +1,22 @@
export default function copy(text = "") {
let input = document.createElement("input");
input.style.position = "fixed";
input.style.top = "-10000px";
input.style.zIndex = "-999";
document.body.appendChild(input);
input.value = text;
input.focus();
input.select();
try {
let result = document.execCommand("copy");
document.body.removeChild(input);
if (!result || result === "unsuccessful") {
console.log("复制失败");
} else {
console.log("复制成功");
}
} catch (e) {
document.body.removeChild(input);
console.log("当前浏览器不支持复制功能,请检查更新或更换其他浏览器操作");
}
}