Skip to content

getApp

描述

获取当前应用类型。综合判断当前运行环境,返回对应的应用类型。检测优先级:PC客户端 > 移动端客户端 > 微信/微博环境 > 浏览器环境。

语法

ts
getApp(): AppType

返回值

  • AppType: 当前应用类型,可能的值包括:
    • 'ths': 同花顺移动端
    • 'thsPcTy': 同花顺PC统一客户端
    • 'thsPcYh': 同花顺PC远航版客户端
    • 'futures': 期货通移动端
    • 'futuresPc': 期货通PC客户端
    • 'ifind': iFund客户端
    • 'ainvest': A股投资客户端
    • 'weixin': 微信环境
    • 'weibo': 微博环境
    • 'browser': 普通浏览器
    • 'unknown': 未知环境

异常

  • 无异常抛出,在检测失败时会返回 'unknown' 并输出警告信息

检测优先级

  1. PC客户端(最高优先级)

    • 同花顺PC统一客户端 (thsPcTy)
    • 同花顺PC远航版客户端 (thsPcYh)
    • 期货通PC客户端 (futuresPc)
  2. 移动端客户端

    • 同花顺移动端 (ths)
    • 期货通移动端 (futures)
    • iFund客户端 (ifind)
    • A股投资客户端 (ainvest)
  3. 微信/微博环境

    • 微信环境 (weixin)
    • 微博环境 (weibo)
  4. 浏览器环境

    • 普通浏览器 (browser)
  5. 未知环境

    • 未知环境 (unknown)

示例

基本用法

ts
import { getApp } from '@fu/matrix';

const appType = getApp();
console.log(`当前应用: ${appType}`);

// 输出示例:
// 当前应用: thsPcTy
// 当前应用: ths
// 当前应用: weixin
// 当前应用: browser

条件判断

ts
import { getApp } from '@fu/matrix';

const appType = getApp();

switch (appType) {
  case 'thsPcTy':
    console.log('同花顺PC统一客户端');
    // 执行PC统一客户端特有逻辑
    break;
  case 'thsPcYh':
    console.log('同花顺PC远航版客户端');
    // 执行PC远航版特有逻辑
    break;
  case 'futuresPc':
    console.log('期货通PC客户端');
    // 执行期货通PC特有逻辑
    break;
  case 'ths':
    console.log('同花顺移动端');
    // 执行同花顺移动端逻辑
    break;
  case 'futures':
    console.log('期货通移动端');
    // 执行期货通移动端逻辑
    break;
  case 'weixin':
    console.log('微信环境');
    // 执行微信环境逻辑
    break;
  case 'weibo':
    console.log('微博环境');
    // 执行微博环境逻辑
    break;
  case 'browser':
    console.log('普通浏览器');
    // 执行浏览器环境逻辑
    break;
  default:
    console.log('未知环境');
    // 执行默认逻辑
}

功能适配

ts
import { getApp } from '@fu/matrix';

// 根据应用类型启用不同功能
const initializeApp = () => {
  const appType = getApp();
  
  if (appType === 'thsPcTy' || appType === 'thsPcYh') {
    // PC客户端特有功能
    enableAdvancedFeatures();
    setupDesktopUI();
    configurePCAPIs();
  } else if (appType === 'ths' || appType === 'futures') {
    // 移动端客户端功能
    enableMobileFeatures();
    setupMobileUI();
    configureMobileAPIs();
  } else if (appType === 'weixin') {
    // 微信环境功能
    enableWeixinFeatures();
    setupWeixinUI();
    configureWeixinAPIs();
  } else {
    // 通用功能
    enableBasicFeatures();
    setupStandardUI();
  }
};

权限控制

ts
import { getApp } from '@fu/matrix';

// 根据应用类型控制功能权限
const checkPermission = (feature: string) => {
  const appType = getApp();
  
  const permissions = {
    'thsPcTy': ['all'],
    'thsPcYh': ['all'],
    'futuresPc': ['trading', 'marketData'],
    'ths': ['basic', 'trading'],
    'futures': ['basic', 'trading'],
    'ifind': ['basic', 'research'],
    'ainvest': ['basic'],
    'weixin': ['basic'],
    'weibo': ['basic'],
    'browser': ['basic'],
    'unknown': []
  };
  
  const allowedFeatures = permissions[appType] || [];
  return allowedFeatures.includes('all') || allowedFeatures.includes(feature);
};

// 使用示例
if (checkPermission('trading')) {
  showTradingPanel();
} else {
  showBasicPanel();
}

环境配置

ts
import { getApp } from '@fu/matrix';

// 根据应用类型配置环境
const getEnvironmentConfig = () => {
  const appType = getApp();
  
  const configs = {
    'thsPcTy': {
      apiBaseUrl: 'https://api.ths.com/pc-ty',
      features: ['advancedTrading', 'realTimeData'],
      ui: 'desktop'
    },
    'thsPcYh': {
      apiBaseUrl: 'https://api.ths.com/pc-yh',
      features: ['advancedTrading', 'realTimeData'],
      ui: 'desktop'
    },
    'futuresPc': {
      apiBaseUrl: 'https://api.futures.com/pc',
      features: ['futuresTrading', 'riskManagement'],
      ui: 'desktop'
    },
    'ths': {
      apiBaseUrl: 'https://api.ths.com/mobile',
      features: ['basicTrading'],
      ui: 'mobile'
    },
    'futures': {
      apiBaseUrl: 'https://api.futures.com/mobile',
      features: ['basicTrading'],
      ui: 'mobile'
    },
    'weixin': {
      apiBaseUrl: 'https://api.weixin.com',
      features: ['basic'],
      ui: 'weixin'
    },
    'weibo': {
      apiBaseUrl: 'https://api.weibo.com',
      features: ['basic'],
      ui: 'weibo'
    },
    'browser': {
      apiBaseUrl: 'https://api.browser.com',
      features: ['basic'],
      ui: 'web'
    }
  };
  
  return configs[appType] || configs['browser'];
};

实现原理

该函数通过以下步骤进行检测:

  1. 按优先级顺序调用各个客户端检测方法
  2. 一旦检测到匹配的客户端类型,立即返回对应结果
  3. 如果所有客户端检测都失败,则检测微信/微博环境
  4. 如果微信/微博环境检测也失败,则检测浏览器环境
  5. 如果浏览器环境也不存在,则返回未知环境

兼容性

  • 浏览器支持: 所有现代浏览器
  • Node.js 支持: 在服务端环境中返回 'unknown'
  • 移动端: 支持所有移动端环境检测

注意事项

  1. 检测优先级是固定的,PC客户端优先级最高
  2. 如果多个检测方法都返回true,会返回优先级最高的结果
  3. 在服务端环境中会返回 'unknown'
  4. 检测过程中的异常会被捕获,返回 'unknown'

相关方法

版本历史

  • v1.0.0: 初始版本