Skip to content

isFuturesPcClient

描述

判断当前环境是否为期货通PC客户端。通过检测 window.external.IsQhtWin 方法来判断是否为期货通PC客户端。

语法

ts
isFuturesPcClient(): boolean

返回值

  • boolean: 如果是期货通PC客户端返回 true,否则返回 false

异常

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

示例

基本用法

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

if (isFuturesPcClient()) {
  // 在期货通PC客户端中的处理逻辑
  console.log('当前在期货通PC客户端中');
  // 可以使用期货通特有的功能
} else {
  // 在其他环境中的处理逻辑
  console.log('当前不在期货通PC客户端中');
}

客户端功能检测

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

// 检测期货通客户端特有功能
const checkFuturesFeatures = () => {
  if (isFuturesPcClient()) {
    return {
      canAccessFuturesAPI: true,
      canUseTradingTools: true,
      canAccessMarketData: true,
      canUseRiskManagement: true
    };
  }
  
  return {
    canAccessFuturesAPI: false,
    canUseTradingTools: false,
    canAccessMarketData: false,
    canUseRiskManagement: false
  };
};

界面适配

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

// 根据客户端类型调整界面
const renderTradingInterface = () => {
  if (isFuturesPcClient()) {
    // 期货通专用交易界面
    return (
      <div className="futures-trading-interface">
        <TradingPanel />
        <RiskManagementPanel />
        <MarketDataPanel />
        <OrderManagementPanel />
      </div>
    );
  }
  
  // 通用界面
  return (
    <div className="general-interface">
      <BasicTradingPanel />
    </div>
  );
};

错误处理

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

const safeFuturesOperation = () => {
  try {
    if (isFuturesPcClient()) {
      // 执行期货通特有操作
      return performFuturesOperation();
    }
    return performGeneralOperation();
  } catch (error) {
    console.warn('期货通操作失败:', error);
    return null;
  }
};

多客户端检测

ts
import { isFuturesPcClient, isThsPcTyClient, isThsPcYhClient } from '@fu/matrix';

const detectClientType = () => {
  if (isFuturesPcClient()) {
    return '期货通PC客户端';
  }
  if (isThsPcTyClient()) {
    return '同花顺PC统一客户端';
  }
  if (isThsPcYhClient()) {
    return '同花顺PC远航版客户端';
  }
  return '其他环境';
};

console.log(`检测到的客户端类型: ${detectClientType()}`);

实现原理

该函数通过检测 window.external 对象及其 IsQhtWin 方法来判断当前环境。期货通PC客户端会在全局 window 对象上提供 external 接口,其中包含 IsQhtWin 方法用于标识期货通环境。

兼容性

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

注意事项

  1. 该函数依赖于浏览器环境,在服务端渲染时会返回 false
  2. 如果 window.external 对象不存在或 IsQhtWin 方法不可用,函数会返回 false
  3. 该检测方法仅适用于期货通PC客户端
  4. 期货通环境可能同时存在 HevoCef 对象,因此 isThsPcYhClient 会排除期货通环境

相关方法

版本历史

  • v1.0.0: 初始版本