uniapp开发小程序/H5页面,判断微信端、企微端、飞书端
·
第一步
在utils/equipment.js文件
export function getEnvInfo() {
const sys = uni.getSystemInfoSync()
const {
uniPlatform,
ua = ''
} = sys
const u = ua.toLowerCase()
// console.log('sys', sys, navigator.userAgent,u)
// 小程序环境
const isMini = uniPlatform.startsWith('mp-')
const isWechatMini = uniPlatform === 'mp-weixin'
const isLarkMini = uniPlatform === 'mp-lark'
// 企业微信小程序判断
let isWechatWorkMini = false
if (isWechatMini && typeof wx !== 'undefined') {
try {
isWechatWorkMini = wx.env === 'work'
} catch (e) {
isWechatWorkMini = false
}
}
// H5 环境
const isH5 = uniPlatform === 'h5' || uniPlatform === 'web' //兼容 h5 + web
const isWechatH5 = isH5 && /micromessenger/.test(u) && /wechat/.test(u)
const isWechatWorkH5 = isH5 && /micromessenger/.test(u) && /wxwork/.test(u)
const isLarkH5 = isH5 && /lark/.test(u)
const isNormalH5 = isH5 && !isWechatH5 && !isWechatWorkH5 && !isLarkH5
return {
isMini, //小程序
isWechatMini, // 微信小程序
isWechatWorkMini, // 企业微信小程序
isLarkMini, // 飞书小程序
isH5, // 是否H5
isNormalH5, // 普通H5(浏览器)
isWechatH5, // 微信H5
isWechatWorkH5, // 企业微信H5
isLarkH5, // 飞书H5
isWechat: isWechatMini || isWechatH5, //微信端(小程序+H5)
isWechatWork: isWechatWorkMini || isWechatWorkH5, //企业微信端(小程序+H5)
isLark: isLarkMini || isLarkH5, //飞书端(小程序+H5)
}
}
const equipment= getEnvInfo()
export default equipment
第二步
在需要判断的页面
import equipment from '@/utils/equipment.js'
// 检测设备(调用这个函数就行了)
const getMiniOrH5 = () => {
console.log('检测设备', env)
// 是否 H5(所有网页)
if (env.isH5) {
console.log("这是 H5 环境(所有网页)")
}
// 是否普通浏览器 H5
if (env.isNormalH5) {
console.log("这是 普通浏览器 打开的 H5")
}
// 是否微信内 H5
if (env.isWechatH5) {
console.log("这是 微信内部 打开的 H5")
}
// 是否企业微信内 H5
if (env.isWechatWorkH5) {
console.log("这是 企业微信内部 打开的 H5")
}
// 是否飞书内 H5
if (env.isLarkH5) {
console.log("这是 飞书内部 打开的 H5")
}
// ------------------------------------------------
// 是否微信生态(小程序 + H5)
if (env.isWechat) {
console.log("这是 微信环境(含小程序/内部H5)")
}
// 是否企业微信生态(小程序 + H5)
if (env.isWechatWork) {
console.log("这是 企业微信环境")
}
// 是否飞书生态(小程序 + H5)
if (env.isLark) {
console.log("这是 飞书环境")
}
}
更多推荐
所有评论(0)