155 lines
4.9 KiB
Vue
Raw Normal View History

2023-03-04 09:16:33 +08:00
<template>
<el-form ref="loginFormRef" class="form" :model="loginForm" :rules="loginRules" size="large">
2023-03-04 09:16:33 +08:00
<el-form-item prop="account">
2023-04-25 10:48:27 +08:00
<el-input v-model="loginForm.account" placeholder="请输入账号">
2023-03-04 09:16:33 +08:00
<template #prefix>
<img src="@/assets/images/login/accountIcon.png" alt="" />
2023-03-04 09:16:33 +08:00
</template>
</el-input>
</el-form-item>
<el-form-item prop="showPassword">
2023-03-15 14:15:22 +08:00
<el-input
type="password"
v-model="loginForm.showPassword"
2023-04-25 10:48:27 +08:00
placeholder="请输入密码"
2023-03-15 14:15:22 +08:00
show-password
autocomplete="new-password"
>
2023-03-04 09:16:33 +08:00
<template #prefix>
2023-03-24 19:36:11 +08:00
<!-- <el-icon class="el-input__icon"><lock /></el-icon> -->
<img src="@/assets/images/login/lockIcon.png" alt="" />
2023-03-04 09:16:33 +08:00
</template>
</el-input>
</el-form-item>
</el-form>
<!-- <div class="login-btn"> -->
2023-03-04 09:16:33 +08:00
<div class="login-btn">
<!-- <el-button :icon="CircleClose" round @click="resetForm(loginFormRef)" size="large">重置</el-button>
2023-03-04 09:16:33 +08:00
<el-button :icon="UserFilled" round @click="login(loginFormRef)" size="large" type="primary" :loading="loading">
登录
</el-button> -->
<el-button @click="login(loginFormRef)" size="large" :loading="loading"> 登录 </el-button>
2023-03-04 09:16:33 +08:00
</div>
2023-03-15 14:15:22 +08:00
<div class="logon">
2023-03-24 19:36:11 +08:00
<el-button type="info" style="color: #2246b4" @click="companyLogon" link>企业注册</el-button>
<el-button style="margin: 0 40px; color: #2246b4" type="info" link @click="projectLogon">项目注册</el-button>
2023-03-15 14:15:22 +08:00
</div>
2023-03-04 09:16:33 +08:00
</template>
2023-04-25 10:48:27 +08:00
<script setup lang="ts" name="logonPage">
2023-03-15 14:15:22 +08:00
import { ref, reactive, onMounted, onBeforeUnmount } from "vue";
2023-03-04 09:16:33 +08:00
import { useRouter } from "vue-router";
import { Login } from "@/api/interface";
import { ElMessage, ElNotification } from "element-plus";
2023-03-04 09:16:33 +08:00
import { loginApi } from "@/api/modules/login";
import { GlobalStore } from "@/stores";
import { TabsStore } from "@/stores/modules/tabs";
import { KeepAliveStore } from "@/stores/modules/keepAlive";
import { getTimeState } from "@/utils/util";
2023-04-25 10:48:27 +08:00
import { HOME_URL } from "@/enums/Home";
2023-03-04 09:16:33 +08:00
import { initDynamicRouter } from "@/routers/modules/dynamicRouter";
// import { CircleClose, UserFilled } from "@element-plus/icons-vue";
2023-03-04 09:16:33 +08:00
import type { ElForm } from "element-plus";
const router = useRouter();
const tabsStore = TabsStore();
const keepAlive = KeepAliveStore();
const globalStore = GlobalStore();
// 定义 formRef校验规则
type FormInstance = InstanceType<typeof ElForm>;
const loginFormRef = ref<FormInstance>();
const loginRules = reactive({
account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
showPassword: [{ required: true, message: "请输入密码", trigger: "blur" }]
});
const loading = ref(false);
2023-05-25 17:46:14 +08:00
const loginForm = reactive<Login.ReqLoginForm>({ account: "", showPassword: "" });
2023-03-04 09:16:33 +08:00
const login = (formEl: FormInstance | undefined) => {
if (!formEl) return;
2023-03-24 19:36:11 +08:00
formEl.validate(async (valid, params) => {
2023-03-04 09:16:33 +08:00
if (!valid) return;
loading.value = true;
try {
const arr = ref(["/government", "/home", "/home", "/home"]);
2023-03-04 09:16:33 +08:00
// 1.执行登录接口
const { result } = await loginApi({ ...loginForm, showPassword: loginForm.showPassword });
2023-03-24 19:36:11 +08:00
globalStore.setToken(result.token);
globalStore.setAccount(result.account);
2023-03-27 14:17:30 +08:00
globalStore.setAccountType(result.accountType);
2023-04-25 10:48:27 +08:00
globalStore.setProjectDateAuth(result.projectDateAuth);
globalStore.setIsManager(result.isManager); //我已知晓
2023-03-27 14:17:30 +08:00
2023-03-04 09:16:33 +08:00
// 2.添加动态路由
2023-03-24 19:36:11 +08:00
// await initDynamicRouter();
// router.push(arr[result.accountType - 1]);
2023-03-04 09:16:33 +08:00
// 3.清空 tabs、keepAlive 保留的数据
tabsStore.closeMultipleTab();
keepAlive.setKeepAliveName();
if (result.accountType === 1) {
await initDynamicRouter();
router.push(arr.value[result.accountType - 1]);
} else {
router.push(arr.value[result.accountType - 1]);
}
router.go(0);
// router.push(arr.value[result.accountType - 1]);
2023-03-04 09:16:33 +08:00
// 4.跳转到首页
2023-03-24 19:36:11 +08:00
// ElNotification({
// title: getTimeState(),
// message: "欢迎登录",
// type: "success",
// duration: 3000
// });
// return router.go(0);
2023-03-04 09:16:33 +08:00
} finally {
loading.value = false;
}
});
};
// resetForm
const resetForm = (formEl: FormInstance | undefined) => {
if (!formEl) return;
formEl.resetFields();
};
2023-03-15 14:15:22 +08:00
const projectLogon = () => {
router.push("/projectlogon");
};
2023-03-24 19:36:11 +08:00
const companyLogon = () => {
router.push("/compLogon");
};
2023-03-04 09:16:33 +08:00
onMounted(() => {
// console.log(import.meta.env.VITE_API_URL);
2023-03-17 09:22:28 +08:00
2023-03-04 09:16:33 +08:00
// 监听enter事件调用登录
document.onkeydown = (e: any) => {
e = window.event || e;
if (e.code === "Enter" || e.code === "enter" || e.code === "NumpadEnter") {
if (loading.value) return;
login(loginFormRef.value);
}
};
});
// login页面销毁时需要去掉enter监听
onBeforeUnmount(() => {
2023-03-15 14:15:22 +08:00
document.onkeydown = null;
});
2023-03-04 09:16:33 +08:00
</script>
<style scoped lang="scss">
@import "../index.scss";
.form :deep(.el-input__wrapper) {
box-shadow: none;
border-bottom: 1px solid #b5b5b5;
border-radius: 0;
padding: 0;
}
2023-03-04 09:16:33 +08:00
</style>