feat(分享): 添加新闻详情页的微信分享功能

在indexNewsInfo.vue中添加微信分享URL生成和跳转逻辑
更新index.html中的微信JS-SDK相关代码
移除minihome/index.vue中无用的样式注释
修复pages.json文件格式问题
This commit is contained in:
34701892@qq.com 2025-12-25 16:34:25 +08:00
parent e8a9f2cb91
commit 2dc59ae87d
4 changed files with 110 additions and 8 deletions

View File

@ -16,9 +16,9 @@
<!-- <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> --> <!-- <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> -->
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script> <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script type="text/javascript"> <script type="text/javascript">
const urlParams = parseUrlParams(); console.log("🚀 ~ window.location.href:", window.location.href);
console.log("解析后的URL参数", urlParams); // 示例输出:{name: "张三", age: "20"}
const urlParams = parseUrlParams();
wx.miniProgram.postMessage({ wx.miniProgram.postMessage({
data: { data: {
type: "share_info", type: "share_info",
@ -28,6 +28,26 @@
}, },
}); });
// const currentRoute = getCurrentRoute();
// console.log("当前纯路由地址:", currentRoute); // 输出:/pages/detail/indexNewsInfo
function getCurrentRoute() {
// 1. 获取hash处理hash为空的边界情况
const hash = window.location.hash || "";
if (!hash || hash === "#") return "";
// 2. 去掉开头的#,得到 hash 主体(如 /pages/detail/indexNewsInfo?id=xxx
const hashMain = hash.slice(1);
// 3. 拆分路由和参数(以?为分隔符,取第一部分)
const routePart = hashMain.split("?")[0];
// 4. 兼容:去除路由前后多余的空格(防异常)
const pureRoute = routePart.trim();
return pureRoute;
}
// ========== 核心URL参数解析函数 ========== // ========== 核心URL参数解析函数 ==========
function parseUrlParams() { function parseUrlParams() {
const params = {}; const params = {};
@ -78,6 +98,77 @@
<body> <body>
<div id="app"><!--app-html--></div> <div id="app"><!--app-html--></div>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
<script>
// // 路由变化的处理逻辑
// function handleRouteChange() {
// console.log("🚀 ~ handleRouteChange ~ handleRouteChange:");
// const { route, params } = parseH5Route();
// console.log("H5地址变化", {
// 路由: route, // 如 "/pages/detail/indexNewsInfo"
// 参数: params, // 如 {id: "98511"}
// });
// if (route === "/pages/detail/indexNewsInfo") {
// wx.miniProgram.navigateTo({
// url: "/pages/webView/index?url=" + encodeURIComponent(shareUrl),
// });
// }
// // 更新全局变量供Uniapp页面使用
// window.currentH5Route = { route, params };
// // 派发自定义事件供Vue页面监听
// window.dispatchEvent(new CustomEvent("h5RouteChanged", { detail: { route, params } }));
// }
// // 监听hash变化Uniapp默认hash路由
// window.addEventListener("hashchange", handleRouteChange);
// // 监听history变化如浏览器前进/后退、history.pushState
// window.addEventListener("popstate", handleRouteChange);
// // 初始化解析
// handleRouteChange();
// // 页面卸载时移除监听,防止内存泄漏
// window.addEventListener("beforeunload", () => {
// window.removeEventListener("hashchange", handleRouteChange);
// window.removeEventListener("popstate", handleRouteChange);
// });
// // 解析H5路由兼容hash/history
// function parseH5Route() {
// let route = "";
// let params = {};
// const { hash, pathname, search } = window.location;
// // 处理Uniapp默认的hash路由
// if (hash) {
// const hashMain = hash.slice(1);
// const [routePart, paramPart] = hashMain.split("?");
// route = routePart;
// // 解析hash参数
// if (paramPart) {
// const paramArr = paramPart.split("&");
// paramArr.forEach((item) => {
// const [key, value] = item.split("=");
// if (key) params[key] = decodeURIComponent(value || "");
// });
// }
// }
// // 处理history路由
// else {
// route = pathname;
// // 解析search参数
// if (search) {
// const searchParams = new URLSearchParams(search.slice(1));
// searchParams.forEach((value, key) => {
// params[key] = decodeURIComponent(value);
// });
// }
// }
// return { route, params };
// }
</script>
<!-- <script type="text/javascript"> <!-- <script type="text/javascript">
var link = location.href.split("#")[0]; var link = location.href.split("#")[0];
console.log("🚀 ~ link:", link); console.log("🚀 ~ link:", link);

View File

@ -152,6 +152,18 @@ onLoad(async (option: any) => {
window.location.reload(); window.location.reload();
} }
} }
let shareUrl =
"https://cankao.cs.com.cn/jnh/#/pages/detail/indexNewsInfo?id=" +
option.id +
"&phone=" +
uni.getStorageSync("token") +
"&userType=" +
userType.value;
wx.miniProgram.navigateTo({
url: "/pages/webView/index?url=" + encodeURIComponent(shareUrl),
});
}); });
const newList = async (columnId: number) => { const newList = async (columnId: number) => {
@ -263,6 +275,7 @@ function goLogin() {
} }
import { useTimerStore } from "@/stores/timerStore"; import { useTimerStore } from "@/stores/timerStore";
import wx from "weixin-js-sdk";
// function initWxConfig() { // function initWxConfig() {
// getWeChatSdkData({ url: window.location.href }).then((res: any) => { // getWeChatSdkData({ url: window.location.href }).then((res: any) => {

View File

@ -25,15 +25,13 @@
></view> ></view>
<view class="content"> <view class="content">
<view <!-- :style="{
:style="{
position: 'sticky', position: 'sticky',
top: getNavHeight() + 'px', top: getNavHeight() + 'px',
backgroundColor: isScroll ? '#fff' : 'transparent', backgroundColor: isScroll ? '#fff' : 'transparent',
zIndex: '9999', zIndex: '9999',
}" }" -->
v-if="tabIndex == 0 || tabIndex == 2" <view v-if="tabIndex == 0 || tabIndex == 2">
>
<!-- <view class="r_sreach"> <!-- <view class="r_sreach">
<image class="logo_text" src="https://cankao.obs.cn-east-3.myhuaweicloud.com/mini/newmini/logo_text_icon.png"> <image class="logo_text" src="https://cankao.obs.cn-east-3.myhuaweicloud.com/mini/newmini/logo_text_icon.png">
</image> </image>