update: 微信自定义分享

main
fadeaway 8 months ago
parent 2bcc827773
commit 064c1df294

@ -1,6 +1,7 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
output: 'export', output: 'export',
distDir: 'dist',
}; };
export default nextConfig; export default nextConfig;

8
package-lock.json generated

@ -11,7 +11,8 @@
"lib-flexible": "^0.3.2", "lib-flexible": "^0.3.2",
"next": "14.1.3", "next": "14.1.3",
"react": "^18", "react": "^18",
"react-dom": "^18" "react-dom": "^18",
"weixin-js-sdk": "^1.6.5"
}, },
"devDependencies": { "devDependencies": {
"@commitlint/cli": "^19.1.0", "@commitlint/cli": "^19.1.0",
@ -8626,6 +8627,11 @@
} }
} }
}, },
"node_modules/weixin-js-sdk": {
"version": "1.6.5",
"resolved": "https://registry.npmmirror.com/weixin-js-sdk/-/weixin-js-sdk-1.6.5.tgz",
"integrity": "sha512-Gph1WAWB2YN/lMOFB/ymb+hbU/wYazzJgu6PMMktCy9cSCeW5wA6Zwt0dpahJbJ+RJEwtTv2x9iIu0U4enuVSQ=="
},
"node_modules/which": { "node_modules/which": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmmirror.com/which/-/which-2.0.2.tgz", "resolved": "https://registry.npmmirror.com/which/-/which-2.0.2.tgz",

@ -12,7 +12,8 @@
"lib-flexible": "^0.3.2", "lib-flexible": "^0.3.2",
"next": "14.1.3", "next": "14.1.3",
"react": "^18", "react": "^18",
"react-dom": "^18" "react-dom": "^18",
"weixin-js-sdk": "^1.6.5"
}, },
"devDependencies": { "devDependencies": {
"@commitlint/cli": "^19.1.0", "@commitlint/cli": "^19.1.0",

@ -5,7 +5,6 @@ import { useRef } from 'react';
import Image from 'next/image'; import Image from 'next/image';
import Link from 'next/link'; import Link from 'next/link';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import Script from 'next/script';
import AppLogo from '@/components/AppLogo'; import AppLogo from '@/components/AppLogo';
import WXShareBtn from '@/components/WXComponent/WXShareBtn'; import WXShareBtn from '@/components/WXComponent/WXShareBtn';
@ -31,7 +30,6 @@ export default function Home() {
return ( return (
<> <>
<Script src="//res.wx.qq.com/open/js/jweixin-1.6.0.js" strategy="beforeInteractive" />
<main className="max-w-screen-sm min-h-screen mx-auto flex flex-col items-center text-white font-normal"> <main className="max-w-screen-sm min-h-screen mx-auto flex flex-col items-center text-white font-normal">
{/* 首屏 */} {/* 首屏 */}
<div <div

@ -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> */}
</> </>
); );
} }

@ -4778,6 +4778,11 @@ util-deprecate@^1.0.2:
optionalDependencies: optionalDependencies:
fsevents "~2.3.3" fsevents "~2.3.3"
weixin-js-sdk@^1.6.5:
version "1.6.5"
resolved "https://registry.npmmirror.com/weixin-js-sdk/-/weixin-js-sdk-1.6.5.tgz"
integrity sha512-Gph1WAWB2YN/lMOFB/ymb+hbU/wYazzJgu6PMMktCy9cSCeW5wA6Zwt0dpahJbJ+RJEwtTv2x9iIu0U4enuVSQ==
which-boxed-primitive@^1.0.2: which-boxed-primitive@^1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.npmmirror.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz" resolved "https://registry.npmmirror.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz"

Loading…
Cancel
Save