[add]register
This commit is contained in:
parent
91cef69a63
commit
8bd67de4a8
112
docs/tasks/任务-restful接口回应数据的统一封装的实现.md
Normal file
112
docs/tasks/任务-restful接口回应数据的统一封装的实现.md
Normal file
@ -0,0 +1,112 @@
|
||||
### 任务名称: Restful接口回应数据的统一封装
|
||||
#### 目标:
|
||||
- 掌握Spring Boot框架基本层次结构
|
||||
- 掌握接口的统一返回格式的设计和实现
|
||||
- 泛型的使用
|
||||
- lombok的使用
|
||||
- spring配置文件中自定义属性
|
||||
#### 预备知识:
|
||||
- 泛型
|
||||
|
||||
#### 操作步骤:
|
||||
##### 1. 项目java包结构设计
|
||||
创建包:controller、entity、repository、service、dto、 dto.rest.response
|
||||
|
||||
##### 2. 实现类Result
|
||||
```java
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
##### 3. 实现类DataResult
|
||||
DataResult继承Result,添加data属性,通过泛型来确定data的类型。
|
||||
```java
|
||||
package com.lk.paopao.dto.rest.response;
|
||||
|
||||
import lombok.Getter;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Getter
|
||||
public class DataResult<T> extends Result implements Serializable {
|
||||
|
||||
T data;
|
||||
|
||||
public DataResult(int code, T data, String message){
|
||||
super(code, message);
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
##### 4. 实现类DataResult
|
||||
|
||||
通过工具类ResultUtil,实现ok、fail方法, 返回DataResult对象。
|
||||
|
||||
此工具类简化创建DataResult对象的过程。
|
||||
|
||||
```java
|
||||
package com.lk.paopao.dto.rest.response;
|
||||
|
||||
|
||||
public class ResultUtil {
|
||||
public static <T> DataResult<T> ok(T data) {
|
||||
return new DataResult<T>(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);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
##### 5. 自定义API接口版本号
|
||||
在application.yml配置文件中添加如下配置:
|
||||
```yaml
|
||||
|
||||
app:
|
||||
version: v1
|
||||
default:
|
||||
head-icon: https://assets.paopao.info/public/avatar/default/joshua.png
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
#### 技术/工具需求:
|
||||
- [列出完成任务所需的技术栈、工具、软件版本等。]
|
||||
|
||||
#### 成功标准:
|
||||
- [明确完成任务的评判标准,如代码功能实现、性能指标、测试通过条件等。]
|
||||
|
||||
#### 扩展学习(可选):
|
||||
- [提供一些额外学习资源或挑战性任务,鼓励学有余力的学生进一步探索。]
|
||||
|
||||
#### 评估与反馈:
|
||||
- [说明如何提交作业、代码审查的标准、或任何反馈收集机制。]
|
||||
|
||||
#### 时间估算:
|
||||
- [给出预计完成该任务所需的时间,帮助学生合理安排学习计划。]
|
166
docs/tasks/任务-注册接口实现.md
Normal file
166
docs/tasks/任务-注册接口实现.md
Normal file
@ -0,0 +1,166 @@
|
||||
### 任务名称: 实现注册接口
|
||||
#### 目标:
|
||||
- 掌握Restful API设计
|
||||
- 掌握RestController类的使用
|
||||
- 掌握JPA Entity类的使用
|
||||
- 掌握JPARepository接口的使用
|
||||
|
||||
#### 预备知识:
|
||||
- Restful API设计
|
||||
- JPA
|
||||
#### 操作步骤:
|
||||
##### 1. 创建RestController类:AuthController
|
||||
```java
|
||||
package com.lk.paopao.controller;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("${app.version}/auth")
|
||||
public class AuthController {
|
||||
@Autowired
|
||||
AuthService authService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public DataResult<User> register(@RequestBody RegisterRequest req) {
|
||||
User saved = authService.register(req);
|
||||
|
||||
return ResultUtil.ok(saved);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
##### 2. 创建实体类User
|
||||
|
||||
```java
|
||||
package com.lk.paopao.domain;
|
||||
|
||||
|
||||
@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", 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 = false)
|
||||
private Long deletedOn = 0L;
|
||||
|
||||
@Comment("是否删除 0 为未删除、1 为已删除")
|
||||
@Column(name = "is_del")
|
||||
private byte isDel = 0;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
##### 3. 创建接口UserRepository
|
||||
```java
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
}
|
||||
```
|
||||
##### 4. 创建AuthService
|
||||
```java
|
||||
package com.lk.paopao.service;
|
||||
|
||||
@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) {
|
||||
// 检查用户是否已存在
|
||||
userRepository.findByUsername(reg.getUsername()).ifPresent((u)-> {throw new ResourceNotFoundException("","");});
|
||||
// 创建用户对象并设置信息
|
||||
User user = new User();
|
||||
user.setPassword(encoder.encode(reg.getPassword())); // 编码密码
|
||||
user.setAvatar(DEFAULT_HEAD_ICON); // 设置默认头像
|
||||
user.setNickname(reg.getUsername()); // 设置昵称为用户名
|
||||
user.setUsername(reg.getUsername()); // 设置用户名
|
||||
user.setSalt(""); // 盐值,默认未使用
|
||||
user.setPhone(""); // 电话号码,默认未设置
|
||||
return userRepository.save(user); // 保存用户到数据库
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
#### 技术/工具需求:
|
||||
- [列出完成任务所需的技术栈、工具、软件版本等。]
|
||||
|
||||
#### 成功标准:
|
||||
- [明确完成任务的评判标准,如代码功能实现、性能指标、测试通过条件等。]
|
||||
|
||||
#### 扩展学习(可选):
|
||||
- [提供一些额外学习资源或挑战性任务,鼓励学有余力的学生进一步探索。]
|
||||
|
||||
#### 评估与反馈:
|
||||
- [说明如何提交作业、代码审查的标准、或任何反馈收集机制。]
|
||||
|
||||
#### 时间估算:
|
||||
- [给出预计完成该任务所需的时间,帮助学生合理安排学习计划。]
|
Loading…
Reference in New Issue
Block a user