feat(微信小程序): 添加运行时环境判断及微信小程序webview跳转支持
添加 judgeRuntimeEnv 工具函数判断当前运行环境 在 domesticMini 和 RankListMini 组件中根据环境使用不同的导航方式 微信小程序webview环境下使用 wx.miniProgram.navigateTo 跳转
This commit is contained in:
parent
7cd93926f8
commit
b492406f7b
|
|
@ -71,6 +71,7 @@ import { ref, watch } from "vue";
|
|||
import dayjs from "dayjs/esm/index";
|
||||
import LoginPopup from "@/components/loginPopup/index.vue";
|
||||
import { Session } from "@/utils/storage";
|
||||
import { judgeRuntimeEnv } from "@/utils/util";
|
||||
|
||||
const isExp = ref(true);
|
||||
const rankListLocal = ref([]);
|
||||
|
|
@ -165,10 +166,8 @@ function goDetail(item) {
|
|||
// }
|
||||
let phone = props.userParams.phone || "";
|
||||
let userType = props.userParams.userType || "";
|
||||
uni.navigateTo({
|
||||
url: `/pages/detail/indexNewsInfo?id=${item.news_id}&phone=${phone}&userType=${userType}`,
|
||||
});
|
||||
|
||||
if (judgeRuntimeEnv().isWxMiniWebview) {
|
||||
let shareUrl =
|
||||
"https://cankao.cs.com.cn/jnh/#/pages/detail/indexNewsInfo?id=" +
|
||||
// "http://localhost:8881/jnh/#/pages/detail/indexNewsInfo?id=" +
|
||||
|
|
@ -178,9 +177,14 @@ function goDetail(item) {
|
|||
"&userType=" +
|
||||
userType.value;
|
||||
|
||||
wx.miniProgram.redirectTo({
|
||||
wx.miniProgram.navigateTo({
|
||||
url: "/pages/webView/index?url=" + encodeURIComponent(shareUrl),
|
||||
});
|
||||
} else {
|
||||
uni.navigateTo({
|
||||
url: `/pages/detail/indexNewsInfo?id=${item.news_id}&phone=${phone}&userType=${userType}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const LoginShow = ref(false);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,18 @@ import { Session } from "@/utils/storage";
|
|||
import { getDomestic } from "@/api/index";
|
||||
import List from "@/components/articleList/indexDomestic.vue";
|
||||
import { onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
|
||||
import { judgeRuntimeEnv } from "@/utils/util";
|
||||
import wx from "weixin-js-sdk";
|
||||
|
||||
const props = defineProps({
|
||||
userParams: {
|
||||
type: Object,
|
||||
default: () => ({
|
||||
phone: "",
|
||||
userType: "",
|
||||
}),
|
||||
},
|
||||
});
|
||||
const newsList = ref([]);
|
||||
|
||||
const form = reactive({
|
||||
|
|
@ -25,6 +36,27 @@ function doDetail(item) {
|
|||
uni.navigateTo({
|
||||
url: "/pages/detail/indexNewsInfo?id=" + item.id + "&type=guonei",
|
||||
});
|
||||
|
||||
let phone = props.userParams.phone || "";
|
||||
let userType = props.userParams.userType || "";
|
||||
|
||||
if (judgeRuntimeEnv().isWxMiniWebview) {
|
||||
let shareUrl =
|
||||
"https://cankao.cs.com.cn/jnh/#/pages/detail/indexNewsInfo?id=" +
|
||||
item.news_id +
|
||||
"&phone=" +
|
||||
uni.getStorageSync("token") +
|
||||
"&userType=" +
|
||||
userType.value;
|
||||
|
||||
wx.miniProgram.navigateTo({
|
||||
url: "/pages/webView/index?url=" + encodeURIComponent(shareUrl),
|
||||
});
|
||||
} else {
|
||||
uni.navigateTo({
|
||||
url: `/pages/detail/indexNewsInfo?id=${item.news_id}&phone=${phone}&userType=${userType}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function getDomesticFn() {
|
||||
|
|
|
|||
|
|
@ -88,3 +88,43 @@ export function getNavHeight() {
|
|||
|
||||
return statusBarHeight;
|
||||
}
|
||||
|
||||
import wx from "weixin-js-sdk";
|
||||
/**
|
||||
* 判断当前环境:浏览器 / 微信小程序webview
|
||||
* @returns {Object} { isBrowser: 布尔, isWxMiniWebview: 布尔 }
|
||||
*/
|
||||
export function judgeRuntimeEnv() {
|
||||
// 初始化结果
|
||||
const result = {
|
||||
isBrowser: false,
|
||||
isWxMiniWebview: false,
|
||||
};
|
||||
|
||||
// 1. 优先通过 uni-app 内置API判断(编译到小程序时生效)
|
||||
try {
|
||||
const systemInfo = uni.getSystemInfoSync();
|
||||
// 小程序环境(包括webview)的 platform 为 'devtools'(开发者工具)、'ios'、'android'
|
||||
// 且不存在浏览器的 window.navigator 特征(或UA不含浏览器标识)
|
||||
if (systemInfo.platform && !/(Chrome|Safari|Firefox)/i.test(navigator.userAgent)) {
|
||||
// 进一步判断是否是微信小程序
|
||||
if (navigator.userAgent.includes("miniProgram")) {
|
||||
result.isWxMiniWebview = true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// uni.getSystemInfoSync 执行失败,说明不是小程序环境(如浏览器)
|
||||
}
|
||||
|
||||
// 2. 检测微信小程序特有全局对象(webview中会注入wx对象)
|
||||
if (typeof wx !== "undefined" && wx.miniProgram) {
|
||||
result.isWxMiniWebview = true;
|
||||
}
|
||||
|
||||
// 3. 检测浏览器环境(兜底判断)
|
||||
if (typeof window !== "undefined" && typeof document !== "undefined" && !result.isWxMiniWebview) {
|
||||
result.isBrowser = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue