Files
dy_web/src/views/login/index.vue
13684185576 ce2cc79be3 修改
2025-11-13 20:55:41 +08:00

387 lines
9.1 KiB
Vue

<template>
<div class="login-container">
<el-form class="login-form" ref="loginFromRef" :model="loginForm" :rules="loginRules" @submit.native.prevent>
<div class="title-container">
<h3 class="title">用户登录</h3>
</div>
<el-form-item prop="userName">
<span class="svg-container">
<svg-icon icon="user" />
</span>
<el-input placeholder="请输入账号" name="userName" type="text" v-model="loginForm.userName" />
</el-form-item>
<el-form-item prop="password1">
<span class="svg-container">
<svg-icon icon="password" />
</span>
<el-input placeholder="请输入密码" name="password" :type="passwordType" v-model="loginForm.password" />
<span class="show-pwd">
<svg-icon :icon="passwordType === 'password' ? 'eye' : 'eye-open'" @click="onChangePwdType" />
</span>
</el-form-item>
<el-form-item prop="type">
<div>
<span class="svg-container">
<el-icon :size="18"><Avatar /></el-icon>
</span>
<el-radio-group v-model="loginForm.type">
<el-radio label="01">民警</el-radio>
<el-radio label="02">辅警</el-radio>
</el-radio-group>
</div>
</el-form-item>
<el-form-item v-if="isShowKaptCha" prop="kaptcha">
<span class="svg-container">
<svg-icon icon="kaptcha" />
</span>
<el-input placeholder="请输入验证码" name="kaptcha" type="text" v-model="loginForm.kaptcha"
@keydown.enter="handleLogin()" />
<span @click="getKaptchaImg">
<el-image class="show-kaptcha" :src="kaptchaUrl" fit="cover">
<template #error>
<div class="image-slot">
<svg-icon icon="errorImg" />
</div>
</template>
</el-image>
</span>
</el-form-item>
<!---登录按钮-->
<el-form-item style="height: 49px" v-if="!loginDialog">
<el-button type="primary" style="width: 520px; height: 49px" :loading="loading" @click="handleLogin"
native-type="submit">登录
</el-button>
</el-form-item>
<el-form-item class="choosedept-wrap" v-if="loginDialog">
<el-select v-model="deptId" @change="refreshToken" placeholder="请选择部门">
<el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId">
</el-option>
</el-select>
</el-form-item>
</el-form>
<!-- <el-dialog
v-model="loginDialog"
title="请选择部门"
width="30%"
:before-close="handleClose"
>
<div>
<el-select v-model="deptId" @change="refreshToken" placeholder="Select">
<el-option
v-for="item in deptList"
:key="item.deptId"
:label="item.deptName"
:value="item.deptId"
>
</el-option>
</el-select>
</div>
</el-dialog> -->
</div>
</template>
<script>
export default {
name: "login",
};
</script>
<script setup>
import { ElNotification } from "element-plus";
import * as MOSTY from "@/components/MyComponents/index";
import { getKaptcha } from "@/api/sys";
import { onMounted, ref, onUnmounted } from "vue";
import { validatePassword } from "./rules";
import { useStore } from "vuex";
import { useRouter, onBeforeRouteLeave } from "vue-router";
const store = useStore();
const kaptchaUrl = ref("");
// 数据源
const loginForm = ref({
userName: "",
password: "",
kaptcha: "",
type: '02'
});
const loginDialog = ref(false);
const deptList = ref([]);
const deptId = ref("");
const authorization = ref("");
const isShowKaptCha = ref(false);
// 验证规则
const loginRules = ref({
userName: [
{
required: true,
trigger: "blur",
message: "用户名为必填项"
}
],
password: [
{
required: true,
trigger: "blur",
validator: validatePassword()
}
],
type: [
{
required: true,
trigger: "blur",
message: "请选择类型"
}
],
kaptcha: [
{
required: true,
trigger: "blur",
message: "验证码为必填项"
}
]
});
const handleClose = () => { };
// onBeforeRouteLeave((to, from, next) => {
// if (to.name == "lz/home") {
// store.commit("user/setKeepLiiveRoute", "lz/home");
// }
// next();
// });
const refreshToken = (e) => {
store
.dispatch("user/refreshToken", {
deptId: e,
jwtToken: authorization.value
})
.then((res) => {
loading.value = false;
store.commit("user/setDeptId", e);
// 登录后操作
// return false //调试好后删除
// router.push("/");
window.location.href = '/'
})
.catch(() => {
loading.value = false;
});
};
// 处理密码框文本显示状态
const passwordType = ref("password");
const onChangePwdType = () => {
if (passwordType.value === "password") {
passwordType.value = "text";
} else {
passwordType.value = "password";
}
};
// 登录动作处理
const loading = ref(false);
const loginFromRef = ref(null);
const router = useRouter();
const handleLogin = () => {
loginFromRef.value.validate((valid) => {
if (!valid) return false;
loading.value = true;
console.log(loginForm.value,'loginForm.value')
store.dispatch("user/login", loginForm.value).then((res) => {
// loading.value = false;
// // 登录后操作
// // return false //调试好后删除
// if (res.deptList.length === 1) {
// window.location.href = '/'
// } else {
// deptList.value = [...res.deptList];
// loginDialog.value = true;
// authorization.value = res.jwtToken;
// ElNotification({
// title: "提示",
// message: "请选择部门",
// duration: 3000
// });
// }
})
.catch(() => {
loading.value = false;
});
});
};
const logout = () => {
store.dispatch("user/logout");
};
onMounted(() => {
// logout();
// getKaptchaImg();
});
const getKaptchaImg = () => {
const res =
`${process.env.VUE_APP_GATEWAY_BASE_URL}/mosty-api/mosty-base/kaptcha?date=` +
new Date();
kaptchaUrl.value = res;
};
</script>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;
.login-container {
min-height: 100%;
width: 100%;
background-color: $bg;
overflow: hidden;
.login-form {
position: relative;
width: 520px;
max-width: 100%;
padding: 160px 35px 0;
margin: 0 auto;
overflow: hidden;
&::v-deep .el-input__inner {
-webkit-box-shadow: 0 0 0 1000px #283443 inset;
-webkit-text-fill-color: #ffffff !important;
}
&::v-deep .el-form-item {
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
color: #454545;
}
&::v-deep .el-input {
display: inline-block;
height: 47px;
width: 85%;
input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: $light_gray;
height: 47px;
caret-color: $cursor;
}
}
}
.tips {
font-size: 16px;
line-height: 28px;
color: #fff;
margin-bottom: 10px;
span {
&:first-of-type {
margin-right: 16px;
}
}
}
.svg-container {
padding: 6px 5px 6px 15px;
color: $dark_gray;
vertical-align: middle;
display: inline-block;
}
.title-container {
position: relative;
.title {
font-size: 26px;
color: $light_gray;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
::v-deep .lang-select {
position: absolute;
top: 4px;
right: 0;
background-color: white;
font-size: 22px;
padding: 4px;
border-radius: 4px;
cursor: pointer;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $dark_gray;
cursor: pointer;
user-select: none;
}
.show-kaptcha {
width: 80px;
height: 40px;
overflow: hidden;
position: absolute;
right: 6px;
top: 3px;
font-size: 16px;
color: $dark_gray;
cursor: pointer;
user-select: none;
display: flex;
align-items: center;
justify-content: center;
font-size: 22px;
}
.choosedept-wrap {
.el-form-item__content {
width: 100%;
.el-select {
width: 100%;
}
}
::v-deep .el-input {
width: 100%;
}
}
// .el-dialog{
// background: #293444;
// color: white;
// .el-dialog__title{
// color: #fff;
// }
// .el-select{
// width: 300px;
// padding-bottom: 40px;
// }
// }
}
::v-deep .el-radio-group {
margin-left: 10px;
}
::v-deep .el-radio__original {
display: none !important; /* 隐藏原生 radio 输入,但仍然允许交互 */
}
::v-deep .el-radio:focus:not(.is-focus):not(:active):not(.is-disabled) .el-radio__inner {
box-shadow: none !important;
}
</style>