工厂函数&ts类型应用

30 阅读1分钟
// 路由映射工厂函数 - 支持多环境配置
export const ROUTE_MAP = (a_id?: string) => {
  // 环境检测
  const isProduction = config.env.VITE_APP_ENV === 'online';

  // 动态生成链接
  const aUrl = isProduction
    ? `https://xxx.net/detail?a_id=${a_id}`
    : `https://xxx-text.net/detail?a_id=${a_id}`;

  return {
    lock: "/m/lock",
    release: "/m/release",
    viewApply: aUrl, // 动态生成的链接
    suspend: "/m/suspend",
    schedule: "/m/schedule",
    recovery: "/m/recovery",
    edit: "/m/edit",
  };
};

// 安全的参数传递
const handleActionClick = (event: string, record: any) => {
  const productVersionId = record.product_version_id;
  const aId = record.a_id;

  // 通过安全的路由映射传递敏感参数
  const routeMap = ROUTE_MAP(aId);
  const route = routeMap[event as keyof typeof routeMap]
  // "我相信 event 一定是 lock、release 或 viewApply 中的一个

  if (event === 'viewApply') {
    // 外部跳转,参数已包含在脱敏URL中
    window.open(route, '_blank');
  } else {
    // 内部路由,只传递非敏感ID
    navigate(`${route}?id=${productVersionId}`);
  }
}