useLogin.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import type { FormInstance } from 'ant-design-vue/lib/form/Form';
  2. import type {
  3. RuleObject,
  4. NamePath,
  5. Rule as ValidationRule,
  6. } from 'ant-design-vue/lib/form/interface';
  7. import { ref, computed, unref, Ref } from 'vue';
  8. import { useI18n } from '/@/hooks/web/useI18n';
  9. export enum LoginStateEnum {
  10. LOGIN,
  11. REGISTER,
  12. RESET_PASSWORD,
  13. MOBILE,
  14. QR_CODE,
  15. }
  16. const currentState = ref(LoginStateEnum.LOGIN);
  17. // 这里也可以优化
  18. // import { createGlobalState } from '@vueuse/core'
  19. export function useLoginState() {
  20. function setLoginState(state: LoginStateEnum) {
  21. currentState.value = state;
  22. }
  23. const getLoginState = computed(() => currentState.value);
  24. function handleBackLogin() {
  25. setLoginState(LoginStateEnum.LOGIN);
  26. }
  27. return { setLoginState, getLoginState, handleBackLogin };
  28. }
  29. export function useFormValid<T extends Object = any>(formRef: Ref<FormInstance>) {
  30. const validate = computed(() => {
  31. const form = unref(formRef);
  32. return form?.validate ?? ((_nameList?: NamePath) => Promise.resolve());
  33. });
  34. async function validForm() {
  35. const form = unref(formRef);
  36. if (!form) return;
  37. const data = await form.validate();
  38. return data as T;
  39. }
  40. return { validate, validForm };
  41. }
  42. export function useFormRules(formData?: Recordable) {
  43. const { t } = useI18n();
  44. const getAccountFormRule = computed(() => createRule(t('sys.login.accountPlaceholder')));
  45. const getPasswordFormRule = computed(() => createRule(t('sys.login.passwordPlaceholder')));
  46. const getSmsFormRule = computed(() => createRule(t('sys.login.smsPlaceholder')));
  47. const getMobileFormRule = computed(() => createRule(t('sys.login.mobilePlaceholder')));
  48. const validatePolicy = async (_: RuleObject, value: boolean) => {
  49. return !value ? Promise.reject(t('sys.login.policyPlaceholder')) : Promise.resolve();
  50. };
  51. const validateConfirmPassword = (password: string) => {
  52. return async (_: RuleObject, value: string) => {
  53. if (!value) {
  54. return Promise.reject(t('sys.login.passwordPlaceholder'));
  55. }
  56. if (value !== password) {
  57. return Promise.reject(t('sys.login.diffPwd'));
  58. }
  59. return Promise.resolve();
  60. };
  61. };
  62. const getFormRules = computed((): { [k: string]: ValidationRule | ValidationRule[] } => {
  63. const accountFormRule = unref(getAccountFormRule);
  64. const passwordFormRule = unref(getPasswordFormRule);
  65. const smsFormRule = unref(getSmsFormRule);
  66. const mobileFormRule = unref(getMobileFormRule);
  67. const mobileRule = {
  68. sms: smsFormRule,
  69. mobile: mobileFormRule,
  70. };
  71. switch (unref(currentState)) {
  72. // register form rules
  73. case LoginStateEnum.REGISTER:
  74. return {
  75. account: accountFormRule,
  76. password: passwordFormRule,
  77. confirmPassword: [
  78. { validator: validateConfirmPassword(formData?.password), trigger: 'change' },
  79. ],
  80. policy: [{ validator: validatePolicy, trigger: 'change' }],
  81. ...mobileRule,
  82. };
  83. // reset password form rules
  84. case LoginStateEnum.RESET_PASSWORD:
  85. return {
  86. account: accountFormRule,
  87. ...mobileRule,
  88. };
  89. // mobile form rules
  90. case LoginStateEnum.MOBILE:
  91. return mobileRule;
  92. // login form rules
  93. default:
  94. return {
  95. account: accountFormRule,
  96. password: passwordFormRule,
  97. };
  98. }
  99. });
  100. return { getFormRules };
  101. }
  102. function createRule(message: string): ValidationRule[] {
  103. return [
  104. {
  105. required: true,
  106. message,
  107. trigger: 'change',
  108. },
  109. ];
  110. }