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';
|
2025-04-23 16:02:41 +08:00
|
|
|
import { fetchCaptchaCode, fetchTenantList } from '@/service/api';
|
2025-04-23 17:28:03 +08:00
|
|
|
// import { fetchGetConfigDetail } from '@/service/api/system/config';
|
2025-04-23 16:02:41 +08:00
|
|
|
import { useAuthStore } from '@/store/modules/auth';
|
2023-11-17 08:45:00 +08:00
|
|
|
import { useRouterPush } from '@/hooks/common/router';
|
2023-12-14 21:45:29 +08:00
|
|
|
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
|
2024-09-07 18:27:39 +08:00
|
|
|
import { localStg } from '@/utils/storage';
|
2025-04-23 16:02:41 +08:00
|
|
|
import { $t } from '@/locales';
|
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);
|
2025-04-23 17:28:03 +08:00
|
|
|
const registerEnabled = ref<boolean>(false);
|
2024-09-03 12:19:57 +08:00
|
|
|
const remberMe = ref<boolean>(false);
|
2024-08-16 16:33:11 +08:00
|
|
|
const tenantOption = ref<SelectOption[]>([]);
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-09-03 12:19:57 +08:00
|
|
|
const model: Api.Auth.PwdLoginForm = reactive({
|
2024-08-16 16:33:11 +08:00
|
|
|
tenantId: '000000',
|
2024-09-07 18:27:39 +08:00
|
|
|
username: '',
|
|
|
|
password: ''
|
2023-11-17 08:45:00 +08:00
|
|
|
});
|
|
|
|
|
2024-09-03 12:19:57 +08:00
|
|
|
type RuleKey = Extract<keyof Api.Auth.PwdLoginForm, 'username' | 'password' | 'code' | 'tenantId'>;
|
|
|
|
|
|
|
|
const rules = computed<Record<RuleKey, 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-09-03 12:19:57 +08:00
|
|
|
const loginRules: Record<RuleKey, App.Global.FormRule[]> = {
|
2024-08-16 16:33:11 +08:00
|
|
|
username: formRules.userName,
|
|
|
|
password: formRules.pwd,
|
|
|
|
code: captchaEnabled.value ? [createRequiredRule($t('form.code.required'))] : [],
|
2024-09-03 12:19:57 +08:00
|
|
|
tenantId: tenantEnabled.value ? formRules.tenantId : []
|
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-09-07 18:27:39 +08:00
|
|
|
// 勾选了需要记住密码设置在 localStorage 中设置记住用户名和密码
|
|
|
|
if (remberMe.value) {
|
|
|
|
const { tenantId, username, password } = model;
|
|
|
|
localStg.set('loginRember', { tenantId, username, password });
|
|
|
|
} else {
|
|
|
|
// 否则移除
|
|
|
|
localStg.remove('loginRember');
|
|
|
|
}
|
2024-08-16 16:33:11 +08:00
|
|
|
try {
|
|
|
|
await authStore.login(model);
|
2024-11-04 11:33:21 +08:00
|
|
|
} catch {
|
2024-08-16 16:33:11 +08:00
|
|
|
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();
|
2024-09-07 18:27:39 +08:00
|
|
|
if (error) return;
|
|
|
|
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();
|
2024-09-07 18:27:39 +08:00
|
|
|
|
|
|
|
function handleLoginRember() {
|
|
|
|
const loginRember = localStg.get('loginRember');
|
|
|
|
if (!loginRember) return;
|
|
|
|
remberMe.value = true;
|
|
|
|
Object.assign(model, loginRember);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLoginRember();
|
2025-04-23 17:28:03 +08:00
|
|
|
|
|
|
|
// async function handleRegister() {
|
|
|
|
// const { data, error } = await fetchGetConfigDetail('sys.account.registerUser');
|
|
|
|
// if (error) return;
|
|
|
|
// registerEnabled.value = data.configValue === 'true';
|
|
|
|
// }
|
|
|
|
|
|
|
|
// handleRegister();
|
|
|
|
|
|
|
|
function handleSocialLogin(type: string) {
|
|
|
|
console.log(type);
|
|
|
|
}
|
2023-11-17 08:45:00 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2024-10-03 16:38:43 +08:00
|
|
|
<NForm ref="formRef" :model="model" :rules="rules" size="large" :show-label="false" @keyup.enter="handleSubmit">
|
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>
|
2025-04-23 17:28:03 +08:00
|
|
|
<NSpace vertical :size="16" class="mb-8px">
|
|
|
|
<div class="mx-6px flex-y-center justify-between">
|
2024-09-03 12:19:57 +08:00
|
|
|
<NCheckbox v-model:checked="remberMe">{{ $t('page.login.pwdLogin.rememberMe') }}</NCheckbox>
|
2025-04-23 17:28:03 +08:00
|
|
|
<NSpace :size="1">
|
|
|
|
<ButtonIcon class="color-#44b549" icon="ic:outline-wechat" @click="handleSocialLogin('wechat_enterprise')" />
|
|
|
|
<ButtonIcon local-icon="topiam" @click="handleSocialLogin('topiam')" />
|
|
|
|
<ButtonIcon local-icon="maxkey" @click="handleSocialLogin('maxkey')" />
|
|
|
|
<ButtonIcon class="color-#c71d23" icon="simple-icons:gitee" @click="handleSocialLogin('gitee')" />
|
|
|
|
<ButtonIcon class="color-#010409" icon="mdi:github" @click="handleSocialLogin('github')" />
|
|
|
|
</NSpace>
|
2023-11-17 08:45:00 +08:00
|
|
|
</div>
|
2024-09-07 18:27:39 +08:00
|
|
|
<NButton type="primary" size="large" block :loading="authStore.loginLoading" @click="handleSubmit">
|
|
|
|
{{ $t('common.login') }}
|
2023-11-17 08:45:00 +08:00
|
|
|
</NButton>
|
2025-04-23 17:28:03 +08:00
|
|
|
<NButton v-if="registerEnabled" size="large" block @click="toggleLoginModule('register')">
|
|
|
|
{{ $t('page.login.common.register') }}
|
|
|
|
</NButton>
|
2023-11-17 08:45:00 +08:00
|
|
|
</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>
|