From ae77d2905c760930ad4a2f5fa39d13e7b8807a8a Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Sun, 13 Jul 2025 21:13:04 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=94=A8=E6=88=B7=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=92=8C=E6=9B=B4=E6=96=B0=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/spring/step2/domain/dto/UserDto.java | 6 ++ .../step2/service/impl/UserServiceImpl.java | 14 ++- .../static/src/components/Pagination.js | 62 ++++++++++++ .../static/src/views/user/DialogUser.js | 94 +++++++++++++++++++ .../main/resources/templates/user/index.html | 90 ++++++++++-------- 5 files changed, 224 insertions(+), 42 deletions(-) create mode 100644 spring-security/step-2/src/main/resources/static/src/components/Pagination.js create mode 100644 spring-security/step-2/src/main/resources/static/src/views/user/DialogUser.js diff --git a/spring-security/step-2/src/main/java/com/spring/step2/domain/dto/UserDto.java b/spring-security/step-2/src/main/java/com/spring/step2/domain/dto/UserDto.java index 8b844b9..9531939 100644 --- a/spring-security/step-2/src/main/java/com/spring/step2/domain/dto/UserDto.java +++ b/spring-security/step-2/src/main/java/com/spring/step2/domain/dto/UserDto.java @@ -1,6 +1,7 @@ package com.spring.step2.domain.dto; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -13,13 +14,18 @@ import lombok.NoArgsConstructor; @Schema(name = "UserDTO对象", title = "用户", description = "用户的DTO对象") public class UserDto { + @Schema(name = "id", title = "主键") + private Long id; + @Schema(name = "username", title = "用户名") + @NotBlank(message = "用户名不能为空") private String username; @Schema(name = "password", title = "密码") private String password; @Schema(name = "email", title = "邮箱") + @NotBlank(message = "邮箱不能为空") private String email; } \ No newline at end of file diff --git a/spring-security/step-2/src/main/java/com/spring/step2/service/impl/UserServiceImpl.java b/spring-security/step-2/src/main/java/com/spring/step2/service/impl/UserServiceImpl.java index 46e66ab..35dae8d 100644 --- a/spring-security/step-2/src/main/java/com/spring/step2/service/impl/UserServiceImpl.java +++ b/spring-security/step-2/src/main/java/com/spring/step2/service/impl/UserServiceImpl.java @@ -15,6 +15,7 @@ import org.springframework.beans.BeanUtils; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; import java.util.List; @@ -65,8 +66,11 @@ public class UserServiceImpl extends ServiceImpl impleme UserEntity user = new UserEntity(); BeanUtils.copyProperties(dto, user); - // 设置用户密码 + // 用户密码是否为空,为空设置默认密码 String password = user.getPassword(); + password = StringUtils.hasText(password) ? password : "123456"; + + // 设置用户密码 String encodePassword = passwordEncoder.encode(password); user.setPassword(encodePassword); @@ -82,6 +86,14 @@ public class UserServiceImpl extends ServiceImpl impleme public void updateUser(UserDto dto) { UserEntity user = new UserEntity(); BeanUtils.copyProperties(dto, user); + + // 设置用户密码 + String password = user.getPassword(); + if (StringUtils.hasText(password)) { + String encodePassword = passwordEncoder.encode(password); + user.setPassword(encodePassword); + } + updateById(user); } diff --git a/spring-security/step-2/src/main/resources/static/src/components/Pagination.js b/spring-security/step-2/src/main/resources/static/src/components/Pagination.js new file mode 100644 index 0000000..0c9229c --- /dev/null +++ b/spring-security/step-2/src/main/resources/static/src/components/Pagination.js @@ -0,0 +1,62 @@ +const {defineComponent} = Vue; +/* + // pagination 类型 + const pagination = { + // 当前页 + pageNo: 0, + // 分页大小 + pageSize: 0, + // 总分页数 + pages: 0, + } +*/ + +const Pagination = defineComponent({ + name: "Pagination", + template: ` + + `, + props: { + pagination: {type: Object, required: true}, + // 初始化加载数据 + onSearch: {type: Function, required: true}, + }, + data() { + return {}; + }, + methods: { + /* 当前分页+1 */ + pageNoIncrease() { + this.pagination.pageNo++; + this.$emit("update:pagination", this.pagination); + this.onSearch(); + }, + /* 当前分页-1 */ + pageNoDecrease() { + this.pagination.pageNo--; + this.$emit("update:pagination", this.pagination); + this.onSearch(); + }, + /* 点击当前页 */ + onCurrentPageClick(page) { + this.pagination.pageNo = page; + this.$emit("update:pagination", this.pagination); + this.onSearch(); + } + } +}) \ No newline at end of file diff --git a/spring-security/step-2/src/main/resources/static/src/views/user/DialogUser.js b/spring-security/step-2/src/main/resources/static/src/views/user/DialogUser.js new file mode 100644 index 0000000..744786c --- /dev/null +++ b/spring-security/step-2/src/main/resources/static/src/views/user/DialogUser.js @@ -0,0 +1,94 @@ +const DialogUser = defineComponent({ + name: "DialogUser", + template: ` + + `, + props: { + // 是否添加 + isAdd: {type: Boolean, default: false}, + // 用户信息 + userinfo: {type: Object, required: true}, + // 加载函数 + onSearch: {type: Function, required: true}, + }, + data() { + return { + modalInstance: ref(null) + } + }, + methods: { + async onSubmit() { + // 是否添加表单 + const {code, message} = this.isAdd ? + await axiosInstance.post("/user", this.userinfo) : + await axiosInstance.put("/user", this.userinfo); + + if (code === 200) { + antd.message.success(message); + // 关闭模态框 + this.modalInstance.hide(); + this.onSearch(); + } + } + }, + mounted() { + // 初始化模态框实例 + const modalEl = this.$refs.modalRef; + this.modalInstance = new bootstrap.Modal(modalEl, { + backdrop: 'static', + keyboard: false + }); + }, + beforeUnmount() { + // 组件销毁时清理模态框实例 + if (this.modalInstance) { + this.modalInstance.dispose(); + } + } +}); \ No newline at end of file diff --git a/spring-security/step-2/src/main/resources/templates/user/index.html b/spring-security/step-2/src/main/resources/templates/user/index.html index ae901e6..0173748 100644 --- a/spring-security/step-2/src/main/resources/templates/user/index.html +++ b/spring-security/step-2/src/main/resources/templates/user/index.html @@ -32,6 +32,9 @@
+ + +
用户查询 @@ -42,12 +45,12 @@
+ type="text" v-model="searchForm.username">
+ type="email" v-model="searchForm.email">
@@ -64,10 +67,11 @@
+
用户列表 -
@@ -93,11 +97,11 @@ {{formatDate(user.updateTime)}}
- -
@@ -107,20 +111,8 @@
- + +
@@ -129,20 +121,29 @@ + + + + + \ No newline at end of file