246 lines
6.7 KiB
Vue
246 lines
6.7 KiB
Vue
<template>
|
|
<LoginFormTitle v-show="getShow" class="enter-x" />
|
|
<Form
|
|
class="p-4 enter-x"
|
|
:model="formData"
|
|
:rules="getFormRules"
|
|
ref="formRef"
|
|
v-show="getShow"
|
|
@keypress.enter="handleLogin('/')"
|
|
>
|
|
<FormItem name="account" class="enter-x">
|
|
<Input
|
|
size="large"
|
|
v-model:value="formData.account"
|
|
:placeholder="t('sys.login.userName')"
|
|
class="fix-auto-fill"
|
|
/>
|
|
</FormItem>
|
|
<FormItem name="password" class="enter-x">
|
|
<InputPassword
|
|
size="large"
|
|
visibilityToggle
|
|
v-model:value="formData.password"
|
|
:placeholder="t('sys.login.password')"
|
|
/>
|
|
</FormItem>
|
|
|
|
<FormItem name="identifyCode" class="enter-x">
|
|
<div class="flex items-center w-full">
|
|
<div class="flex-1">
|
|
<Input
|
|
size="large"
|
|
v-model:value="formData.identifyCode"
|
|
placeholder="请输入验证码"
|
|
class="!w-full !min-w-full"
|
|
/>
|
|
</div>
|
|
|
|
<div @click="refreshCode">
|
|
<Security class="ml-1 flex-none" :identifyCode="identifyCode" />
|
|
</div>
|
|
</div>
|
|
</FormItem>
|
|
|
|
<!-- <ARow class="enter-x">
|
|
<ACol :span="12">
|
|
<FormItem>
|
|
|
|
<Checkbox v-model:checked="rememberMe" size="small">
|
|
{{ t('sys.login.rememberMe') }}
|
|
</Checkbox>
|
|
</FormItem>
|
|
</ACol>
|
|
<ACol :span="12">
|
|
<FormItem :style="{ 'text-align': 'right' }">
|
|
|
|
<Button type="link" size="small" @click="setLoginState(LoginStateEnum.RESET_PASSWORD)">
|
|
{{ t('sys.login.forgetPassword') }}
|
|
</Button>
|
|
</FormItem>
|
|
</ACol>
|
|
</ARow> -->
|
|
|
|
<FormItem class="enter-x">
|
|
<Button size="large" block @click="handleLogin('/v')" :loading="loading"> 登录大屏 </Button>
|
|
<Button
|
|
type="primary"
|
|
class="mt-4"
|
|
size="large"
|
|
block
|
|
@click="handleLogin('/')"
|
|
:loading="loading"
|
|
>
|
|
登录后台
|
|
</Button>
|
|
<!-- <Button size="large" class="mt-4 enter-x" block @click="handleRegister">
|
|
{{ t('sys.login.registerButton') }}
|
|
</Button> -->
|
|
</FormItem>
|
|
<!-- <ARow class="enter-x">
|
|
<ACol :md="8" :xs="24">
|
|
<Button block @click="setLoginState(LoginStateEnum.MOBILE)">
|
|
{{ t('sys.login.mobileSignInFormTitle') }}
|
|
</Button>
|
|
</ACol>
|
|
<ACol :md="8" :xs="24" class="!my-2 !md:my-0 xs:mx-0 md:mx-2">
|
|
<Button block @click="setLoginState(LoginStateEnum.QR_CODE)">
|
|
{{ t('sys.login.qrSignInFormTitle') }}
|
|
</Button>
|
|
</ACol>
|
|
<ACol :md="6" :xs="24">
|
|
<Button block @click="setLoginState(LoginStateEnum.REGISTER)">
|
|
{{ t('sys.login.registerButton') }}
|
|
</Button>
|
|
</ACol>
|
|
</ARow> -->
|
|
|
|
<!-- <Divider class="enter-x">{{ t('sys.login.otherSignIn') }}</Divider> -->
|
|
|
|
<!-- <div class="flex justify-evenly enter-x" :class="`${prefixCls}-sign-in-way`">
|
|
<GithubFilled />
|
|
<WechatFilled />
|
|
<AlipayCircleFilled />
|
|
<GoogleCircleFilled />
|
|
<TwitterCircleFilled />
|
|
</div> -->
|
|
</Form>
|
|
</template>
|
|
<script lang="ts" setup>
|
|
import Security from '/@/components/Security/index.vue'
|
|
import { reactive, ref, unref, computed, onBeforeMount } from 'vue'
|
|
import md5 from 'js-md5'
|
|
import { Form, Input, Button } from 'ant-design-vue'
|
|
|
|
import LoginFormTitle from './LoginFormTitle.vue'
|
|
|
|
import { useI18n } from '/@/hooks/web/useI18n'
|
|
import { useMessage } from '/@/hooks/web/useMessage'
|
|
|
|
import { useUserStore } from '/@/store/modules/user'
|
|
import { LoginStateEnum, useLoginState, useFormRules, useFormValid } from './useLogin'
|
|
import { useDesign } from '/@/hooks/web/useDesign'
|
|
import { useRoute } from 'vue-router'
|
|
//import { onKeyStroke } from '@vueuse/core';
|
|
|
|
const route = useRoute()
|
|
|
|
const FormItem = Form.Item
|
|
const InputPassword = Input.Password
|
|
const { t } = useI18n()
|
|
const { notification, createErrorModal } = useMessage()
|
|
const { prefixCls } = useDesign('login')
|
|
const userStore = useUserStore()
|
|
|
|
const { setLoginState, getLoginState } = useLoginState()
|
|
const { getFormRules } = useFormRules()
|
|
|
|
const formRef = ref()
|
|
const loading = ref(false)
|
|
const rememberMe = ref(false)
|
|
const identifyCode = ref('')
|
|
const formData = reactive({
|
|
account: '',
|
|
password: '',
|
|
identifyCode: '',
|
|
})
|
|
|
|
const { validForm } = useFormValid(formRef)
|
|
|
|
//onKeyStroke('Enter', handleLogin);
|
|
const { username, userpwd, userToken } = route.query
|
|
|
|
const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
|
|
|
|
const autoLogin = ref(!!userToken || (!!username && !!userpwd))
|
|
onBeforeMount(() => {
|
|
if (userToken) {
|
|
autoLoginByToken(userToken)
|
|
}
|
|
if (username && userpwd) {
|
|
autoLoginByUser(username, userpwd)
|
|
}
|
|
})
|
|
|
|
function autoLoginByToken(token) {
|
|
try {
|
|
userStore.setToken(token)
|
|
userStore.afterLoginAction()
|
|
} catch (error) {
|
|
console.log(error)
|
|
}
|
|
}
|
|
|
|
function autoLoginByUser(username, userpwd) {
|
|
const { redirect } = route.query
|
|
let e = redirect
|
|
if (!e) {
|
|
e = '/'
|
|
}
|
|
userStore.login({
|
|
password: md5(userpwd),
|
|
username: username,
|
|
mode: 'none', //不要默认的错误提示
|
|
homeRouter: e,
|
|
})
|
|
}
|
|
|
|
function randomNum(min, max) {
|
|
return Math.floor(Math.random() * (max - min) + min)
|
|
}
|
|
|
|
function refreshCode() {
|
|
formData.identifyCode = ''
|
|
makeCode()
|
|
}
|
|
|
|
makeCode()
|
|
|
|
function makeCode(l = 4) {
|
|
const identifyCodes = '1234567890abcdef'
|
|
let code = ''
|
|
|
|
for (let i = 0; i < l; i++) {
|
|
code += identifyCodes[randomNum(0, identifyCodes.length)]
|
|
}
|
|
identifyCode.value = code
|
|
}
|
|
|
|
async function handleLogin(e) {
|
|
const data = await validForm()
|
|
if (!data) return
|
|
if (data.identifyCode.toLowerCase() !== identifyCode.value.toLowerCase()) {
|
|
createErrorModal({
|
|
title: '错误提示',
|
|
content: '验证码不正确',
|
|
})
|
|
return
|
|
}
|
|
|
|
try {
|
|
loading.value = true
|
|
const userInfo = await userStore.login({
|
|
password: md5(data.password),
|
|
username: data.account,
|
|
mode: 'none', //不要默认的错误提示
|
|
homeRouter: e,
|
|
})
|
|
if (userInfo) {
|
|
notification.success({
|
|
message: t('sys.login.loginSuccessTitle'),
|
|
description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.name}`,
|
|
duration: 3,
|
|
})
|
|
}
|
|
} catch (error) {
|
|
createErrorModal({
|
|
title: t('sys.api.errorTip'),
|
|
content: (error as unknown as Error).message || t('sys.api.networkExceptionMsg'),
|
|
getContainer: () => document.body.querySelector(`.${prefixCls}`) || document.body,
|
|
})
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
</script>
|