import { ValidateErrorEntity } from "ant-design-vue/lib/form/interface";
import { Ref, toRaw, UnwrapRef } from "vue";
let timer: number | null = null
/**
 * 表单验证
 * @param formRef
 * @param formState
 * @returns
 */
export function validateAction<T>(formRef: Ref<any>, formState: UnwrapRef<T>): Promise<T> {
    return formRef.value
        .validate()
        .then(() => {
            const param = toRaw(formState);
            console.log('请求参数：', param);
            if (timer) {
                // 节流 2秒
                const now = new Date().getTime()
                if (now - timer > 2 * 1000) {
                    timer = new Date().getTime()
                    return param
                }
            } else {
                timer = new Date().getTime()
                return param
            }
        })
        .catch((error: ValidateErrorEntity<T>) => {
            console.error('表单验证错误：', error);
            throw new Error('表单验证错误：')
        });
}