2b69686795
- 新增404页面,优化未找到页面体验,避免被搜索引擎索引 - 增加文件代理接口,隐藏真实文件服务器地址,支持文件请求代理 - 实现/article、/case、/product及/page动态路由兼容列表与详情展示 - 添加动态CMS页面兼容入口处理旧式路径,统一路由与SEO设置 - 新增模板1、模板7、模板2、模板3关于我们页面,实现多模板支持 - 模板增强支持CMS单页内容加载及SEO信息动态设置 - 配置环境变量及Git忽略文件规则辅助开发和构建环境管理
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import { $fetch } from 'ofetch'
|
|
import type { SubscriptionStatus } from '~/app/types'
|
|
|
|
type RuntimeConfig = ReturnType<typeof useRuntimeConfig>
|
|
|
|
// 服务端缓存:租户 → 订阅状态
|
|
const subscriptionCache = new Map<string, { data: SubscriptionStatus; expireAt: number }>()
|
|
|
|
/**
|
|
* 查询应用订阅状态
|
|
* 带缓存,避免每次请求都调用后端
|
|
*/
|
|
export async function getSubscriptionStatus(
|
|
tenantId: string,
|
|
appId: string,
|
|
config: RuntimeConfig
|
|
): Promise<SubscriptionStatus> {
|
|
const cacheKey = `${tenantId}:${appId}`
|
|
const now = Date.now()
|
|
const cacheTtl = (config.subscriptionCacheTtl || 60) * 1000
|
|
|
|
// 检查缓存
|
|
const cached = subscriptionCache.get(cacheKey)
|
|
if (cached && cached.expireAt > now) {
|
|
return cached.data
|
|
}
|
|
|
|
// 查询后端
|
|
const appApiBase = config.public.appApiBase as string
|
|
const defaultResult: SubscriptionStatus = {
|
|
subscribed: true,
|
|
status: 'active',
|
|
expired: false
|
|
}
|
|
|
|
try {
|
|
const res = await $fetch<{ code?: number; data?: SubscriptionStatus }>('/subscription/check', {
|
|
baseURL: `${appApiBase}/api/app`,
|
|
headers: { TenantId: tenantId },
|
|
query: { appId },
|
|
timeout: 5000,
|
|
retry: 1
|
|
})
|
|
|
|
const data = res.data || res
|
|
|
|
const result: SubscriptionStatus = {
|
|
subscribed: data.subscribed ?? true,
|
|
status: data.status || 'active',
|
|
expireTime: data.expireTime,
|
|
startTime: data.startTime,
|
|
expired: data.expired ?? false,
|
|
subscriptionId: data.subscriptionId,
|
|
productId: data.productId,
|
|
productName: data.productName
|
|
}
|
|
|
|
// 写入缓存
|
|
subscriptionCache.set(cacheKey, { data: result, expireAt: now + cacheTtl })
|
|
|
|
return result
|
|
} catch {
|
|
// 接口异常时降级:允许访问(避免后端故障导致所有站点不可用)
|
|
return defaultResult
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 清除订阅缓存
|
|
*/
|
|
export function clearSubscriptionCache(tenantId?: string, appId?: string) {
|
|
if (tenantId && appId) {
|
|
subscriptionCache.delete(`${tenantId}:${appId}`)
|
|
} else {
|
|
subscriptionCache.clear()
|
|
}
|
|
}
|