Vue3+SpringBoot实现【登录】【毛玻璃】【渐变色】

2023-10-19 23:10

本文主要是介绍Vue3+SpringBoot实现【登录】【毛玻璃】【渐变色】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先创建Login.vue,编写界面和样式

这个是渐变色背景,登陆框背景为白色

<template><div class="wrapper"><div style="margin: 200px auto; background-color: #fff; width: 350px; height: 300px;padding: 20px;border-radius: 10px"><div style="margin: 20px 0;text-align: center;font-size: 24px"><b>登录</b><el-input size="medium" style="margin: 10px 0" v-model="user.username"><template #prefix><el-icon class="el-input__icon"><user></user></el-icon></template></el-input><el-input size="medium" style="margin: 10px 0" show-password v-model="user.password"><template #prefix><el-icon class="el-input__icon"><lock></lock></el-icon></template></el-input><div style="margin: 10px 0; text-align: right"><el-button type="primary" size="small" autocomplete="off">登录</el-button><el-button type="primary" size="small" autocomplete="off">注册</el-button></div></div></div></div>
</template><script>
import {Lock, User} from "@element-plus/icons";
export default {name: "LoginView",components: {Lock, User},data(){return{user:{}}}
}
</script><style scoped>.wrapper{height: 100vh;background-image: linear-gradient(to bottom right,#FC466B,#3F5EFB);overflow:hidden;}
</style>

,如果想要登录框为毛玻璃,可以将上面第二个div的 “background-color: #fff;”去掉

然后把这个div设置为class=login,编写login类的样式如下

 .login{background-color:rgba(255,255,255,0.1); //透明度backdrop-filter: blur(10px);  //毛玻璃效果}

 效果: (如果换一个比较鲜艳的背景,毛玻璃效果会更明显)

3e9045abe86345d3aab2ba0cd6541761.png

设置Login.vue的路由

caf5514e56124b4888bb6a80c9a4e946.png

 绑定登陆按钮的点击时间login 

4b821335552c4f4f93b49f7b7718d4cd.png

 编写login事件

 methods:{login(){this.request.post("http://localhost:8081/login",this.user).then(res =>{if(!res){this.$message.error("用户名或密码错误")}else {this.$message.success("登陆成功")this.$router.push("/new/home")}})}}

定义一个UserDto类

import lombok.Data;/*
接受前端登陆请求的参数*/@Data
public class UserDto {private String username;private String password;}

编写接口:

 //登陆的接口  RequestBody是将前端json传后端@PostMapping("/login")public boolean login(@RequestBody UserDto userDto){String username = userDto.getUsername();String password = userDto.getPassword();//这个if校验最好放在前端做if(StrUtil.isBlank(username)||StrUtil.isBlank(username))  {//判断是否为空,且使用了hutool里的工具类return false;}return userService.login(userDto);}

编写业务层接口

public boolean login(UserDto userDto) {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.eq("username",userDto.getUsername());queryWrapper.eq("password",userDto.getPassword());User one = getOne(queryWrapper);return one!=null;}

即可实现登录功能

452fa0c348c54bd7be17cce94ddb3b41.png

如果想要退出登陆,其实很简单,给退出绑定一个router-link就可以了

    <el-dropdown-item><router-link to="/login" style="text-decoration: none">退出</router-link></el-dropdown-item>

完善:

添加校验

(1)在把需要校验的部分用el-form表单标签括起来

<el-form  :rules="rules"><el-input size="default" style="margin: 10px 0" v-model="user.username"><template #prefix><el-icon class="el-input__icon"><user></user></el-icon></template></el-input><el-input size="default" style="margin: 10px 0" show-password v-model="user.password"><template #prefix><el-icon class="el-input__icon"><lock></lock></el-icon></template></el-input><div style="margin: 10px 0; text-align: right"><el-button type="primary" size="small" autocomplete="off" @click="login">登录</el-button><el-button type="warning" size="small" autocomplete="off">注册</el-button></div></el-form>

(2)在element-plus官网找到form那一块的校验 Form 表单 | Element Plus (element-plus.org)

a7e9373f1c07460db4f8c9f5c315561f.png

将rules绑定属性填进form标签9b0f433631a6479480f5eb3f0dd29af6.png

 然后在return里填写官网给的rules返回的数据形式,编写校验的规则

return {user: {},rules: {username: [{required: true, message: '请输入用户名', trigger: 'blur'},{min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur'},],password: [{required: true, message: '请输入用户名', trigger: 'blur'},{min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur'},],}}

再把我们的规则用prop传值写在el-form-item里面

 <el-form-item  label="用户名" prop="username" ><el-input size="default"  v-model="user.username" ><template #prefix><el-icon class="el-input__icon"><user></user></el-icon></template></el-input></el-form-item><el-form-item  label="密码" prop="password" ><el-input size="default"  show-password v-model="user.password" ><template #prefix><el-icon class="el-input__icon"><lock></lock></el-icon></template></el-input></el-form-item>

 此时如果没有传值,会出提示,但是提示并不会消失

我们则需要在e-form处传一个校验:此时输入正确后,提示将会消失

2617a24a5a7346fd9ea80ba6f0aecea8.png

 label-width=“auto”会让标签默认右对齐

最后效果:

e0ba04f1b9cb42df85d15cc9260282ef.png

 0aa4809b92204c06b072256559b6e73b.png

简单版的login到这里就结束了,但是这样写登陆的业务逻辑判断不是很好。但是如果不是在公司做登陆,只是做一个简单的系统是足够的。

加强处理异常,和规范性

创建一个common的包,里面创建接口Constans和Result类

package com.lnw.common;public interface Constants {String CODE_200 = "200";      //成功String CODE_401 = "401";      //权限不足String CODE_400 = "400";      //参数错误String CODE_500 ="500";       //系统错误String CODE_600 = "600"; //其它业务异常
}
package com.lnw.common;import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*
接口统一返回包装类*/@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {private String code;private String msg;private Object data;public static Result success(){return new Result(Constants.CODE_200,"",null);}public static Result success(Object data){return new Result(Constants.CODE_200,"",data);}public static Result error(String code,String msg){return new Result(code,msg,null);}public static Result error(){return new Result(Constants.CODE_500,"系统错误",null);}}

 创建exception包,创建全局管理器和重写ServiceException

package com.lnw.exception;import com.lnw.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
//全局自定义异常处理器@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(ServiceException.class)@ResponseBodypublic Result handle(ServiceException se ){return Result.error(se.getCode(),se.getMessage());}}
@Data
public class ServiceException extends RuntimeException {private String code;//构造器public ServiceException (String code, String msg){super(msg);this.code=code;}
}

 重新封装接口 controller

 //登陆的接口  RequestBody是将前端json传后端@PostMapping("/login")public Result login(@RequestBody UserDto userDto){String username = userDto.getUsername();String password = userDto.getPassword();//这个if校验最好放在前端做if(StrUtil.isBlank(username)||StrUtil.isBlank(username))  {//判断是否为空,且使用了hutool里的工具类return Result.error(Constants.CODE_400,"参数错误");}UserDto dto = userService.login(userDto);return Result.success(dto);}

重新写前端接口

login(){this.request.post("http://localhost:8081/login",this.user).then(res =>{// if(!res){//   this.$message.error("用户名或密码错误")// }else {//   this.$message.success("登陆成功")//   this.$router.push("/new/home")// }if(res.code === '200'){// 如果获取到,则存一个user对象 为了替换登录后右上角的个人用户名localStorage.setItem("user",JSON.stringify(res.data)) //存储用户信息到浏览器this.$router.push("/new/home")this.$message.success("登陆成功")}else {this.$message.error(res.msg)}})}

这样就是更规范版本的登陆

这篇关于Vue3+SpringBoot实现【登录】【毛玻璃】【渐变色】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/242982

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——