| | |
| | | import type { ValidationRule } from 'ant-design-vue/lib/form/Form'; |
| | | import type { RuleObject } from 'ant-design-vue/lib/form/interface'; |
| | | import type { FormInstance } from 'ant-design-vue/lib/form/Form'; |
| | | import type { |
| | | RuleObject, |
| | | NamePath, |
| | | Rule as ValidationRule, |
| | | } from 'ant-design-vue/lib/form/interface'; |
| | | import { ref, computed, unref, Ref } from 'vue'; |
| | | import { useI18n } from '/@/hooks/web/useI18n'; |
| | | import { useI18n } from '@/hooks/web/useI18n'; |
| | | |
| | | export enum LoginStateEnum { |
| | | LOGIN, |
| | |
| | | } |
| | | |
| | | const currentState = ref(LoginStateEnum.LOGIN); |
| | | |
| | | // 这里也可以优化 |
| | | // import { createGlobalState } from '@vueuse/core' |
| | | |
| | | export function useLoginState() { |
| | | function setLoginState(state: LoginStateEnum) { |
| | |
| | | return { setLoginState, getLoginState, handleBackLogin }; |
| | | } |
| | | |
| | | export function useFormValid<T extends Object = any>(formRef: Ref<any>) { |
| | | export function useFormValid<T extends Object = any>(formRef: Ref<FormInstance>) { |
| | | const validate = computed(() => { |
| | | const form = unref(formRef); |
| | | return form?.validate ?? ((_nameList?: NamePath) => Promise.resolve()); |
| | | }); |
| | | |
| | | async function validForm() { |
| | | const form = unref(formRef); |
| | | if (!form) return; |
| | |
| | | return data as T; |
| | | } |
| | | |
| | | return { validForm }; |
| | | return { validate, validForm }; |
| | | } |
| | | |
| | | export function useFormRules(formData?: Recordable) { |
| | |
| | | |
| | | const getAccountFormRule = computed(() => createRule(t('sys.login.accountPlaceholder'))); |
| | | const getPasswordFormRule = computed(() => createRule(t('sys.login.passwordPlaceholder'))); |
| | | // const getDeptFormRule = computed(() => createRule(t('sys.login.deptPlaceholder'))); |
| | | const getDeptFormRule = computed(() => createRule(t('请选择据点'))); |
| | | const getSmsFormRule = computed(() => createRule(t('sys.login.smsPlaceholder'))); |
| | | const getMobileFormRule = computed(() => createRule(t('sys.login.mobilePlaceholder'))); |
| | | |
| | |
| | | return Promise.reject(t('sys.login.passwordPlaceholder')); |
| | | } |
| | | if (value !== password) { |
| | | return Promise.reject(t('账号或密码错误')); |
| | | // return Promise.reject(t('sys.login.diffPwd')); |
| | | return Promise.reject(t('sys.login.diffPwd')); |
| | | } |
| | | return Promise.resolve(); |
| | | }; |
| | |
| | | const getFormRules = computed((): { [k: string]: ValidationRule | ValidationRule[] } => { |
| | | const accountFormRule = unref(getAccountFormRule); |
| | | const passwordFormRule = unref(getPasswordFormRule); |
| | | const deptFormRule = unref(getDeptFormRule); |
| | | const smsFormRule = unref(getSmsFormRule); |
| | | const mobileFormRule = unref(getMobileFormRule); |
| | | |
| | |
| | | return { |
| | | account: accountFormRule, |
| | | password: passwordFormRule, |
| | | dept:deptFormRule, |
| | | confirmPassword: [ |
| | | { validator: validateConfirmPassword(formData?.password), trigger: 'change' }, |
| | | ], |
| | |
| | | return { |
| | | account: accountFormRule, |
| | | password: passwordFormRule, |
| | | // dept:deptFormRule, |
| | | }; |
| | | } |
| | | }); |
| | | return { getFormRules }; |
| | | } |
| | | |
| | | function createRule(message: string) { |
| | | function createRule(message: string): ValidationRule[] { |
| | | return [ |
| | | { |
| | | required: true, |