ruoyi-plus-soybean/src/views/_builtin/login/modules/pwd-login.vue

150 lines
4.7 KiB
Vue
Raw Normal View History

2023-11-17 08:45:00 +08:00
<script setup lang="ts">
2024-08-16 16:33:11 +08:00
import { computed, reactive, ref } from 'vue';
import type { SelectOption } from 'naive-ui';
import { useLoading } from '@sa/hooks';
2023-11-17 08:45:00 +08:00
import { $t } from '@/locales';
import { loginModuleRecord } from '@/constants/app';
import { useRouterPush } from '@/hooks/common/router';
2023-12-14 21:45:29 +08:00
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
2023-11-17 08:45:00 +08:00
import { useAuthStore } from '@/store/modules/auth';
2024-08-16 16:33:11 +08:00
import { fetchCaptchaCode, fetchTenantList } from '@/service/api';
2023-11-17 08:45:00 +08:00
defineOptions({
name: 'PwdLogin'
});
const authStore = useAuthStore();
const { toggleLoginModule } = useRouterPush();
const { formRef, validate } = useNaiveForm();
2024-08-16 16:33:11 +08:00
const { loading: codeLoading, startLoading: startCodeLoading, endLoading: endCodeLoading } = useLoading();
2023-11-17 08:45:00 +08:00
2024-08-16 16:33:11 +08:00
const codeUrl = ref<string>();
const captchaEnabled = ref<boolean>(false);
const tenantEnabled = ref<boolean>(false);
const tenantOption = ref<SelectOption[]>([]);
2023-11-17 08:45:00 +08:00
2024-08-16 16:33:11 +08:00
const model: Api.Auth.LoginForm = reactive({
tenantId: '000000',
username: 'admin',
password: 'admin123',
remberMe: false
2023-11-17 08:45:00 +08:00
});
2024-08-16 16:33:11 +08:00
const rules = computed<Record<keyof Api.Auth.LoginForm, App.Global.FormRule[]>>(() => {
2024-03-24 03:02:08 +08:00
// inside computed to make locale reactive, if not apply i18n, you can define it without computed
2024-08-16 16:33:11 +08:00
const { formRules, createRequiredRule } = useFormRules();
2024-01-28 16:24:43 +08:00
2024-08-16 16:33:11 +08:00
const loginRules: Record<keyof Api.Auth.LoginForm, App.Global.FormRule[]> = {
username: formRules.userName,
password: formRules.pwd,
code: captchaEnabled.value ? [createRequiredRule($t('form.code.required'))] : [],
tenantId: tenantEnabled.value ? formRules.tenantId : [],
rememberMe: [],
uuid: []
2024-01-28 16:24:43 +08:00
};
2024-08-16 16:33:11 +08:00
return loginRules;
2024-01-28 16:24:43 +08:00
});
2023-11-17 08:45:00 +08:00
async function handleSubmit() {
await validate();
2024-08-16 16:33:11 +08:00
try {
await authStore.login(model);
} catch (error) {
handleFetchCaptchaCode();
}
2023-11-17 08:45:00 +08:00
}
2024-03-24 15:39:41 +08:00
2024-08-16 16:33:11 +08:00
async function handleFetchTenantList() {
const { data, error } = await fetchTenantList();
if (!error) {
tenantEnabled.value = data.tenantEnabled;
tenantOption.value = data.voList.map(tenant => {
return {
label: tenant.companyName,
value: tenant.tenantId
};
});
}
2024-03-24 15:39:41 +08:00
}
2024-08-16 16:33:11 +08:00
handleFetchTenantList();
2024-03-24 15:39:41 +08:00
2024-08-16 16:33:11 +08:00
async function handleFetchCaptchaCode() {
startCodeLoading();
const { data, error } = await fetchCaptchaCode();
if (!error) {
captchaEnabled.value = data.captchaEnabled;
if (data.captchaEnabled) {
model.uuid = data.uuid;
codeUrl.value = `data:image/gif;base64,${data.img}`;
}
}
endCodeLoading();
2024-03-24 15:39:41 +08:00
}
2024-08-16 16:33:11 +08:00
handleFetchCaptchaCode();
2023-11-17 08:45:00 +08:00
</script>
<template>
<NForm ref="formRef" :model="model" :rules="rules" size="large" :show-label="false">
2024-08-16 16:33:11 +08:00
<NFormItem v-if="tenantEnabled" path="tenantId">
<NSelect v-model:value="model.tenantId" placeholder="请选择/输入公司名称" :options="tenantOption" />
</NFormItem>
<NFormItem path="username">
2024-09-02 09:34:34 +08:00
<NInput v-model:value="model.username" :placeholder="$t('page.login.common.userNamePlaceholder')" />
2023-11-17 08:45:00 +08:00
</NFormItem>
<NFormItem path="password">
<NInput
v-model:value="model.password"
type="password"
2024-03-24 04:03:49 +08:00
show-password-on="click"
2023-11-17 08:45:00 +08:00
:placeholder="$t('page.login.common.passwordPlaceholder')"
/>
</NFormItem>
2024-08-16 16:33:11 +08:00
<NFormItem v-if="captchaEnabled" path="code">
<div class="w-full flex-y-center gap-16px">
<NInput v-model:value="model.code" :placeholder="$t('page.login.common.codePlaceholder')" />
<NSpin :show="codeLoading" :size="28" class="h-42px">
<NButton :focusable="false" class="login-code h-42px w-116px" @click="handleFetchCaptchaCode">
<img v-if="codeUrl" :src="codeUrl" />
<NEmpty v-else :show-icon="false" description="暂无验证码" />
</NButton>
</NSpin>
</div>
</NFormItem>
2023-12-14 21:45:29 +08:00
<NSpace vertical :size="24">
2023-11-17 08:45:00 +08:00
<div class="flex-y-center justify-between">
<NCheckbox>{{ $t('page.login.pwdLogin.rememberMe') }}</NCheckbox>
2024-03-24 03:02:08 +08:00
<NButton quaternary @click="toggleLoginModule('reset-pwd')">
{{ $t('page.login.pwdLogin.forgetPassword') }}
</NButton>
2023-11-17 08:45:00 +08:00
</div>
<NButton type="primary" size="large" round block :loading="authStore.loginLoading" @click="handleSubmit">
2023-11-17 08:45:00 +08:00
{{ $t('common.confirm') }}
</NButton>
<div class="flex-y-center justify-between gap-12px">
<NButton class="flex-1" block @click="toggleLoginModule('code-login')">
{{ $t(loginModuleRecord['code-login']) }}
</NButton>
<NButton class="flex-1" block @click="toggleLoginModule('register')">
{{ $t(loginModuleRecord.register) }}
</NButton>
</div>
</NSpace>
</NForm>
</template>
2024-08-16 16:33:11 +08:00
<style scoped>
.login-code {
&.n-button {
--n-padding: 0 8px !important;
background-color: #c0c0c0;
}
img {
height: 40px;
}
}
</style>