|
| 1 | +import {FormInstance} from "antd/es/form/hooks/useForm"; |
| 2 | +import {NamePath} from "rc-field-form/es/interface"; |
| 3 | + |
| 4 | +// 流程表单API 提供get post的能力 |
| 5 | +export interface FlowFormApi { |
| 6 | + get: (url: string, params?: any) => Promise<any>; |
| 7 | + post: (url: string, data: any) => Promise<any>; |
| 8 | +} |
| 9 | + |
| 10 | +// 流程表单验证内容 |
| 11 | +export class FlowFormValidateContent { |
| 12 | + readonly value: any; |
| 13 | + readonly form: FormInstance; |
| 14 | + readonly api?: FlowFormApi |
| 15 | + |
| 16 | + constructor(value: any, form: FormInstance, api?: FlowFormApi) { |
| 17 | + this.value = value; |
| 18 | + this.form = form; |
| 19 | + this.api = api; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// 自定义验证 |
| 24 | +export interface FlowFormCustomValidate { |
| 25 | + name: NamePath; |
| 26 | + validate: (content: FlowFormValidateContent) => Promise<string[]>; |
| 27 | +} |
| 28 | + |
| 29 | +// 流程表单API上下文 |
| 30 | +export class FlowFormApiContext { |
| 31 | + |
| 32 | + private static readonly instance: FlowFormApiContext = new FlowFormApiContext(); |
| 33 | + |
| 34 | + private api: FlowFormApi | undefined; |
| 35 | + |
| 36 | + private constructor() { |
| 37 | + this.api = undefined; |
| 38 | + } |
| 39 | + |
| 40 | + public static getInstance() { |
| 41 | + return FlowFormApiContext.instance; |
| 42 | + } |
| 43 | + |
| 44 | + public setApi(api: FlowFormApi) { |
| 45 | + this.api = api; |
| 46 | + } |
| 47 | + |
| 48 | + public getApi() { |
| 49 | + return this.api; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// 自定义验证上下文 |
| 54 | +export class FlowFormCustomValidateContext { |
| 55 | + |
| 56 | + private readonly map: Map<NamePath, FlowFormCustomValidate>; |
| 57 | + |
| 58 | + constructor() { |
| 59 | + this.map = new Map(); |
| 60 | + } |
| 61 | + |
| 62 | + public addValidate(validate: FlowFormCustomValidate) { |
| 63 | + this.map.set(validate.name, validate); |
| 64 | + } |
| 65 | + |
| 66 | + public addCustomFunctionCodeValidate(namePath:NamePath,validateFuncCode:string){ |
| 67 | + const validateFunc = new Function('content', validateFuncCode); |
| 68 | + this.addValidate({ |
| 69 | + name: namePath, |
| 70 | + validate: async (content) => { |
| 71 | + return validateFunc(content); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + public validate(form: FormInstance) { |
| 77 | + this.map.values().forEach((validate) => { |
| 78 | + const value = form.getFieldValue(validate.name); |
| 79 | + const content = new FlowFormValidateContent(value, form, FlowFormApiContext.getInstance().getApi()); |
| 80 | + validate.validate(content).then((res) => { |
| 81 | + form.setFields( |
| 82 | + [ |
| 83 | + { |
| 84 | + name: validate.name, |
| 85 | + errors: res, |
| 86 | + } |
| 87 | + ] |
| 88 | + ) |
| 89 | + }).catch((error) => { |
| 90 | + form.setFields( |
| 91 | + [ |
| 92 | + { |
| 93 | + name: validate.name, |
| 94 | + errors: [error], |
| 95 | + } |
| 96 | + ] |
| 97 | + ) |
| 98 | + }); |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
0 commit comments