|
|
@ -1,64 +1,92 @@
|
|
|
|
'use client';
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
|
|
// import { useState, useEffect } from 'react';
|
|
|
|
import { /** useState, */ useEffect, useRef } from 'react';
|
|
|
|
|
|
|
|
|
|
|
|
import Image from 'next/image';
|
|
|
|
// import Image from 'next/image';
|
|
|
|
|
|
|
|
import wx from 'weixin-js-sdk';
|
|
|
|
|
|
|
|
|
|
|
|
// import { APPID } from '@/const';
|
|
|
|
import { APPID } from '@/const';
|
|
|
|
// import useUA from '@/hooks/useUA';
|
|
|
|
import useUA from '@/hooks/useUA';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 分享获取微信token
|
|
|
|
|
|
|
|
async function getShareAccessToken(url: string) {
|
|
|
|
|
|
|
|
const res = await fetch('http://api.indie.cn:9012/user/wechat/getShareAccessToken', {
|
|
|
|
|
|
|
|
// const res = await fetch('http://39.103.180.196:9012/user/wechat/getShareAccessToken', {
|
|
|
|
|
|
|
|
method: 'post',
|
|
|
|
|
|
|
|
body: JSON.stringify({ url }),
|
|
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.json();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function WXShareBtn() {
|
|
|
|
export default function WXShareBtn() {
|
|
|
|
// const { inWX } = useUA();
|
|
|
|
const { inWX } = useUA();
|
|
|
|
// const [wxPrepared, setWxPrepared] = useState(false);
|
|
|
|
// const [wxPrepared, setWxPrepared] = useState(false);
|
|
|
|
|
|
|
|
const fetchingRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
|
|
const handleShare = () => {
|
|
|
|
// const handleShare = () => {
|
|
|
|
alert('敬请期待');
|
|
|
|
|
|
|
|
// if (!wxPrepared) return;
|
|
|
|
// if (!wxPrepared) return;
|
|
|
|
// window.wx.share();
|
|
|
|
// };
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
// if (!inWX) return;
|
|
|
|
if (!inWX) return;
|
|
|
|
|
|
|
|
|
|
|
|
// const { searchParams } = new URL(window.location.href);
|
|
|
|
const { origin, pathname, searchParams } = new URL(window.location.href);
|
|
|
|
// const code = searchParams.get('code');
|
|
|
|
const code = searchParams.get('code');
|
|
|
|
|
|
|
|
|
|
|
|
// // 未授权
|
|
|
|
// 未授权
|
|
|
|
// if (!code) {
|
|
|
|
if (!code) {
|
|
|
|
// window.location.replace(
|
|
|
|
window.location.replace(
|
|
|
|
// `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=snsapi_base&state=queyue#wechat_redirect`,
|
|
|
|
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=snsapi_base&state=queyue#wechat_redirect`,
|
|
|
|
// );
|
|
|
|
);
|
|
|
|
// return;
|
|
|
|
return;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // 已授权
|
|
|
|
// 已授权
|
|
|
|
// const wx = window.wx;
|
|
|
|
if (fetchingRef.current) return;
|
|
|
|
// if (typeof wx === 'undefined') return;
|
|
|
|
fetchingRef.current = true;
|
|
|
|
// wx.config({
|
|
|
|
getShareAccessToken(window.location.href)
|
|
|
|
// debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
|
|
|
.then((res) => {
|
|
|
|
// appId: APPID, // 必填,公众号的唯一标识
|
|
|
|
if (!res?.data) return;
|
|
|
|
// timestamp: 0, // 必填,生成签名的时间戳
|
|
|
|
if (typeof wx === 'undefined') return;
|
|
|
|
// nonceStr: '', // 必填,生成签名的随机串
|
|
|
|
const { timestamp, nonceStr, signature } = res?.data || {};
|
|
|
|
// signature: '', // 必填,签名
|
|
|
|
wx.config({
|
|
|
|
// jsApiList: [wx.updateAppMessageShareData], // 必填,需要使用的JS接口列表
|
|
|
|
debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
|
|
|
// });
|
|
|
|
appId: APPID, // 必填,公众号的唯一标识
|
|
|
|
// wx.ready(function () {
|
|
|
|
timestamp, // 必填,生成签名的时间戳
|
|
|
|
// // 需在用户可能点击分享按钮前就先调用
|
|
|
|
nonceStr, // 必填,生成签名的随机串
|
|
|
|
// wx.updateAppMessageShareData({
|
|
|
|
signature, // 必填,签名
|
|
|
|
// title: '', // 分享标题
|
|
|
|
jsApiList: ['updateAppMessageShareData', 'onMenuShareAppMessage', 'onMenuShareTimeline'], // 必填,需要使用的JS接口列表
|
|
|
|
// desc: '', // 分享描述
|
|
|
|
});
|
|
|
|
// link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
|
|
|
|
wx.ready(function () {
|
|
|
|
// imgUrl: '', // 分享图标
|
|
|
|
|
|
|
|
// success: () => {
|
|
|
|
|
|
|
|
// setWxPrepared(true);
|
|
|
|
// setWxPrepared(true);
|
|
|
|
// },
|
|
|
|
const queryArr: string[] = [];
|
|
|
|
// });
|
|
|
|
searchParams.forEach((value, key) => {
|
|
|
|
// });
|
|
|
|
if (key && !['code', 'state'].includes(key)) {
|
|
|
|
// }, [inWX]);
|
|
|
|
queryArr.push(`${key}=${value}`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
const shareParam = {
|
|
|
|
|
|
|
|
title: '雀乐', // 分享标题
|
|
|
|
|
|
|
|
link: `${origin}${pathname}${queryArr.length > 0 ? `?${queryArr.join('&')}` : ''}`, // 分享链接
|
|
|
|
|
|
|
|
desc: '独立 不独于世', // 分享描述
|
|
|
|
|
|
|
|
imgUrl: '', // 分享图标
|
|
|
|
|
|
|
|
success() {},
|
|
|
|
|
|
|
|
cancel() {},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
wx.updateAppMessageShareData(shareParam);
|
|
|
|
|
|
|
|
wx.onMenuShareAppMessage(shareParam);
|
|
|
|
|
|
|
|
wx.onMenuShareTimeline(shareParam);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.finally(() => {
|
|
|
|
|
|
|
|
fetchingRef.current = false;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}, [inWX]);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<>
|
|
|
|
<button
|
|
|
|
{/* <button
|
|
|
|
className="w-[283px] h-[48px] flex items-center justify-center bg-[#000000f2] font-medium text-[17px] rounded-full mb-[16px]"
|
|
|
|
className="w-[283px] h-[48px] flex items-center justify-center bg-[#000000f2] font-medium text-[17px] rounded-full mb-[16px]"
|
|
|
|
onClick={handleShare}
|
|
|
|
onClick={handleShare}
|
|
|
|
>
|
|
|
|
>
|
|
|
@ -70,7 +98,7 @@ export default function WXShareBtn() {
|
|
|
|
alt="wx-share"
|
|
|
|
alt="wx-share"
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
<span>分享给好友</span>
|
|
|
|
<span>分享给好友</span>
|
|
|
|
</button>
|
|
|
|
</button> */}
|
|
|
|
</>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|