285 lines
6.7 KiB
Vue
Raw Normal View History

2023-01-29 10:17:49 +08:00
<!-- eslint-disable no-unused-vars -->
<!--
* @Author: Kane
* @Date: 2022-12-14 15:23:54
* @LastEditors: Kane
2023-02-03 17:16:46 +08:00
* @LastEditTime: 2023-02-03 15:46:55
* @FilePath: \IT工具综合平台\src\views\account\Login32.vue
2023-01-29 10:17:49 +08:00
* @Description:
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
-->
<template>
<div id="login">
<div class="form-warp">
<ul class="menu-tab">
2023-02-03 17:16:46 +08:00
<li :class="{ 'current': ui.current_menu === item.type }" @click="onToggleMenu(item.type)"
v-for="item in tab_menu" :key="item.type">{{ item.label }}
2023-01-29 10:17:49 +08:00
</li>
</ul>
<!-- <el-form ref="form" :model="form"> -->
<el-form ref="form">
<el-form-item>
<label class="form-label">用户名</label>
<el-input type="text" v-model.lazy.trim="loginForm.username"></el-input>
</el-form-item>
<el-form-item>
<label class="form-label">密码</label>
<el-input type="password" v-model.lazy.trim="loginForm.password"></el-input>
</el-form-item>
2023-02-03 17:16:46 +08:00
<el-form-item v-show="ui.current_menu === tab_menu[1].type">
2023-01-29 10:17:49 +08:00
<label class="form-label">确认密码</label>
<el-input type="password" disabled v-model.lazy.trim="loginForm.confirm_password"></el-input>
</el-form-item>
<el-form-item>
<label class="form-label">验证码</label>
<el-row :gutter="10">
<el-col :span="14">
<el-input type="text" disabled></el-input>
</el-col>
<el-col :span="10">
<el-button type="danger" disabled class="el-button-block" @click="getValidateCode()">获取验证码</el-button>
</el-col>
</el-row>
</el-form-item>
<el-form-item>
<el-button type="primary" class="el-button-block" @click="login" :disabled="submit_btn_disable"
:loading="submit_btn_loading">
2023-02-03 17:16:46 +08:00
{{ ui.current_menu === "login" ? "登录" : "注册" }}
2023-01-29 10:17:49 +08:00
</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
2023-02-03 17:16:46 +08:00
import { reactive, onBeforeMount, onMounted } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
2023-01-29 10:17:49 +08:00
import { Login } from "@/utils/api/info/account";
import { ElMessage } from "element-plus";
2023-02-03 17:16:46 +08:00
//import router from "../../router/index";
2023-01-29 10:17:49 +08:00
export default {
name: "loginPage",
2023-02-03 17:16:46 +08:00
setup()
2023-01-29 10:17:49 +08:00
{
2023-02-03 17:16:46 +08:00
const store = useStore();
const router = useRouter();
const loginForm = reactive({
username: "",
password: "",
confirm_password: "",
validateCode: "",
});
const tab_menu = reactive(
[
2023-01-29 10:17:49 +08:00
{ type: "login", label: "登录" },
{ type: "regiester", label: "注册" },
2023-02-03 17:16:46 +08:00
]);
const ui = reactive(
{
current_menu: "",
staffInfo: null,
submit_btn_disable: false,
submit_btn_loading: false,
});
const onToggleMenu = (type) =>
2023-01-29 10:17:49 +08:00
{
2023-02-03 17:16:46 +08:00
ui.current_menu = type;
2023-01-29 10:17:49 +08:00
console.log(process.env.VUE_APP_API_URL_LOGIN);
2023-02-03 17:16:46 +08:00
};
const getValidateCode = () =>
2023-01-29 10:17:49 +08:00
{
ElMessage({
message: "测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字",
center: true,
type: "error",
});
2023-02-03 17:16:46 +08:00
};
2023-01-29 10:17:49 +08:00
/**
* 登录
*/
2023-02-03 17:16:46 +08:00
const login = () =>
2023-01-29 10:17:49 +08:00
{
2023-02-03 17:16:46 +08:00
if (loginForm.username.length === 0 || loginForm.password === 0)
2023-01-29 10:17:49 +08:00
{
ElMessage({
message: "请填写您的P13账号和密码",
type: "error",
});
return;
}
2023-02-03 17:16:46 +08:00
ui.submit_btn_disable = true;
ui.submit_btn_loading = true;
2023-01-29 10:17:49 +08:00
const userInfo = {
2023-02-03 17:16:46 +08:00
p13account: loginForm.username,
password: loginForm.password,
2023-01-29 10:17:49 +08:00
};
Login(userInfo)
.then((response) =>
{
//成功获取到返回值时的响应函数,要判断返回值的成功标志
//验证成功将获取到的token和用户信息保存到vuex和localStoreage
//然后router.push进行路由跳转到控制台
const data = response.data;
//判断是否成功,显示提示信息
if (data.success === true)
{
ElMessage({
message: data.message,
type: "success",
center: true,
});
2023-02-03 17:16:46 +08:00
ui.staffInfo = data.staffInfo;
2023-01-29 10:17:49 +08:00
//保存用户信息和token
2023-02-03 17:16:46 +08:00
saveUserInfo(data);
2023-01-29 10:17:49 +08:00
//验证成功,跳转路由
router.push("/Desktop");
}
else
{
//验证失败
ElMessage({
message: data.message,
type: "error",
center: true,
});
2023-02-03 17:16:46 +08:00
ui.submit_btn_disable = false;
ui.submit_btn_loading = false;
2023-01-29 10:17:49 +08:00
}
})
.catch((error) =>
{
//没有获取到响应数据
console.log(error);
ElMessage({
message: error.message,
type: "error",
center: true,
});
2023-02-03 17:16:46 +08:00
ui.submit_btn_disable = false;
ui.submit_btn_loading = false;
2023-01-29 10:17:49 +08:00
});
2023-02-03 17:16:46 +08:00
};
//将获取到的用户信息和token保存到vuex和localStorage
const saveUserInfo = (userInfo) =>
2023-01-29 10:17:49 +08:00
{
//保存到vuex
2023-02-03 17:16:46 +08:00
store.commit("app/SET_USERINFO", userInfo);
2023-01-29 10:17:49 +08:00
//保存到localStorage
const token = userInfo.token;
const userInfoJson = JSON.stringify(userInfo);
window.localStorage.setItem("token", token);
window.localStorage.setItem("user_info", userInfoJson);
2023-02-03 17:16:46 +08:00
};
onBeforeMount(() =>
{
//初始化菜单选项
ui.current_menu = tab_menu[0].type;
});
onMounted(() =>
{
//清理状态
store.state.app.userInfo = null;
});
return {
//数据
ui,
loginForm,
tab_menu,
//方法
onToggleMenu,
saveUserInfo,
login,
getValidateCode,
};
2023-01-29 10:17:49 +08:00
},
};
</script>
<style scoped>
#login {
height: 100vh;
background-color: #344a5f;
padding-top: 50px;
}
.form-warp {
width: 320px;
padding: 30px;
margin: auto;
background-color: #ffffff10;
border-radius: 5px;
}
/*.menu-tab {
text-align: center;
margin-bottom: 15px;
li {
display: inline-block;
padding: 10px 24px;
margin: 0 10px;
color: #fff;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
&.current {
background-color: rgba(0, 0, 0, 0.1);
}
}
}*/
.menu-tab {
text-align: center;
margin-bottom: 15px;
}
.menu-tab li {
display: inline-block;
padding: 10px 24px;
margin: 0 10px;
color: #fff;
font-size: 14px;
border-radius: 5px;
cursor: pointer;
}
.menu-tab .current {
background-color: rgba(0, 0, 0, 0.1);
}
.form-label {
display: block;
color: #fff;
font-size: 14px;
}
.el-button-block {
width: 100%;
}
</style>