cankao-h5/index.html

249 lines
8.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script>
var coverSupport =
"CSS" in window &&
typeof CSS.supports === "function" &&
(CSS.supports("top: env(a)") || CSS.supports("top: constant(a)"));
document.write(
'<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' +
(coverSupport ? ", viewport-fit=cover" : "") +
'" />'
);
</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">
console.log("🚀 ~ window.location.href:", window.location.href);
const urlParams = parseUrlParams();
wx.miniProgram.postMessage({
data: {
type: "share_info",
userType: urlParams?.userType || null,
phone: urlParams?.phone || null,
id: urlParams?.id || null,
},
});
// 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参数解析函数 ==========
function parseUrlParams() {
const params = {};
let search = window.location.search; // 获取 ? 后的参数(如 ?name=test&age=18
let hash = window.location.hash; // 获取 # 后的参数(如 #/pages/index?name=test
// 步骤1优先解析 search 参数history模式
if (search) {
// 去掉开头的 ?,拆分参数对
const searchParams = new URLSearchParams(search.slice(1));
searchParams.forEach((value, key) => {
// 解码中文/特殊字符(解决小程序传参中文乱码)
params[key] = decodeURIComponent(value);
});
}
// 步骤2解析 hash 中的参数Uniapp H5 默认 hash 路由,参数可能在 # 后)
if (hash) {
// 提取 hash 中 ? 后的部分(如 #/pages/index?name=test → ?name=test
const hashQuery = hash.split("?")[1];
if (hashQuery) {
const hashParams = new URLSearchParams(hashQuery);
hashParams.forEach((value, key) => {
params[key] = decodeURIComponent(value); // 解码
});
}
}
return params;
}
</script>
<!-- <script
type="text/javascript"
src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"
></script>
<script>
window.jWeixin = window.wx;
delete window.wx;
</script>
<script src="https://www.cs.com.cn/js/2020/jquery-3.4.1.min.js"></script> -->
<!-- <link rel="icon" href="/src/assets/favicon.ico" /> -->
<title></title>
<!--preload-links-->
<!--app-context-->
</head>
<body>
<div id="app"><!--app-html--></div>
<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">
var link = location.href.split("#")[0];
console.log("🚀 ~ link:", link);
var link2 = location.href.split("#")[1];
console.log("🚀 ~ link2:", link2);
$.ajax({
url: "https://cankao.cs.com.cn/mini/wechat/share",
type: "GET",
data: {
url: link,
},
async: true,
dataType: "json",
success: function (res) {
const { data } = res;
console.log("🚀 ~ data:", data);
jWeixin.config({
debug: false,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: [
"updateAppMessageShareData",
"updateTimelineShareData",
"onMenuShareTimeline",
"onMenuShareAppMessage",
],
});
jWeixin.error(function (res) {
console.log(res);
});
jWeixin.ready(function () {
jWeixin.updateAppMessageShareData({
title: "中证参考",
desc: "天下事 早知道",
link: link,
imgUrl: "https://cankao.cs.com.cn/static/share-default.jpg",
success: function () {},
});
jWeixin.updateTimelineShareData({
title: "中证参考",
desc: "天下事 早知道",
link: link,
imgUrl: "https://cankao.cs.com.cn/static/share-default.jpg",
success: function () {},
});
jWeixin.onMenuShareAppMessage({
title: "中证参考",
desc: "天下事 早知道",
link: link,
imgUrl: "https://cankao.cs.com.cn/static/share-default.jpg",
trigger: function (res) {},
success: function (res) {},
cancel: function (res) {},
fail: function (res) {},
});
jWeixin.onMenuShareTimeline({
title: "中证参考",
desc: "天下事 早知道",
link: link,
imgUrl: "https://cankao.cs.com.cn/static/share-default.jpg",
trigger: function (res) {},
success: function (res) {},
cancel: function (res) {},
fail: function (res) {},
});
});
},
error: function (error) {},
});
</script> -->
</body>
</html>