瀏覽代碼

feat: add the global constant "__UNI_PLATFORM__"

Feng 2 年之前
父節點
當前提交
c214ebedfb
共有 4 個文件被更改,包括 29 次插入2 次删除
  1. 14 0
      global.d.ts
  2. 10 0
      src/pages/index/index.vue
  3. 2 1
      tsconfig.json
  4. 3 1
      vite.config.ts

+ 14 - 0
global.d.ts

@@ -0,0 +1,14 @@
+declare const __UNI_PLATFORM__:
+  | 'h5'
+  | 'app'
+  | 'mp-alipay'
+  | 'mp-baidu'
+  | 'mp-jd'
+  | 'mp-kuaishou'
+  | 'mp-lark'
+  | 'mp-qq'
+  | 'mp-toutiao'
+  | 'mp-weixin'
+  | 'quickapp-webview'
+  | 'quickapp-webview-huawei'
+  | 'quickapp-webview-union'

+ 10 - 0
src/pages/index/index.vue

@@ -17,6 +17,7 @@
     </view>
     <view class="text-center text-4xl main-title-color mt-4">unibest</view>
     <view class="text-center text-2xl mt-2 mb-8">最好用的 uniapp 开发模板</view>
+
     <view class="text-justify max-w-100 m-auto text-4 indent mb-2">{{ description }}</view>
     <view class="mt-8 text-center">
       <text class="text-green-400">在线文档:</text>
@@ -40,12 +41,21 @@
       </a>
       <!-- #endif -->
     </view>
+
+    <view class="text-center py-4" v-if="isH5">当前平台是 - h5</view>
+    <view class="text-center py-4" v-else-if="isApp">当前平台是 - app</view>
+    <view class="text-center py-4" v-else>当前平台是 - {{ platform }}</view>
   </view>
 </template>
 
 <script lang="ts" setup>
 // 获取屏幕边界到安全区域距离
 const { safeAreaInsets } = uni.getSystemInfoSync()
+const platform = __UNI_PLATFORM__
+const isH5 = __UNI_PLATFORM__ === 'h5'
+const isApp = __UNI_PLATFORM__ === 'app'
+const isMP = __UNI_PLATFORM__.startsWith('mp-')
+const isQuickApp = __UNI_PLATFORM__.startsWith('quickapp-')
 
 const author = ref('菲鸽')
 const description = ref(

+ 2 - 1
tsconfig.json

@@ -17,7 +17,8 @@
       "@dcloudio/types",
       "@types/wechat-miniprogram",
       "@uni-helper/uni-app-types",
-      "wot-design-uni/global.d.ts"
+      "wot-design-uni/global.d.ts",
+      "./global.d.ts"
     ]
   },
   "vueCompilerOptions": {

+ 3 - 1
vite.config.ts

@@ -108,7 +108,9 @@ export default ({ command, mode }) => {
       // TODO: 缓存每次压缩过的图片,已经压缩过的不再压缩
       imagemin(mode === 'production'),
     ],
-
+    define: {
+      __UNI_PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM),
+    },
     css: {
       postcss: {
         plugins: [