Skip to content

Commit

Permalink
fix: 修复运行时环境判断错误
Browse files Browse the repository at this point in the history
  • Loading branch information
yang1206 committed Jul 8, 2023
1 parent b443300 commit ce3b0c7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 157 deletions.
2 changes: 1 addition & 1 deletion example/vite.config.ts
Expand Up @@ -12,7 +12,7 @@ import { NutResolver } from '../packages/nutui/src'
// https://vitejs.dev/config/
export default defineConfig({
root: process.cwd(),
base: '/ui/',
// base: '/ui/',
resolve: {
alias: {
'@/': `${resolve(__dirname, 'src')}/`,
Expand Down
252 changes: 98 additions & 154 deletions packages/nutui/components/_utils/env.ts
@@ -1,159 +1,103 @@
const processShim: typeof process
= typeof process !== 'undefined' ? process : ({} as typeof process)
const envShim = processShim.env || ({} as typeof process.env)

type PLATFORM =
| 'h5'
| 'app'
| 'mp-alipay'
| 'mp-baidu'
| 'mp-kuaishou'
| 'mp-lark'
| 'mp-qq'
| 'mp-toutiao'
| 'mp-weixin'
| 'quickapp-webview'
| 'quickapp-webview-huawei'
| 'quickapp-webview-union'

/** Value of `process.env.UNI_PLATFORM` */
export const platform = envShim.UNI_PLATFORM as PLATFORM

/** Value of `process.env.UNI_UTS_PLATFORM` */
export const utsPlatform = envShim.UNI_UTS_PLATFORM as
| PLATFORM
| 'app-android'
| 'app-ios'
| 'web'

/** Value of `process.env.UNI_APP_PLATFORM` */
export const appPlatform = envShim.UNI_APP_PLATFORM as
| 'android'
| 'ios'
| undefined

/** Value of `process.env.UNI_SUB_PLATFORM` */
export const subPlatform = envShim.UNI_SUB_PLATFORM as
| 'quickapp-webview-huawei'
| 'quickapp-webview-union'
| undefined

/** Value of `process.env.UNI_CLI_CONTEXT` */
export const cliContext = envShim.UNI_CLI_CONTEXT as string

/** Value of `process.env.UNI_INPUT_DIR` */
export const inputDir = envShim.UNI_INPUT_DIR as string

/** Value of `process.env.UNI_OUTPUT_DIR` */
export const outputDir = envShim.UNI_OUTPUT_DIR as string

/** Detect if `VUE_APP_DARK_MODE` environment variable is set */
export const hasDarkMode = toBoolean(envShim.VUE_APP_DARK_MODE)

/** Detect if `SOURCEMAP` environment variable is set */
export const hasSourcemap = toBoolean(envShim.SOURCEMAP)

/** Value of `process.env.UNI_COMPILER` */
export const compiler = envShim.UNI_COMPILER as 'vue' | 'nvue'

/** Value of `process.env.UNI_NVUE_COMPILER` */
export const nvueCompiler = envShim.UNI_NVUE_COMPILER as
| 'uni-app'
| 'vue'
| undefined

/** Value of `process.env.UNI_NVUE_STYLE_COMPILER` */
export const nvueStyleCompiler = envShim.UNI_NVUE_STYLE_COMPILER as
| 'uni-app'
| undefined

/** Value of `process.env.UNI_COMPILER_VERSION` */
export const compilerVersion = envShim.UNI_COMPILER_VERSION as string

/** Value of `process.env.UNI_COMPILER_VERSION_TYPE` */
export const compilerVersionType = envShim.UNI_COMPILER_VERSION_TYPE as
| 'a'
| 'r'

/** Value of `process.env.STAT_TITLE_JSON` */
export const statTitleJson = parseJSON<Record<string, string>>(
envShim.STAT_TITLE_JSON,
)

/** Value of `process.env.UNI_CUSTOM_CONTEXT` */
export const customContext = envShim.UNI_CUSTOM_CONTEXT

/** Value of `process.env.UNI_CUSTOM_SCRIPT` */
export const customScript = envShim.UNI_CUSTOM_SCRIPT

/** Value of `process.env.UNI_CUSTOM_DEFINE` */
export const customDefine = envShim.UNI_CUSTOM_DEFINE

/** Value of `process.env.UNI_SUBPACKAGE` */
export const subpackage = envShim.UNI_SUBPACKAGE

/** Detect if `UNI_MP_PLUGIN` environment variable is set */
export const isMpPlugin = toBoolean(envShim.UNI_MP_PLUGIN)

/** Value of `process.env.UNI_RENDERER` */
export const renderer = envShim.UNI_RENDERER as 'native' | undefined

/** Value of `process.env.UNI_RENDERER_NATIVE` */
export const rendererNative = envShim.UNI_RENDERER_NATIVE as
| 'appService'
| 'pages'
| undefined

/** Detect if `UNI_MINIMIZE` environment variable is set */
export const isMinimize = toBoolean(envShim.UNI_MINIMIZE)

/** Detect if `UNI_SSR_CLIENT` environment variable is set */
export const isSSRClient = toBoolean(envShim.UNI_SSR_CLIENT)

/** Detect if `UNI_SSR_SERVER` environment variable is set */
export const isSSRServer = toBoolean(envShim.UNI_SSR_SERVER)

/** Detect if `process.env.UNI_PLATFORM` is `H5` */
export const isH5 = platform === 'h5'

/** Detect if `process.env.UNI_PLATFORM` is `APP` */
export const isApp = platform === 'app'

/** Detect if `process.env.UNI_APP_PLATFORM` is `android` or if `process.env.UNI_UTS_PLATFORM` is "app-android" */
export const isAppAndroid = appPlatform === 'android' || utsPlatform === 'app-android'

/** Detect if `process.env.UNI_APP_PLATFORM` is `ios` or if `process.env.UNI_UTS_PLATFORM` is "app-ios" */
export const isAppIOS = appPlatform === 'ios' || utsPlatform === 'app-ios'

/** Detect if `process.env.UNI_PLATFORM` is `小程序` */
export const isMp = /^mp-/i.test(platform)

/** Detect if `process.env.UNI_PLATFORM` is `微信小程序` */
export const isMpWeixin = platform === 'mp-weixin'

/** Detect if `process.env.UNI_PLATFORM` is `支付宝小程序` */
export const isMpAlipay = platform === 'mp-alipay'

/** Detect if `process.env.UNI_PLATFORM` is `百度小程序` */
export const isMpBaidu = platform === 'mp-baidu'

/** Detect if `process.env.UNI_PLATFORM` is `快手小程序` */
export const isMpKuaishou = platform === 'mp-kuaishou'

/** Detect if `process.env.UNI_PLATFORM` is `QQ小程序` */
export const isMpQQ = platform === 'mp-qq'

/** Detect if `process.env.UNI_PLATFORM` is `头条小程序` */
export const isMpToutiao = platform === 'mp-toutiao'

/** Detect if `process.env.UNI_PLATFORM` is `快应用` */
export const isQuickapp = /^quickapp--webview/i.test(platform)
/** 枚举EPlatform */
enum EPlatform {
/** App */
AppPlus = 'APP-PLUS',
/** App nvue */
AppPlusNvue = 'APP-PLUS-NVUE',
/** H5 */
H5 = 'H5',
/** 微信小程序 */
MpWeixin = 'MP-WEIXIN',
/** 支付宝小程序 */
MpAlipay = 'MP-ALIPAY',
/** 百度小程序 */
MpBaidu = 'MP-BAIDU',
/** 字节跳动小程序 */
MpToutiao = 'MP-TOUTIAO',
/** QQ小程序 */
MpQq = 'MP-QQ',
/** 360小程序 */
Mp360 = 'MP-360',
/** 微信小程序/支付宝小程序/百度小程序/字节跳动小程序/QQ小程序/360小程序 */
Mp = 'MP',
/** 快应用通用(包含联盟、华为) */
QuickappWebview = 'quickapp-webview',
/** 快应用联盟 */
QuickappWebviewUnion = 'quickapp-webview-union',
/** 快应用华为 */
QuickappWebviewHuawei = 'quickapp-webview-huawei',
}

/** Detect if `process.env.UNI_PLATFORM` is `快应用联盟` */
export const isQuickappUnion = platform === 'quickapp-webview-union'
/** 使用条件编译获取平台信息 */
function ifDefPlatform(): EPlatform {
let platform: EPlatform
// #ifdef APP-PLUS
platform = EPlatform.AppPlus
// #endif
// #ifdef APP-PLUS-NVUE
platform = EPlatform.AppPlusNvue
// #endif
// #ifdef H5
platform = EPlatform.H5
// #endif
// #ifdef MP-WEIXIN
platform = EPlatform.MpWeixin
// #endif
// #ifdef MP-ALIPAY
platform = EPlatform.MpAlipay
// #endif
// #ifdef MP-BAIDU
platform = EPlatform.MpBaidu
// #endif
// #ifdef MP-TOUTIAO
platform = EPlatform.MpToutiao
// #endif
// #ifdef MP-QQ
platform = EPlatform.MpQq
// #endif
// #ifdef MP-360
platform = EPlatform.Mp360
// #endif
// #ifdef MP
platform = EPlatform.Mp
// #endif
// #ifdef quickapp-webview
platform = EPlatform.QuickappWebview
// #endif
// #ifdef quickapp-webview-union
platform = EPlatform.QuickappWebviewUnion
// #endif
// #ifdef quickapp-webview-huawei
platform = EPlatform.QuickappWebviewHuawei
// #endif
return platform
}

/** Detect if `process.env.UNI_PLATFORM` is `快应用华为` */
export const isQuickappHuawei = platform === 'quickapp-webview-huawei'
/** 平台类型 */
export const platform: EPlatform = ifDefPlatform()

/** H5 */
export const isH5 = platform === EPlatform.H5
/** 微信小程序 */
export const isMpWeixin = platform === EPlatform.MpWeixin
/** 支付宝小程序 */
export const isMpAlipay = platform === EPlatform.MpAlipay
/** 百度小程序 */
export const isMpBaidu = platform === EPlatform.MpBaidu
/** 字节跳动小程序 */
export const isMpToutiao = platform === EPlatform.MpToutiao
/** QQ小程序 */
export const isMpQq = platform === EPlatform.MpQq
/** 360小程序 */
export const isMp360 = platform === EPlatform.Mp360
/** 微信小程序/支付宝小程序/百度小程序/字节跳动小程序/QQ小程序/360小程序 */
export const isMp = platform === EPlatform.Mp
/** 快应用通用(包含联盟、华为) */
export const isQuickappWebview = platform === EPlatform.QuickappWebview
/** 快应用联盟 */
export const isQuickappWebviewUnion = platform === EPlatform.QuickappWebviewUnion
/** 快应用华为 */
export const isQuickappWebviewHuawei = platform === EPlatform.QuickappWebviewHuawei

// utils
function toBoolean(val: any) {
Expand Down
13 changes: 11 additions & 2 deletions packages/nutui/components/picker/picker.vue
Expand Up @@ -2,7 +2,7 @@
import type { CSSProperties } from 'vue'
import { computed, defineComponent, onMounted, reactive, ref, toRefs, watch } from 'vue'
import type { PickerOption } from '../pickercolumn/types'
import { isH5, pxCheck } from '../_utils'
import { pxCheck } from '../_utils'
import { useTranslate } from '../../locale'
import NutPickerColumn from '../pickercolumn/pickercolumn.vue'
import { pickerEmits, pickerProps } from './picker'
Expand Down Expand Up @@ -188,6 +188,15 @@ const {
pickerViewStyles,
} = componentWeapp()
// #endif
function confirmIf() {
// #ifdef H5
confirmHandler()
// #endif
// #ifndef H5
confirmHandlerMp()
// #endif
}
</script>

<script lang="ts">
Expand All @@ -212,7 +221,7 @@ export default defineComponent({
<view class="nut-picker__title">
{{ title }}
</view>
<view class="nut-picker__confirm nut-picker__right nut-picker__button" @click="isH5 ? confirmHandler() : confirmHandlerMp()">
<view class="nut-picker__confirm nut-picker__right nut-picker__button" @click="confirmIf ">
{{
okText || translate('confirm')
}}
Expand Down

0 comments on commit ce3b0c7

Please sign in to comment.