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