"use client"; import React, { useState, useEffect } from "react"; import baseRequest from "@/utils/baseRequest"; import { Toast } from "antd-mobile"; import { generateSignature } from "@/utils/crypto"; import Image from "next/image"; import html2canvas from "html2canvas"; export default function GenerateLink({ params }) { //生成二维码 const [qrcodeUrl, setQrcodeUrl] = useState(""); useEffect(() => { var QRCode = require("qrcode"); QRCode.toDataURL( `https://tiefen.fun/${params.user_id}`, function (err, url) { setQrcodeUrl(url); } ); }, []); //页面数据 const [isFetching, setIsFetching] = useState(true); const [data, setData] = useState({}); useEffect(() => { const getData = async () => { try { const base = baseRequest(); const signature = generateSignature({ user_id: parseInt(params.user_id, 10), ...base, }); const detailResponse = await fetch( `/api/streamer/list_ext_by_user_id?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user_id: parseInt(params.user_id, 10), ...base, }), } ); const detailData = await detailResponse.json(); if (detailData.ret === -1) { Toast.show({ content: detailData.msg, }); return; } setData(detailData.data.streamer_ext); setIsFetching(false); } catch (error) { console.error(error); } }; getData(); }, []); //保存二维码 const saveQrcode = async () => { window.ReactNativeWebView.postMessage( JSON.stringify({ type: "SAVE_IMAGE", data: qrcodeUrl, }) ); }; //保存海报 const savePoster = async () => { const element = document.getElementById("print"); const canvas = await html2canvas(element, { useCORS: true, }); const data = canvas.toDataURL("image/jpg"); window.ReactNativeWebView.postMessage( JSON.stringify({ type: "SAVE_IMAGE", data: data, }) ); }; //复制链接 const copyUrl = () => { window.ReactNativeWebView.postMessage( JSON.stringify({ type: "COPY_URL", data: `https://tiefen.fun/${params.user_id}`, }) ); }; if (isFetching) { return ( ); } return ( 已为您生成专属链接与分享海报 请保存您的分享链接或海报 复制链接 保存分享海报到相册 只保存二维码到相册 {data?.name} {data?.bio} 我的微信 {data?.platforms?.map((item, index) => ( {item.link_name}|{item.nickname} ))} {qrcodeUrl && ( )} ); }
请保存您的分享链接或海报
{data?.name}
{data?.bio}
我的微信
{item.link_name}|{item.nickname}