From b01de93eee97813299dea3b7475cec6d253faaf7 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Thu, 25 Dec 2025 17:45:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(ns-login):=20=E4=BF=AE=E5=A4=8DH5=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=9C=A8=E5=BE=AE=E4=BF=A1App=E4=B8=AD=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E7=99=BB=E5=BD=95=E6=97=A0=E5=8F=8D=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js/util.js | 30 ++++++++++++++++++++++++++++++ components/ns-login/ns-login.vue | 7 ++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/common/js/util.js b/common/js/util.js index cbe0f01..1b95393 100644 --- a/common/js/util.js +++ b/common/js/util.js @@ -379,6 +379,36 @@ export default { return false; } }, + /** + * 判断是否在微信小程序的web-view中打开H5 + * @returns {boolean} 是否在微信小程序的web-view中 + */ + isWeChatMiniProgram() { + const userAgent = navigator.userAgent.toLowerCase(); + + // 微信小程序的web-view User-Agent会包含以下特征之一 + const miniProgramIndicators = [ + 'miniprogram', // 普通微信小程序 + 'wxwork', // 企业微信小程序 + 'micromessenger/[0-9]+\.[0-9]+\.[0-9]+ nettype/wifi wxwebviewtype/1' + ]; + + return miniProgramIndicators.some(indicator => { + if (typeof indicator === 'string' && indicator.includes('/')) { + // 正则表达式匹配 + const regex = new RegExp(indicator, 'i'); + return regex.test(userAgent); + } + return userAgent.includes(indicator); + }); + }, + /** + * 判断是否在微信浏览器中打开H5(非小程序的web-view) + * @returns {boolean} 是否在微信浏览器中打开的H5 + */ + isWeChatBrowser() { + return isWeiXin() && !isWeChatMiniProgram(); + }, /** * 显示消息提示框 * @param {Object} params 参数 diff --git a/components/ns-login/ns-login.vue b/components/ns-login/ns-login.vue index 3d431b6..77e3ec5 100644 --- a/components/ns-login/ns-login.vue +++ b/components/ns-login/ns-login.vue @@ -209,11 +209,16 @@ // #endif // #ifdef H5 - if (this.$util.isWeiXin()) { + if (this.$util.isWeChatMiniProgram()) { + // 微信小程序环境,使用微信授权登录 let authData = uni.getStorageSync('authInfo'); if (authData) this.authLogin(authData); else this.getCode(); + } else if (this.$util.isWeiXin()) { + // 微信浏览器环境,显示登录页面 + this.toLogin(); } else { + // 其他浏览器环境,显示登录页面 this.toLogin(); } // #endif