返回 AiToEarn
platformLimits.ts
1 /**
2 * platformLimits - 平台参数限制计算工具
3 * 根据选中的平台组合,取各限制字段的最小值
4 */
5
6 import type { PlatType } from '@/app/config/platConfig'
7 import { getPlatformInfoSync } from '@/store/platformMetadata'
8
9 /** 有效限制(null 表示所有选中平台都没有该限制) */
10 export interface EffectiveLimits {
11 titleMax: number | null
12 desMax: number | null
13 topicMax: number | null
14 imagesMax: number | null
15 }
16
17 /** 带来源信息的限制详情 */
18 export interface LimitDetail {
19 value: number
20 limitedBy: PlatType
21 }
22
23 /** 带来源的有效限制 */
24 export type EffectiveLimitsDetailed = Record<keyof EffectiveLimits, LimitDetail | null>
25
26 /**
27 * 计算选中平台的有效参数限制(取最小值)
28 * 无选中平台时返回全 null(无限制)
29 */
30 export function calcEffectiveLimits(platforms: PlatType[]): EffectiveLimits {
31 if (platforms.length === 0) {
32 return { titleMax: null, desMax: null, topicMax: null, imagesMax: null }
33 }
34
35 const detailed = calcEffectiveLimitsDetailed(platforms)
36 return {
37 titleMax: detailed.titleMax?.value ?? null,
38 desMax: detailed.desMax?.value ?? null,
39 topicMax: detailed.topicMax?.value ?? null,
40 imagesMax: detailed.imagesMax?.value ?? null,
41 }
42 }
43
44 /**
45 * 计算选中平台的有效参数限制(带来源平台信息)
46 * 记录是哪个平台施加了最严限制
47 */
48 export function calcEffectiveLimitsDetailed(platforms: PlatType[]): EffectiveLimitsDetailed {
49 const result: EffectiveLimitsDetailed = {
50 titleMax: null,
51 desMax: null,
52 topicMax: null,
53 imagesMax: null,
54 }
55
56 if (platforms.length === 0)
57 return result
58
59 for (const plat of platforms) {
60 const info = getPlatformInfoSync(plat)
61 if (!info)
62 continue
63
64 const config = info.commonPubParamsConfig
65
66 if (typeof config.titleMax === 'number' && config.titleMax > 0) {
67 if (result.titleMax === null || config.titleMax < result.titleMax.value) {
68 result.titleMax = { value: config.titleMax, limitedBy: plat }
69 }
70 }
71
72 if (result.desMax === null || config.desMax < (result.desMax?.value ?? Infinity)) {
73 result.desMax = { value: config.desMax, limitedBy: plat }
74 }
75
76 if (config.topicMax !== undefined) {
77 if (result.topicMax === null || config.topicMax < result.topicMax.value) {
78 result.topicMax = { value: config.topicMax, limitedBy: plat }
79 }
80 }
81
82 if (config.imagesMax !== undefined) {
83 if (result.imagesMax === null || config.imagesMax < result.imagesMax.value) {
84 result.imagesMax = { value: config.imagesMax, limitedBy: plat }
85 }
86 }
87 }
88
89 return result
90 }
91
91 lines TYPESCRIPT