Uni app platform judgment | uni app judgment H5 applet app and other different platforms

Time:2021-3-18

It is recommended to use conditional compilation instead of conditional judgment

//  @/utils/ EPlatform.ts/ **Enumerating ePlatform*/
export enum EPlatform {
  /**App*/
  AppPlus = 'APP-PLUS',
  /**App nvue*/
  AppPlusNvue = 'APP-PLUS-NVUE',
  /**H5*/
  H5 = 'H5',
  /**Wechat applet*/
  MpWeixin = 'MP-WEIXIN',
  * * Alipay applet * /
  MpAlipay = 'MP-ALIPAY',
  /**Baidu app*/
  MpBaidu = 'MP-BAIDU',
  /**Byte skipping applet*/
  MpToutiao = 'MP-TOUTIAO',
  /**Qq small program*/
  MpQq = 'MP-QQ',
  /**360 applet*/
  Mp360 = 'MP-360',
  * * WeChat Applet / Alipay Applet / Baidu Applet / byte beating applet /QQ applet /360 applet * /
  Mp = 'MP',
  /**Fast application general (including alliance and Huawei)*/
  QuickappWebview = 'quickapp-webview',
  /**Fast application Alliance*/
  QuickappWebviewUnion = 'quickapp-webview-union',
  /**Fast application of Huawei*/
  QuickappWebviewHuawei = 'quickapp-webview-huawei',
}
// @/utils/ Platform.ts/ **Type platform*/
import {EPlatform} from "@/utils/EPlatform";

/**Using conditional compilation to get platform information*/
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
}

/**Platform type*/
export const Platform: EPlatform = ifDefPlatform()
/**Default export platform type*/
export default Platform

/**App*/
export const isAppPlus = Platform == EPlatform.AppPlus
/**App nvue*/
export const isAppPlusNvue = Platform == EPlatform.AppPlusNvue
/**H5*/
export const isH5 = Platform == EPlatform.H5
/**Wechat applet*/
export const isMpWeixin = Platform == EPlatform.MpWeixin
* * Alipay applet * /
export const isMpAlipay = Platform == EPlatform.MpAlipay
/**Baidu app*/
export const isMpBaidu = Platform == EPlatform.MpBaidu
/**Byte skipping applet*/
export const isMpToutiao = Platform == EPlatform.MpToutiao
/**Qq small program*/
export const isMpQq = Platform == EPlatform.MpQq
/**360 applet*/
export const isMp360 = Platform == EPlatform.Mp360
* * WeChat Applet / Alipay Applet / Baidu Applet / byte beating applet /QQ applet /360 applet * /
export const isMp = Platform == EPlatform.Mp
/**Fast application general (including alliance and Huawei)*/
export const isQuickappWebview = Platform == EPlatform.QuickappWebview
/**Fast application Alliance*/
export const isQuickappWebviewUnion = Platform == EPlatform.QuickappWebviewUnion
/**Fast application of Huawei*/
export const isQuickappWebviewHuawei = Platform == EPlatform.QuickappWebviewHuawei

use

// @/utils/Config.tsimport {EPlatform} from './EPlatform';
import {isH5, Platform} from '@/utils/Platform'

/**Configuration information*/
export default class Config {
  /**HTTP request root*/
  static get httpBaseUrl(): string {
    if (isH5) {
      return '/'
    } else {
      return 'http://demo.cn/'
    }
  }
}

Address:

https://www.cnblogs.com/stumpx/p/13289154.html