Vue3 登录页面 - 使用图形验证码和本地存储Token
<p>"<script>\nimport axios from 'axios';\nimport Vcode from "vue3-puzzle-vcode";\nimport { ref } from 'vue';\n\nexport default {\n name: "loginPage",\n data() {\n return {\n username: '',\n password: '',\n errorMsg: '',\n isShow: false,\n };\n },\n methods: {\n login() {\n // 阻止默认提交事件\n e.preventDefault();\n\n // 图形验证码\n const onSuccess = () => {\n this.isShow = false; // 验证成功,关闭模态框\n this.sendLoginRequest();\n };\n\n if (onSuccess == true) {\n // 发送post请求\n axios.post(<code>http://nn.nnCoding.icu:8080/api/login</code>, {\n username: this.username,\n password: this.password\n }).then(result => {\n // 登录成功,将token存储在本地\n localStorage.setItem('token', result.data.token);\n this.$router.push("/home");\n console.log(result);\n }).catch(error => {\n this.errorMsg = "用户名或密码错误";\n console.log(error);\n console.log(error.response.data.message);\n });\n }\n },\n sendLoginRequest() {\n axios.post(<code>http://nn.nnCoding.icu:8080/api/login</code>, {\n username: this.username,\n password: this.password\n }).then(result => {\n // 登录成功,将token存储在本地\n localStorage.setItem('token', result.data.token);\n this.$router.push("/home");\n console.log(result);\n }).catch(error => {\n this.errorMsg = "用户名或密码错误";\n console.log(error);\n console.log(error.response.data.message);\n });\n }\n }\n}\n</script>\n\n<template>\n <div id="cloud"><span class="shadow"></span></div>\n <div id="cloud1"><span class="shadow"></span></div>\n <div id="cloud2"><span class="shadow"></span></div>\n <div class="login-page">\n <div v-show="show" class="login-container">\n <h2 class="login-title">登录</h2>\n <el-form class="login-form">\n <el-form-item label="账号:">\n <el-input v-model="username" type="text" style="width: 250px;" placeholder="用户名或手机号登录"></el-input>\n </el-form-item>\n <el-form-item label="密码:">\n <el-input v-model="password" type="password" style="width:250px" show-password /> </el-form-item>\n <el-form-item>\n <Vcode :show="isShow" @success="onSuccess" />\n <el-button class="login-button" type="primary" @click="login">登录</el-button>\n </el-form-item>\n </el-form>\n <div class="error-msg" v-if="errorMsg">{{ errorMsg }}</div>\n <div class="register-link">\n <router-link to="/register">\n <el-button type="info" style="width: 100%;">注册</el-button>\n </router-link>\n </div>\n </div>\n </div>\n</template>\n\n<style scoped>\n.login-page {\n background: -webkit-linear-gradient(skyblue, pink);\n height: 100vh;\n display: flex;\n justify-content: center;\n align-items: center;\n}\n\n.login-container {\n background-color: #fff;\n padding: 30px;\n border-radius: 10px;\n box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);\n}\n\n.login-title {\n text-align: center;\n margin-bottom: 20px;\n font-size: 24px;\n}\n\n.login-form {\n max-width: 400px;\n margin: 0 auto;\n}\n\n.login-button {\n width: 100%;\n}\n\n.register-link {\n margin-top: 20px;\n text-align: center;\n}\n\n.error-msg {\n color: red;\n margin-top: 10px;\n text-align: center;\n}\n</style>\n<style src="../../styles/dark/cloud_login.css" /></p>
原文地址: https://www.cveoy.top/t/topic/pL9V 著作权归作者所有。请勿转载和采集!