mini/src/utils/platform.ts 提供了一套完整的平台检测工具,用于在 Taro 跨端小程序中识别当前运行环境,支持微信小程序、H5 网页端等多种平台的条件判断。
import { getPlatform, isWeapp, isH5 } from '@/utils/platform'
getPlatform(): TaroGeneral.ENV_TYPE
返回当前运行环境的平台类型枚举值。
返回值说明:
WEAPP: 微信小程序WEB: H5 网页端RN: React NativeSWAN: 百度智能小程序ALIPAY: 支付宝小程序TT: 字节跳动小程序QQ: QQ 小程序使用示例:
import { getPlatform } from '@/utils/platform'
import Taro from '@tarojs/taro'
const currentPlatform = getPlatform()
console.log('当前平台:', currentPlatform)
// 根据平台执行不同逻辑
switch (currentPlatform) {
case Taro.ENV_TYPE.WEAPP:
// 微信小程序专属逻辑
break
case Taro.ENV_TYPE.WEB:
// H5 网页端专属逻辑
break
default:
// 其他平台通用逻辑
}
isWeapp(): boolean
判断当前是否在微信小程序环境中运行。
使用示例:
import { isWeapp } from '@/utils/platform'
if (isWeapp()) {
// 微信小程序专属功能
wx.login({
success: (res) => {
console.log('微信登录成功:', res.code)
}
})
// 使用小程序 API
wx.getUserProfile({
desc: '用于完善用户资料',
success: (res) => {
console.log('用户信息:', res.userInfo)
}
})
} else {
// 非小程序环境的替代方案
console.log('当前不是微信小程序环境')
}
isH5(): boolean
判断当前是否在 H5 网页端环境中运行。
使用示例:
import { isH5 } from '@/utils/platform'
if (isH5()) {
// H5 网页端专属功能
// 使用浏览器 API
localStorage.setItem('token', 'your-token')
// 使用 DOM API
window.addEventListener('resize', handleResize)
} else {
// 小程序环境的替代方案
Taro.setStorageSync('token', 'your-token')
}
import { isWeapp, isH5 } from '@/utils/platform'
const PlatformSpecificButton = () => {
if (isWeapp()) {
return (
<Button onClick={() => wx.navigateToMiniProgram({ appId: 'targetAppId' })}>
打开其他小程序
</Button>
)
}
if (isH5()) {
return (
<Button onClick={() => window.open('https://example.com', '_blank')}>
打开外部链接
</Button>
)
}
return null
}
import { isWeapp, isH5 } from '@/utils/platform'
const uploadImage = async (file: File) => {
if (isWeapp()) {
// 小程序上传
return new Promise((resolve, reject) => {
wx.uploadFile({
url: '/api/upload',
filePath: file.path,
name: 'file',
success: resolve,
fail: reject
})
})
}
if (isH5()) {
// H5 上传
const formData = new FormData()
formData.append('file', file)
const response = await fetch('/api/upload', {
method: 'POST',
body: formData
})
return response.json()
}
}
import { isWeapp, isH5 } from '@/utils/platform'
const getPlatformStyles = () => {
const baseStyles = 'p-4 rounded-lg'
if (isWeapp()) {
return `${baseStyles} bg-green-100 text-green-800`
}
if (isH5()) {
return `${baseStyles} bg-blue-100 text-blue-800 shadow-lg`
}
return baseStyles
}
平台检测工具与 Taro 的 API 完美集成,可以结合使用:
import { isWeapp, isH5 } from '@/utils/platform'
import Taro from '@tarojs/taro'
// 平台特定的导航
const navigateToPage = (url: string) => {
if (isWeapp()) {
Taro.navigateTo({ url })
} else if (isH5()) {
window.location.href = url
}
}
// 平台特定的存储
const setStorage = (key: string, value: any) => {
if (isWeapp()) {
Taro.setStorageSync(key, value)
} else if (isH5()) {
localStorage.setItem(key, JSON.stringify(value))
}
}
可以根据项目需要扩展更多平台检测函数:
```typescript // 在 platform.ts 中添加更多检测函数 export const isAlipay = (): boolean => { return getPlatform() === Taro.ENV_TYPE.ALIPAY }
export const isBaidu = (): boolean => { return getPlatform() === Taro.ENV_TYPE.SWAN }