import { requestJson } from './api.uts' import { wanAmountValid, wanToCents } from './money.uts' export type FinancingPurpose = { type: string, code: string, label: string, requiresDetail: boolean } // 用途由服务端发布;加载失败时提示重试,不用本机猜测字典后提交。 export async function fetchFinancingPurposes(): Promise { const result = await requestJson('/dictionaries', 'GET', {}) const versions = result['assessmentInputVersions'] as number[] | null const minimum = result['minNewAssessmentInputVersion'] as number | null if (versions == null || !versions.includes(2) || minimum == null || minimum > 2) throw new Error('评估版本已更新,请刷新页面后重试') const dictionary = result['financingPurposes'] as UTSJSONObject | null if (dictionary == null) throw new Error('资金用途暂不可用,请重试') const items = dictionary['items'] as UTSJSONObject[] | null if (items == null) throw new Error('资金用途暂不可用,请重试') const purposes: FinancingPurpose[] = [] items.forEach((item: UTSJSONObject) => { if (item['enabled'] == true) purposes.push({ type: item['type'] as string, code: item['code'] as string, label: item['label'] as string, requiresDetail: item['requiresDetail'] == true } as FinancingPurpose) }) if (purposes.length == 0) throw new Error('资金用途暂不可用,请重试') return purposes } export function demandValidationMessage(amount: string, term: string, code: string, detail: string): string { if (!wanAmountValid(amount) || wanToCents(amount) <= 0) return '请填写大于零的融资金额(万元,最多六位小数)' if (!/^[1-9][0-9]{0,3}$/.test(term)) return '融资期限请输入1至9999的整数月' if (code.length == 0) return '请选择资金用途' if (code == 'other' && (detail.trim().length < 2 || detail.trim().length > 200 || /[<>\x00-\x1f\x7f]/.test(detail))) return '其他用途请填写2至200字说明,不含标签或控制字符' if (code != 'other' && detail.length > 0) return '仅其他用途需要填写说明' return '' }