|
|
@@ -1,105 +1,146 @@
|
|
|
<script setup lang="ts">
|
|
|
import type { FormInstance, FormRules } from 'element-plus'
|
|
|
import useUserStore from '@/store/modules/user'
|
|
|
-import { ref, useTemplateRef } from 'vue'
|
|
|
+import { ref, useTemplateRef, onMounted } from 'vue'
|
|
|
|
|
|
defineOptions({
|
|
|
name: 'LoginForm',
|
|
|
})
|
|
|
|
|
|
const props = defineProps<{
|
|
|
- account?: string
|
|
|
+ userName?: string
|
|
|
+ passWord?: string
|
|
|
+ captcha?: string
|
|
|
+ system?: string
|
|
|
+ captchaId?: number
|
|
|
}>()
|
|
|
|
|
|
const emits = defineEmits<{
|
|
|
- onLogin: [account: string]
|
|
|
- onRegister: [account: string]
|
|
|
- onResetPassword: [account: string]
|
|
|
+ onLogin: [userName: string]
|
|
|
+ onRegister: [userName: string]
|
|
|
+ onResetPassword: [userName: string]
|
|
|
}>()
|
|
|
|
|
|
const userStore = useUserStore()
|
|
|
-
|
|
|
const title = import.meta.env.VITE_APP_TITLE
|
|
|
+const baseurl = import.meta.env.VITE_APP_API_BASEURL
|
|
|
const loading = ref(false)
|
|
|
-
|
|
|
+const remember = ref(false)
|
|
|
+const captchaSrc = ref("")//验证码图片
|
|
|
+// const system = ref([
|
|
|
+// 'PLATFORM',
|
|
|
+// 'JZG'
|
|
|
+// ])
|
|
|
// 登录方式,default 账号密码登录,qrcode 扫码登录
|
|
|
const type = ref('default')
|
|
|
const formRef = useTemplateRef<FormInstance>('formRef')
|
|
|
const form = ref({
|
|
|
- account: props.account ?? localStorage.login_account ?? '',
|
|
|
- password: '',
|
|
|
- remember: !!localStorage.login_account,
|
|
|
+ userName: props.userName ?? localStorage.login_account ?? '',
|
|
|
+ passWord: '',
|
|
|
+ captcha: "",
|
|
|
+ system: "PLATFORM",
|
|
|
+ captchaId: 0,
|
|
|
})
|
|
|
const rules = ref<FormRules>({
|
|
|
- account: [
|
|
|
+ userName: [
|
|
|
{ required: true, trigger: 'blur', message: '请输入用户名' },
|
|
|
],
|
|
|
- password: [
|
|
|
+ passWord: [
|
|
|
{ required: true, trigger: 'blur', message: '请输入密码' },
|
|
|
{ min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' },
|
|
|
],
|
|
|
})
|
|
|
+onMounted(() => {
|
|
|
+ refreshCaptcha()
|
|
|
+})
|
|
|
+//获取验证码接口
|
|
|
+function refreshCaptcha() {
|
|
|
+ let randnum = Math.floor(Math.random() * (1000000 - 1 + 1)) + 1;
|
|
|
+ captchaSrc.value =
|
|
|
+ baseurl +
|
|
|
+ "auth/captchaById?t=" +
|
|
|
+ new Date().getTime() +
|
|
|
+ "&captchaId=" +
|
|
|
+ randnum;
|
|
|
+ form.value.captchaId = randnum;
|
|
|
+}
|
|
|
function handleLogin() {
|
|
|
formRef.value?.validate((valid) => {
|
|
|
if (valid) {
|
|
|
loading.value = true
|
|
|
userStore.login(form.value).then(() => {
|
|
|
- if (form.value.remember) {
|
|
|
- localStorage.setItem('login_account', form.value.account)
|
|
|
+ if (remember) {
|
|
|
+ localStorage.setItem('login_account', form.value.userName)
|
|
|
}
|
|
|
else {
|
|
|
localStorage.removeItem('login_account')
|
|
|
}
|
|
|
- emits('onLogin', form.value.account)
|
|
|
+ emits('onLogin', form.value.userName)
|
|
|
}).finally(() => {
|
|
|
loading.value = false
|
|
|
+ refreshCaptcha();
|
|
|
+
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-function testAccount(account: string) {
|
|
|
- form.value.account = account
|
|
|
- form.value.password = '123456'
|
|
|
+function testAccount(userName: string) {
|
|
|
+ form.value.userName = userName
|
|
|
+ form.value.passWord = '123456'
|
|
|
handleLogin()
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<template>
|
|
|
- <ElForm ref="formRef" :model="form" :rules="rules" class="min-h-500px w-full flex-col-stretch-center p-12">
|
|
|
+ <ElForm ref="formRef" :model="form" :rules="rules" class="min-h-500px w-full flex-col-stretch-center p-12"
|
|
|
+ size="large">
|
|
|
<div class="mb-6">
|
|
|
- <HTabList
|
|
|
- v-model="type" :options="[
|
|
|
- { label: '账号密码登录', value: 'default' },
|
|
|
- { label: '扫码登录', value: 'qrcode' },
|
|
|
- ]"
|
|
|
- />
|
|
|
+ <HTabList v-model="type" :options="[
|
|
|
+ { label: '账号密码登录', value: 'default' },
|
|
|
+ { label: '扫码登录', value: 'qrcode' },
|
|
|
+ ]" />
|
|
|
</div>
|
|
|
<template v-if="type === 'default'">
|
|
|
<h3 class="mb-8 text-xl color-[var(--el-text-color-primary)] font-bold">
|
|
|
欢迎使用 {{ title }} ! 👋🏻
|
|
|
</h3>
|
|
|
<div>
|
|
|
- <ElFormItem prop="account">
|
|
|
- <ElInput v-model="form.account" size="large" placeholder="用户名" type="text" tabindex="1">
|
|
|
+ <ElFormItem prop="userName">
|
|
|
+ <ElInput v-model="form.userName" placeholder="用户名" type="text" tabindex="1">
|
|
|
<template #prefix>
|
|
|
<SvgIcon name="i-ri:user-3-fill" />
|
|
|
</template>
|
|
|
</ElInput>
|
|
|
</ElFormItem>
|
|
|
- <ElFormItem prop="password">
|
|
|
- <ElInput v-model="form.password" type="password" size="large" placeholder="密码" tabindex="2" show-password @keyup.enter="handleLogin">
|
|
|
+ <ElFormItem prop="passWord">
|
|
|
+ <ElInput v-model="form.passWord" type="password" placeholder="密码" tabindex="2" show-password
|
|
|
+ @keyup.enter="handleLogin">
|
|
|
<template #prefix>
|
|
|
<SvgIcon name="i-ri:lock-2-fill" />
|
|
|
</template>
|
|
|
</ElInput>
|
|
|
</ElFormItem>
|
|
|
+
|
|
|
+ <ElFormItem prop="captcha">
|
|
|
+ <ElCol :md="14" :sm="14" :xs="14">
|
|
|
+ <ElInput v-model="form.captcha" placeholder="验证码" type="text" tabindex="3">
|
|
|
+ <template #prefix>
|
|
|
+ <SvgIcon name="i-ri:user-3-fill" />
|
|
|
+ </template>
|
|
|
+ </ElInput>
|
|
|
+ </ElCol>
|
|
|
+ <ElCol :md="10" :sm="10" :xs="10" flex>
|
|
|
+ <img style="width: 100%; height: 32px; margin-left: 20px;" class="pointer" :src="captchaSrc"
|
|
|
+ @click="refreshCaptcha" />
|
|
|
+ </ElCol>
|
|
|
+ </ElFormItem>
|
|
|
</div>
|
|
|
<div class="mb-4 flex-center-between">
|
|
|
- <ElCheckbox v-model="form.remember">
|
|
|
- 记住我
|
|
|
+ <ElCheckbox v-model="remember">
|
|
|
+ 记住密码
|
|
|
</ElCheckbox>
|
|
|
- <ElLink type="primary" :underline="false" @click="emits('onResetPassword', form.account)">
|
|
|
+ <ElLink type="primary" :underline="false" @click="emits('onResetPassword', form.userName)">
|
|
|
忘记密码了?
|
|
|
</ElLink>
|
|
|
</div>
|
|
|
@@ -108,7 +149,7 @@ function testAccount(account: string) {
|
|
|
</ElButton>
|
|
|
<div class="mt-4 flex-center gap-2 text-sm color-[var(--el-text-color-secondary)]">
|
|
|
还没有帐号?
|
|
|
- <ElLink type="primary" :underline="false" @click="emits('onRegister', form.account)">
|
|
|
+ <ElLink type="primary" :underline="false" @click="emits('onRegister', form.userName)">
|
|
|
创建新帐号
|
|
|
</ElLink>
|
|
|
</div>
|