66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
// 开通页面地址(public/web/subscribe.html)
|
||
const SUBSCRIBE_PAGE_URL = '/web/subscribe.html';
|
||
|
||
// 路由路径与 assetId 的映射关系
|
||
const ROUTE_ASSET_MAP: Record<string, { assetId: string; serviceName: string }> = {
|
||
// CID广告业务(聚合广告)
|
||
'/cidService': { assetId: '696f423705e496ba4ccbe665', serviceName: '聚合广告' },
|
||
|
||
// AI客服业务
|
||
'/customerService': { assetId: '696f421205e496ba4ccbe662', serviceName: 'AI客服' },
|
||
|
||
// 聚合电商业务(资产管理)
|
||
'/assets': { assetId: '696b4acd1be1c8b76c4b4c15', serviceName: '资产管理' },
|
||
};
|
||
|
||
/**
|
||
* 根据路由路径获取对应的 assetId 和服务名称
|
||
*/
|
||
export function getAssetInfoByRoute(routePath: string): { assetId: string; serviceName: string } | null {
|
||
// 精确匹配
|
||
if (ROUTE_ASSET_MAP[routePath]) {
|
||
return ROUTE_ASSET_MAP[routePath];
|
||
}
|
||
|
||
// 前缀匹配
|
||
for (const [prefix, info] of Object.entries(ROUTE_ASSET_MAP)) {
|
||
if (routePath.startsWith(prefix)) {
|
||
return info;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 跳转到外部开通页面
|
||
* @param assetId 资产ID
|
||
*/
|
||
export function redirectToSubscribePage(assetId: string) {
|
||
// 当前页面地址作为返回地址
|
||
const returnUrl = encodeURIComponent(window.location.href);
|
||
// 构建跳转URL
|
||
const url = `${SUBSCRIBE_PAGE_URL}?assetId=${assetId}&returnUrl=${returnUrl}`;
|
||
console.log('[redirectToSubscribePage] 跳转到开通页面:', url);
|
||
window.location.href = url;
|
||
}
|
||
|
||
/**
|
||
* 处理 402 错误码(模块未开通)
|
||
*/
|
||
export function handleModuleNotEnabled(routePath: string): boolean {
|
||
console.log('[模块未开通] 当前路由路径:', routePath);
|
||
const assetInfo = getAssetInfoByRoute(routePath);
|
||
console.log('[模块未开通] 匹配到的资产信息:', assetInfo);
|
||
|
||
if (assetInfo) {
|
||
redirectToSubscribePage(assetInfo.assetId);
|
||
return true;
|
||
}
|
||
|
||
// 如果没有匹配到路由,尝试使用默认的资产管理
|
||
console.warn('[模块未开通] 未匹配到路由,使用默认资产管理');
|
||
redirectToSubscribePage('696b4acd1be1c8b76c4b4c15');
|
||
return true;
|
||
}
|