gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<script setup lang="ts">
import type { Recordable } from '..\..\..\..\..\types\src';
 
import type { VbenFormSchema } from '..\..\..\..\..\@core\ui-kit\form-ui\src';
 
import type { AuthenticationProps } from './types';
 
import { computed, onMounted, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
 
import { $t } from '..\..\..\..\..\locales\src';
 
import { useVbenForm } from '..\..\..\..\..\@core\ui-kit\form-ui\src';
import { VbenButton, VbenCheckbox } from '..\..\..\..\..\@core\ui-kit\shadcn-ui\src';
 
import Title from './auth-title.vue';
import DocLink from './doc-link.vue';
import ThirdPartyLogin from './third-party-login.vue';
 
interface Props extends AuthenticationProps {
  formSchema?: VbenFormSchema[];
}
 
defineOptions({
  name: 'AuthenticationLogin',
});
 
const props = withDefaults(defineProps<Props>(), {
  codeLoginPath: '/auth/code-login',
  forgetPasswordPath: '/auth/forget-password',
  formSchema: () => [],
  loading: false,
  qrCodeLoginPath: '/auth/qrcode-login',
  registerPath: '/auth/register',
  showCodeLogin: true,
  showForgetPassword: true,
  showQrcodeLogin: true,
  showRegister: true,
  showRememberMe: true,
  showThirdPartyLogin: true,
  submitButtonText: '',
  subTitle: '',
  title: '',
});
 
const emit = defineEmits<{
  submit: [Recordable<any>];
  thirdLogin: [type: number];
}>();
 
const [Form, formApi] = useVbenForm(
  reactive({
    commonConfig: {
      hideLabel: true,
      hideRequiredMark: true,
    },
    schema: computed(() => props.formSchema),
    showDefaultActions: false,
  }),
);
const router = useRouter();
 
const REMEMBER_ME_KEY = `REMEMBER_ME_USERNAME_${location.hostname}`;
 
const localUsername = localStorage.getItem(REMEMBER_ME_KEY) || '';
 
const rememberMe = ref(!!localUsername);
 
async function handleSubmit() {
  const { valid } = await formApi.validate();
  const values = await formApi.getValues();
  if (valid) {
    localStorage.setItem(
      REMEMBER_ME_KEY,
      rememberMe.value ? values?.username : '',
    );
    emit('submit', values);
  }
}
 
function handleGo(path: string) {
  router.push(path);
}
 
/**
 * 处理第三方登录
 *
 * @param type 第三方平台类型
 */
function handleThirdLogin(type: number) {
  emit('thirdLogin', type);
}
 
onMounted(() => {
  if (localUsername) {
    formApi.setFieldValue('username', localUsername);
  }
});
 
defineExpose({
  getFormApi: () => formApi,
});
</script>
 
<template>
  <div @keydown.enter.prevent="handleSubmit">
    <slot name="title">
      <Title>
        <slot name="title">
          {{ title || `${$t('authentication.welcomeBack')} 👋🏻` }}
        </slot>
        <template #desc>
          <span class="text-muted-foreground">
            <slot name="subTitle">
              {{ subTitle || $t('authentication.loginSubtitle') }}
            </slot>
          </span>
        </template>
      </Title>
    </slot>
 
    <Form />
 
    <div
      v-if="showRememberMe || showForgetPassword"
      class="mb-6 flex justify-between"
    >
      <div class="flex-center">
        <VbenCheckbox
          v-if="showRememberMe"
          v-model="rememberMe"
          name="rememberMe"
        >
          {{ $t('authentication.rememberMe') }}
        </VbenCheckbox>
      </div>
 
      <span
        v-if="showForgetPassword"
        class="vben-link text-sm font-normal"
        @click="handleGo(forgetPasswordPath)"
      >
        {{ $t('authentication.forgetPassword') }}
      </span>
    </div>
    <VbenButton
      :class="{
        'cursor-wait': loading,
      }"
      :loading="loading"
      aria-label="login"
      class="w-full"
      @click="handleSubmit"
    >
      {{ submitButtonText || $t('common.login') }}
    </VbenButton>
 
    <div
      v-if="showCodeLogin || showQrcodeLogin"
      class="mt-4 mb-2 flex items-center justify-between"
    >
      <VbenButton
        v-if="showCodeLogin"
        class="w-1/2"
        variant="outline"
        @click="handleGo(codeLoginPath)"
      >
        {{ $t('authentication.mobileLogin') }}
      </VbenButton>
      <VbenButton
        v-if="showQrcodeLogin"
        class="ml-4 w-1/2"
        variant="outline"
        @click="handleGo(qrCodeLoginPath)"
      >
        {{ $t('authentication.qrcodeLogin') }}
      </VbenButton>
    </div>
 
    <!-- 第三方登录 -->
    <slot name="third-party-login">
      <ThirdPartyLogin
        v-if="showThirdPartyLogin"
        @third-login="handleThirdLogin"
      />
    </slot>
 
    <slot name="to-register">
      <div v-if="showRegister" class="mt-3 text-center text-sm">
        {{ $t('authentication.accountTip') }}
        <span
          class="vben-link text-sm font-normal"
          @click="handleGo(registerPath)"
        >
          {{ $t('authentication.createAccount') }}
        </span>
      </div>
    </slot>
 
    <!-- 萌新必读 -->
    <DocLink />
  </div>
</template>