zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { EditorType } from '@/constants';
import _ from 'lodash';
 
export class FormValidator {
  /**
   * 验证表单input-number 不为0
   */
  static validatorNumberNotNull(rule: any, value: any, callback: any) {
    // eslint-disable-next-line eqeqeq
    if (!value) {
      return callback(new Error(rule.message));
    }
    callback();
  }
 
  static validatorRichEditor(rule: any, value: any[], callback: any) {
    // eslint-disable-next-line eqeqeq
    const textEditorItem = value.filter((x) => x.type === EditorType.Text);
    if (textEditorItem.some((x) => !x.content)) {
      return callback(new Error(rule.message));
    }
    callback();
  }
  static validatorWeMap(rule: any, value: WeMapModel, callback: any) {
    // eslint-disable-next-line eqeqeq
    if (_.isEmpty(value) || Object.values(value).some((x) => !x)) {
      return callback(new Error(rule.message));
    }
    callback();
  }
}
 
export async function validateFormList(refs: any) {
  try {
    const validateList = refs.reverse().map((x) => {
      const ref = x as any;
      if (Array.isArray(ref)) {
        return delay().then(() => ref[0].validate());
      }
      return delay().then(() => ref.validate());
    });
    let res = await Promise.allSettled(validateList);
    return res.every((x) => x.status === 'fulfilled');
  } catch (error) {}
}
 
function delay(timeout = 30) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, timeout);
  });
}