2024-07-17 22:33:59 +00:00
|
|
|
<template>
|
|
|
|
<div class="loginBox">
|
|
|
|
<div class="loginFrom">
|
|
|
|
<!-- <div class="inputBox">
|
|
|
|
账号<input type="text" v-model="user.username" />
|
|
|
|
</div>
|
|
|
|
<div class="inputBox">
|
|
|
|
密码<input type="password" v-model="user.password" />
|
|
|
|
</div>
|
|
|
|
<div class="checkBox">
|
|
|
|
<el-checkbox v-model="remember"></el-checkbox>记住密码
|
|
|
|
</div> -->
|
|
|
|
<el-form >
|
|
|
|
<el-form-item label="账号" prop="username" >
|
|
|
|
<el-input style="width: 200px;height: 30px;border-radius: 15px;" class="inputBox" type="medium" v-model="user.username" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item label="密码" prop="password" >
|
|
|
|
<el-input style="width: 200px;height: 30px;border-radius: 15px;" v-model="user.password" type="password" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item>
|
|
|
|
<el-checkbox v-model="remember">记住密码</el-checkbox>
|
|
|
|
</el-form-item>
|
|
|
|
</el-form>
|
|
|
|
<div class="btnBox">
|
|
|
|
<el-button size="large" type="success" round @click="toLogin"
|
|
|
|
>登录</el-button
|
|
|
|
>
|
|
|
|
<el-button
|
|
|
|
size="large"
|
|
|
|
type="danger"
|
|
|
|
round
|
|
|
|
@click="dialogFormVisible = true"
|
|
|
|
>注册</el-button
|
|
|
|
>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<el-dialog v-model="dialogFormVisible" title="注册新用户" width="500">
|
|
|
|
<el-form
|
|
|
|
ref="ruleFormRef"
|
|
|
|
style="max-width: 600px"
|
|
|
|
:model="ruleForm"
|
|
|
|
status-icon
|
|
|
|
:rules="rules"
|
|
|
|
label-width="auto"
|
|
|
|
class="demo-ruleForm"
|
|
|
|
>
|
|
|
|
<el-form-item label="账号" prop="username">
|
|
|
|
<el-input v-model="ruleForm.username" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item label="密码" prop="password">
|
|
|
|
<el-input
|
|
|
|
v-model="ruleForm.password"
|
|
|
|
type="password"
|
|
|
|
autocomplete="off"
|
|
|
|
/>
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item label="确定密码" prop="password2">
|
|
|
|
<el-input
|
|
|
|
v-model="ruleForm.password2"
|
|
|
|
type="password"
|
|
|
|
autocomplete="off"
|
|
|
|
/>
|
|
|
|
</el-form-item>
|
|
|
|
</el-form>
|
|
|
|
<template #footer>
|
|
|
|
<div class="dialog-footer">
|
|
|
|
<el-button @click="dialogFormVisible = false">取消</el-button>
|
|
|
|
<el-button type="primary" @click="submitForm(ruleFormRef)"
|
|
|
|
>注册</el-button
|
|
|
|
>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</el-dialog>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.loginBox {
|
|
|
|
width: 100%;
|
|
|
|
height: 100vh;
|
2024-07-20 04:36:41 +00:00
|
|
|
background-image: url("@/assets/image/loginback.jpg");
|
2024-07-17 22:33:59 +00:00
|
|
|
background-size: cover;
|
|
|
|
background-position: center;
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
// padding: 0 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.loginFrom {
|
|
|
|
width: 100%;
|
|
|
|
max-width: 400px;
|
|
|
|
padding: 20px;
|
|
|
|
background: rgba(255, 255, 255, 0.8);
|
|
|
|
border-radius: 10px;
|
|
|
|
// box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
// .inputBox {
|
|
|
|
|
|
|
|
// // margin-bottom: 20px;
|
|
|
|
// display: flex;
|
|
|
|
// align-items: center;
|
|
|
|
// justify-content: space-between;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
el-input{
|
|
|
|
height: 40px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.checkBox {
|
|
|
|
width: 100%;
|
|
|
|
margin-bottom: 20px;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.btnBox {
|
|
|
|
width: 80%;
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
}
|
|
|
|
|
|
|
|
.dialog-footer {
|
|
|
|
text-align: right;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
import { ElMessage } from "element-plus";
|
|
|
|
import "element-plus/theme-chalk/el-message.css";
|
|
|
|
import { login, register } from "@/api/user/user";
|
|
|
|
import { useTopicStore } from "@/stores/topic.js";
|
|
|
|
const topStore = useTopicStore();
|
|
|
|
import storage from "@/utils/Sotrage";
|
|
|
|
import TokenService from "@/utils/token";
|
|
|
|
import router from "@/router";
|
|
|
|
import { ref, reactive, onMounted } from "vue";
|
|
|
|
|
|
|
|
const user = reactive({
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
const remember = ref(false);
|
|
|
|
|
|
|
|
const toLogin = async () => {
|
|
|
|
if (user.username === "" || user.password === "") {
|
|
|
|
ElMessage({ message: "请输入完整信息", type: "warning" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = await login(user);
|
|
|
|
if (res.code === 200) {
|
|
|
|
ElMessage({ message: "登录成功", type: "success" });
|
|
|
|
if (remember.value) {
|
|
|
|
storage.setStorage("username", user.username);
|
|
|
|
storage.setStorage("password", user.password);
|
|
|
|
} else {
|
|
|
|
storage.removeStorage("username");
|
|
|
|
storage.removeStorage("password");
|
|
|
|
}
|
|
|
|
TokenService.saveToken(res.token);
|
|
|
|
router.push("/");
|
|
|
|
}else{
|
|
|
|
ElMessage({ message: res.msg, type: "error" });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const dialogFormVisible = ref(false);
|
|
|
|
const ruleFormRef = ref(null);
|
|
|
|
|
|
|
|
const ruleForm = reactive({
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
password2: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
const usernamevalidate = (rule, value, callback) => {
|
|
|
|
if (value === "") {
|
|
|
|
callback(new Error("请输入账号"));
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const validatePass = (rule, value, callback) => {
|
|
|
|
if (value === "") {
|
|
|
|
callback(new Error("请输入密码"));
|
|
|
|
} else {
|
|
|
|
if (ruleForm.password !== "") {
|
|
|
|
if (!ruleFormRef.value) return;
|
|
|
|
ruleFormRef.value.validateField("password2");
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const validatePass2 = (rule, value, callback) => {
|
|
|
|
if (value === "") {
|
|
|
|
callback(new Error("请输入确认密码"));
|
|
|
|
} else if (value !== ruleForm.password) {
|
|
|
|
callback(new Error("密码不一致"));
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const rules = reactive({
|
|
|
|
username: [{ validator: usernamevalidate, trigger: "blur" }],
|
|
|
|
password: [{ validator: validatePass, trigger: "blur" }],
|
|
|
|
password2: [{ validator: validatePass2, trigger: "blur" }],
|
|
|
|
});
|
|
|
|
|
|
|
|
const submitForm = (formEl) => {
|
|
|
|
if (!formEl) return;
|
|
|
|
formEl.validate(async (valid) => {
|
|
|
|
if (valid) {
|
|
|
|
let res = await register({
|
|
|
|
username: ruleForm.username,
|
|
|
|
password: ruleForm.password,
|
|
|
|
});
|
|
|
|
if (res.code === 200) {
|
|
|
|
ElMessage({ message: "注册成功", type: "success" });
|
|
|
|
dialogFormVisible.value = false;
|
|
|
|
} else {
|
|
|
|
ElMessage({ message: res.msg, type: "error" });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("error submit!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
user.username = storage.getStorage("username") || "";
|
|
|
|
user.password = storage.getStorage("password") || "";
|
|
|
|
topStore.clear();
|
|
|
|
});
|
|
|
|
</script>
|