Ben Lin
2024-07-04 d582f081e148c5df7c1b334a94fa1df1cd2655a9
src/views/sys/login/useLogin.ts
@@ -1,7 +1,11 @@
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,
@@ -12,6 +16,9 @@
}
const currentState = ref(LoginStateEnum.LOGIN);
// 这里也可以优化
// import { createGlobalState } from '@vueuse/core'
export function useLoginState() {
  function setLoginState(state: LoginStateEnum) {
@@ -27,7 +34,12 @@
  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;
@@ -35,7 +47,7 @@
    return data as T;
  }
  return { validForm };
  return { validate, validForm };
}
export function useFormRules(formData?: Recordable) {
@@ -43,8 +55,6 @@
  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')));
@@ -58,8 +68,7 @@
        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();
    };
@@ -68,7 +77,6 @@
  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);
@@ -82,7 +90,6 @@
        return {
          account: accountFormRule,
          password: passwordFormRule,
          dept:deptFormRule,
          confirmPassword: [
            { validator: validateConfirmPassword(formData?.password), trigger: 'change' },
          ],
@@ -106,14 +113,13 @@
        return {
          account: accountFormRule,
          password: passwordFormRule,
          // dept:deptFormRule,
        };
    }
  });
  return { getFormRules };
}
function createRule(message: string) {
function createRule(message: string): ValidationRule[] {
  return [
    {
      required: true,