diff --git a/src/main/java/com/lk/paopao/PaopaoApplication.java b/src/main/java/com/lk/paopao/PaopaoApplication.java index 494001a..0b0c77a 100644 --- a/src/main/java/com/lk/paopao/PaopaoApplication.java +++ b/src/main/java/com/lk/paopao/PaopaoApplication.java @@ -2,8 +2,10 @@ package com.lk.paopao; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication +@EnableJpaAuditing public class PaopaoApplication { public static void main(String[] args) { diff --git a/src/main/java/com/lk/paopao/controller/AuthController.java b/src/main/java/com/lk/paopao/controller/AuthController.java new file mode 100644 index 0000000..d3d1083 --- /dev/null +++ b/src/main/java/com/lk/paopao/controller/AuthController.java @@ -0,0 +1,26 @@ +package com.lk.paopao.controller; + +import com.lk.paopao.dto.rest.request.RegisterRequest; +import com.lk.paopao.dto.rest.response.DataResult; +import com.lk.paopao.dto.rest.response.ResultUtil; +import com.lk.paopao.entity.User; +import com.lk.paopao.service.AuthService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("${app.version}/auth") +public class AuthController { + @Autowired + AuthService authService; + + @PostMapping("/register") + public DataResult register(@RequestBody RegisterRequest req) { + User saved = authService.register(req); + + return ResultUtil.ok(saved); + } +} diff --git a/src/main/java/com/lk/paopao/dto/rest/request/RegisterRequest.java b/src/main/java/com/lk/paopao/dto/rest/request/RegisterRequest.java new file mode 100644 index 0000000..d048439 --- /dev/null +++ b/src/main/java/com/lk/paopao/dto/rest/request/RegisterRequest.java @@ -0,0 +1,9 @@ +package com.lk.paopao.dto.rest.request; + +import lombok.Data; + +@Data +public class RegisterRequest { + String username; + String password; +} diff --git a/src/main/java/com/lk/paopao/dto/rest/response/DataResult.java b/src/main/java/com/lk/paopao/dto/rest/response/DataResult.java new file mode 100644 index 0000000..0bef389 --- /dev/null +++ b/src/main/java/com/lk/paopao/dto/rest/response/DataResult.java @@ -0,0 +1,16 @@ +package com.lk.paopao.dto.rest.response; + +import lombok.Getter; +import java.io.Serializable; + +@Getter +public class DataResult extends Result implements Serializable { + + T data; + + public DataResult(int code, T data, String message){ + super(code, message); + this.data = data; + } + +} diff --git a/src/main/java/com/lk/paopao/dto/rest/response/Result.java b/src/main/java/com/lk/paopao/dto/rest/response/Result.java new file mode 100644 index 0000000..99805e7 --- /dev/null +++ b/src/main/java/com/lk/paopao/dto/rest/response/Result.java @@ -0,0 +1,15 @@ +package com.lk.paopao.dto.rest.response; + +import lombok.Getter; + +import java.io.Serializable; + +@Getter +public class Result implements Serializable { + int code; + String msg; + public Result(int code, String message) { + this.code = code; + this.msg = message; + } +} \ No newline at end of file diff --git a/src/main/java/com/lk/paopao/dto/rest/response/ResultUtil.java b/src/main/java/com/lk/paopao/dto/rest/response/ResultUtil.java new file mode 100644 index 0000000..d853f34 --- /dev/null +++ b/src/main/java/com/lk/paopao/dto/rest/response/ResultUtil.java @@ -0,0 +1,20 @@ +package com.lk.paopao.dto.rest.response; + +public class ResultUtil { + public static DataResult ok(T data) { + return new DataResult(0, data, "SUCCESS"); + } + + public static Result ok() { + return new Result(0, "SUCCESS"); + } + + public static Result fail(String error) { + return new Result(500, error); + } + + public static Result fail(int code, String error) { + return new Result(code, error); + } + +} \ No newline at end of file diff --git a/src/main/java/com/lk/paopao/entity/User.java b/src/main/java/com/lk/paopao/entity/User.java new file mode 100644 index 0000000..c29fb41 --- /dev/null +++ b/src/main/java/com/lk/paopao/entity/User.java @@ -0,0 +1,80 @@ +package com.lk.paopao.entity; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.Comment; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +@Getter +@Setter +@Comment("用户") +@Entity +@Table(name = "p_user", indexes = { + @Index(name = "idx_user_phone", columnList = "phone") +}, uniqueConstraints = { + @UniqueConstraint(name = "idx_user_username", columnNames = {"username"}) +}) +@EntityListeners(AuditingEntityListener.class) +public class User { + @Id + @Comment("用户ID") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Comment("昵称") + @ColumnDefault("") + @Column(name = "nickname", nullable = false, length = 32) + private String nickname; + + @Comment("用户名") + @ColumnDefault("") + @Column(name = "username", nullable = false, length = 32) + private String username; + + @Comment("手机号") +// @ColumnDefault("") + @Column(name = "phone", nullable = true, length = 16) + private String phone; + + @Comment("密码") + @ColumnDefault("") + @Column(name = "password", nullable = false, length = 32) + private String password; + + @Comment("状态,1正常,2停用") + @Column(name = "status", nullable = false) + private Byte status = 1; + + @Comment("用户头像") + @ColumnDefault("") + @Column(name = "avatar", nullable = false) + private String avatar; + + @Comment("是否管理员") + @Column(name = "is_admin") + private Boolean isAdmin = false; + + @Comment("创建时间") + @CreatedDate + @Column(name = "created_on", nullable = false) + private Long createdOn; + + @Comment("修改时间") + @LastModifiedDate + @Column(name = "modified_on", nullable = false) + private Long modifiedOn; + + @Comment("删除时间") + @ColumnDefault("0") + @Column(name = "deleted_on", nullable = true) + private Long deletedOn = 0L; + + @Comment("是否删除 0 为未删除、1 为已删除") + @Column(name = "is_del") + private byte isDel = 0; + +} \ No newline at end of file diff --git a/src/main/java/com/lk/paopao/repository/UserRepository.java b/src/main/java/com/lk/paopao/repository/UserRepository.java new file mode 100644 index 0000000..9db57fb --- /dev/null +++ b/src/main/java/com/lk/paopao/repository/UserRepository.java @@ -0,0 +1,10 @@ +package com.lk.paopao.repository; + +import com.lk.paopao.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface UserRepository extends JpaRepository { + Optional findByUsername(String username); +} \ No newline at end of file diff --git a/src/main/java/com/lk/paopao/service/AuthService.java b/src/main/java/com/lk/paopao/service/AuthService.java new file mode 100644 index 0000000..21875e6 --- /dev/null +++ b/src/main/java/com/lk/paopao/service/AuthService.java @@ -0,0 +1,42 @@ +package com.lk.paopao.service; + + +import com.lk.paopao.dto.rest.request.RegisterRequest; +import com.lk.paopao.entity.User; +import com.lk.paopao.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class AuthService { +// @Autowired +// PasswordEncoder encoder; // 自动注入密码编码器 + @Autowired + UserRepository userRepository; // 自动注入用户仓库 + + @Value("${app.default.head-icon}") + private String DEFAULT_HEAD_ICON; // 从配置文件中读取默认头像地址 + + /** + * 用户注册。 + * @param reg 注册请求对象,包含用户名和密码 + * @return 注册后的用户信息 + */ + public User register(RegisterRequest reg) { + // 检查用户是否已存在 + if(userRepository.findByUsername(reg.getUsername()).isPresent()){ + return null; + } + // 创建用户对象并设置信息 + User user = new User(); +// user.setPassword(encoder.encode(reg.getPassword())); // 编码密码 + user.setAvatar(DEFAULT_HEAD_ICON); // 设置默认头像 + user.setNickname(reg.getUsername()); // 设置昵称为用户名 + user.setUsername(reg.getUsername()); // 设置用户名 + user.setPassword(reg.getPassword()); + return userRepository.save(user); // 保存用户到数据库 + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index cf50cf2..6453e4e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,5 +3,32 @@ springdoc: path: /api-docs spring: + datasource: + url: jdbc:h2:file:./paopao.h2 + driverClassName: org.h2.Driver + username: root + password: root + # initialization-mode: always + h2: + console: # 开启console 访问 默认false + enabled: true + settings: + trace: true # 开启h2 console 跟踪 方便调试 默认 false + web-allow-others: true # 允许console 远程访问 默认false + path: /h2 # h2 访问路径上下文 jpa: - open-in-view: false \ No newline at end of file + show-sql: true + open-in-view: false + defer-datasource-initialization: true + database-platform: org.hibernate.dialect.H2Dialect + hibernate: + ddl-auto: create-drop # update create-drop + properties: + hibernate: + format_sql: true + dialect: org.hibernate.dialect.H2Dialect + +app: + version: v1 + default: + head-icon: https://assets.paopao.info/public/avatar/default/joshua.png \ No newline at end of file