基础信息管理
学习目标
在这里,你将系统学习了解 MemoryAPI 忆汇廊 开始菜单
的具体代码实现
我们将以最简单直接
的方式为您呈现内容!
# 🍝 用户信息管理
# 用户注册
- 在
UserService
接口中,我们定义了一个名为userRegister
的方法,用于处理用户的注册操作
。
/**
* 用户注册
*
* @param userAccount 用户账户
* @param userPassword 用户密码
* @param checkPassword 校验密码
* @return 新用户 id
*/
long userRegister(String userAccount, String userPassword, String checkPassword);
- 在
UserServiceImpl
类中,具体实现 UserService 接口的userRegister 方法
,以构建用户注册的业务逻辑。此方法将涵盖用户账户
、密码
及校验密码
的验证流程,并确保新用户能够成功注册并获得相应的用户 id。
/**
* 盐值,混淆密码
*/
private static final String SALT = "memory";
@Override
public long userRegister(String userAccount, String userPassword, String checkPassword) {
// 1. 校验
if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
}
if (userAccount.length() < 4) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户账号过短");
}
if (userPassword.length() < 8 || checkPassword.length() < 8) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户密码过短");
}
// 密码和校验密码相同
if (!userPassword.equals(checkPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次输入的密码不一致");
}
synchronized (userAccount.intern()) {
// 账户不能重复
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userAccount", userAccount);
long count = this.baseMapper.selectCount(queryWrapper);
if (count > 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号重复");
}
// 2. 加密
String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
// 3. 插入数据
User user = new User();
user.setUserAccount(userAccount);
user.setUserPassword(encryptPassword);
boolean saveResult = this.save(user);
if (!saveResult) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "注册失败,数据库错误");
}
return user.getId();
}
}
- 在
UserController
下实现了用户注册功能,通过接收用户注册请求参数,进行参数校验,调用userService.userRegister 方法
完成注册操作,并返回用户 id。
@Resource
private UserService userService;
/**
* 用户注册
*
* @param userRegisterRequest 用户注册请求参数
* @return 用户 id
*/
@PostMapping("/register")
public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
// Controller 层对参数的校验
if (userRegisterRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String userAccount = userRegisterRequest.getUserAccount();
String userPassword = userRegisterRequest.getUserPassword();
String checkPassword = userRegisterRequest.getCheckPassword();
// 校验 userAccount、userPassword、checkPassword 不能为空
if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
return null;
}
long result = userService.userRegister(userAccount, userPassword, checkPassword);
return ResultUtils.success(result);
}
# 用户登录
/**
* 用户登录
*
* @param userLoginRequest 用户登录请求参数
* @param request request
* @return 登录用户信息
*/
@PostMapping("/login")
public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
// Controller 层对参数的校验
if (userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String userAccount = userLoginRequest.getUserAccount();
String userPassword = userLoginRequest.getUserPassword();
if (StringUtils.isAnyBlank(userAccount, userPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword, request);
return ResultUtils.success(loginUserVO);
}
# 短信发送
/**
* 短信发送
*
* @return 发送成功与否
*/
@PostMapping("/sendMsg")
public BaseResponse<Boolean> sendShortMessage(@RequestBody UserLoginRequest userLoginRequest) {
// Controller 层对参数的校验
if (userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long phone = userLoginRequest.getPhone();
boolean result = userService.sendShortMessage(phone);
return ResultUtils.success(result);
}
# 验证码登录
/**
* 验证码登录
*
* @param userLoginRequest 用户登录请求参数
* @param request request
* @return 登录用户信息
*/
@PostMapping("/login/code")
public BaseResponse<LoginUserVO> userLoginByCode(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
// Controller 层对参数的校验
if (userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String code = userLoginRequest.getCode();
Long phone = userLoginRequest.getPhone();
LoginUserVO loginUserVO = userService.userLoginByCode(phone, code, request);
return ResultUtils.success(loginUserVO);
}
# 获取用户列表(管理员)
/**
* 分页获取用户列表
*
* @param userQueryRequest
* @param request
* @return
*/
@PostMapping("/list/page/vo")
public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryRequest userQueryRequest,
HttpServletRequest request) {
if (userQueryRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long current = userQueryRequest.getCurrent();
long size = userQueryRequest.getPageSize();
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<User> userPage = userService.page(new Page<>(current, size),
userService.getQueryWrapper(userQueryRequest));
Page<UserVO> userVOPage = new Page<>(current, size, userPage.getTotal());
List<UserVO> userVO = userService.getUserVO(userPage.getRecords());
userVOPage.setRecords(userVO);
return ResultUtils.success(userVOPage);
}
# 创建用户(管理员)
/**
* 创建用户
*
* @param userAddRequest
* @param request
* @return
*/
@PostMapping("/add")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest, HttpServletRequest request) {
if (userAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User user = new User();
BeanUtils.copyProperties(userAddRequest, user);
boolean result = userService.save(user);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(user.getId());
}
# 删除用户(管理员)
/**
* 删除用户
*
* @param deleteRequest
* @param request
* @return
*/
@PostMapping("/delete")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
if (deleteRequest == null || deleteRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean b = userService.removeById(deleteRequest.getId());
return ResultUtils.success(b);
}
# 更新用户(管理员)
/**
* 更新用户
*
* @param userUpdateRequest
* @param request
* @return
*/
@PostMapping("/update")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest,
HttpServletRequest request) {
if (userUpdateRequest == null || userUpdateRequest.getId() == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User user = new User();
BeanUtils.copyProperties(userUpdateRequest, user);
boolean result = userService.updateById(user);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true);
}
# 当前用户(管理员)
/**
* 获取当前登录用户
*
* @param request
* @return
*/
@GetMapping("/get/login")
public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
User user = userService.getLoginUser(request);
LoginUserVO loginUserVO = userService.getLoginUserVO(user);
return ResultUtils.success(loginUserVO);
}
# 每日签到
/**
* 每日签到
*
* @param request
* @return
*/
@GetMapping("/sign")
public BaseResponse<LoginUserVO> singleSignInTime(HttpServletRequest request) {
LoginUserVO inTime = userService.singleSignInTime(request);
return ResultUtils.success(inTime);
}
# 用户注销
/**
* 用户注销
*
* @param request
* @return
*/
@PostMapping("/logout")
public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
if (request == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = userService.userLogout(request);
return ResultUtils.success(result);
}
# 🍖 接口信息管理
# 新增接口(管理员)
/**
* 创建接口
*
* @param interfaceInfoAddRequest 创建接口参数
* @param request request
* @return 创建接口成功
*/
@PostMapping("/add")
public BaseResponse<Long> addInterfaceInfo(@RequestBody InterfaceInfoAddRequest interfaceInfoAddRequest,
HttpServletRequest request) {
// controller校验参数
if (interfaceInfoAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
InterfaceInfo interfaceInfo = new InterfaceInfo();
BeanUtils.copyProperties(interfaceInfoAddRequest, interfaceInfo);
interfaceInfoService.addInterfaceInfo(interfaceInfo);
User loginUser = userService.getLoginUser(request);
interfaceInfo.setUserId(loginUser.getId());
boolean result = interfaceInfoService.save(interfaceInfo);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
long newInterfaceInfoId = interfaceInfo.getId();
return ResultUtils.success(newInterfaceInfoId);
}
# 删除接口(管理员)
/**
* 删除接口
*
* @param deleteRequest 删除接口参数
* @param request request
* @return 删除接口成功
*/
@PostMapping("/delete")
public BaseResponse<Boolean> deleteInterfaceInfo(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
// controller校验参数
if (deleteRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User user = userService.getLoginUser(request);
long id = deleteRequest.getId();
// 判断是否存在
InterfaceInfo oldInterfaceInfo = interfaceInfoService.getById(id);
ThrowUtils.throwIf(oldInterfaceInfo == null, ErrorCode.NOT_FOUND_ERROR);
// 仅本人或管理员可删除
if (!oldInterfaceInfo.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
}
boolean b = interfaceInfoService.removeById(id);
return ResultUtils.success(b);
}
# 获取接口列表(管理员)
/**
* 获取接口信息列表
* 分页查询
*
* @param interfaceInfoQueryRequest 查询参数
* @param request request
* @return 接口信息列表
*/
@PostMapping("/list/page")
public BaseResponse<Page<InterfaceInfo>> listInterfaceInfoByPage(@RequestBody InterfaceInfoQueryRequest interfaceInfoQueryRequest,
HttpServletRequest request) {
// controller校验参数
if (interfaceInfoQueryRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long current = interfaceInfoQueryRequest.getCurrent();
long size = interfaceInfoQueryRequest.getPageSize();
InterfaceInfo interfaceInfo = new InterfaceInfo();
BeanUtils.copyProperties(interfaceInfoQueryRequest, interfaceInfo);
interfaceInfoService.addInterfaceInfo(interfaceInfo);
// 限制爬虫
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
Page<InterfaceInfo> interfaceInfoPage = interfaceInfoService.page(new Page<>(current, size));
return ResultUtils.success(interfaceInfoPage);
}
# 更新接口信息(管理员)
/**
* 更新接口(仅管理员)
*
* @param interfaceInfoUpdateRequest 更新接口参数
* @return 更新接口成功
*/
@PostMapping("/update")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> updateInterfaceInfo(@RequestBody InterfaceInfoUpdateRequest interfaceInfoUpdateRequest) {
// controller校验参数
if (interfaceInfoUpdateRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
InterfaceInfo interfaceInfo = new InterfaceInfo();
BeanUtils.copyProperties(interfaceInfoUpdateRequest, interfaceInfo);
interfaceInfoService.updateInterfaceInfo(interfaceInfo);
long id = interfaceInfoUpdateRequest.getId();
InterfaceInfo oldInterfaceInfo = interfaceInfoService.getById(id);
ThrowUtils.throwIf(oldInterfaceInfo == null, ErrorCode.NOT_FOUND_ERROR);
boolean result = interfaceInfoService.updateById(interfaceInfo);
return ResultUtils.success(result);
}
# 接口发布(管理员)
/**
* 发布
*
* @param idRequest
* @param request
* @return
*/
@PostMapping("/online")
public BaseResponse<Boolean> onlineInterfaceInfo(@RequestBody IdRequest idRequest,
HttpServletRequest request) {
// controller校验参数
if (idRequest == null || idRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long id = idRequest.getId();
// 判断是否存在
InterfaceInfo oldInterfaceInfo = interfaceInfoService.getById(id);
if (oldInterfaceInfo == null) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
}
// 判断该接口是否可以调用
com.memory.client.model.User user = new com.memory.client.model.User();
user.setName("test");
String username = memoryClientService.getUserByPost(user);
if (StringUtils.isBlank(username)) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "接口验证失败");
}
// 仅本人或管理员可修改
InterfaceInfo interfaceInfo = new InterfaceInfo();
interfaceInfo.setId(id);
interfaceInfo.setStatus(InterfaceInfoStatusEnum.ONLINE.getValue());
boolean result = interfaceInfoService.updateById(interfaceInfo);
return ResultUtils.success(result);
}
# 接口下线(管理员)
/**
* 下线
*
* @param idRequest
* @param request
* @return
*/
@PostMapping("/offline")
public BaseResponse<Boolean> offlineInterfaceInfo(@RequestBody IdRequest idRequest,
HttpServletRequest request) {
// controller校验参数
if (idRequest == null || idRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long id = idRequest.getId();
// 判断是否存在
InterfaceInfo oldInterfaceInfo = interfaceInfoService.getById(id);
if (oldInterfaceInfo == null) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
}
// 仅本人或管理员可修改
InterfaceInfo interfaceInfo = new InterfaceInfo();
interfaceInfo.setId(id);
interfaceInfo.setStatus(InterfaceInfoStatusEnum.OFFLINE.getValue());
boolean result = interfaceInfoService.updateById(interfaceInfo);
return ResultUtils.success(result);
}
# 接口调用
/**
* 接口调用
*
* @param interFaceInfoInvokeRequest 接口调用参数
* @param request request
* @return
*/
@PostMapping("/invoke")
public BaseResponse<Object> invokeInterfaceInfo(@RequestBody InterFaceInfoInvokeRequest interFaceInfoInvokeRequest,
HttpServletRequest request) {
// controller校验参数
if (interFaceInfoInvokeRequest == null || interFaceInfoInvokeRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 接口id
long id = interFaceInfoInvokeRequest.getId();
String userRequestParams = interFaceInfoInvokeRequest.getUserRequestParams();
// 判断是否存在
InterfaceInfo oldInterfaceInfo = interfaceInfoService.getById(id);
if (oldInterfaceInfo == null) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
}
// 判断是否状态是否正常
if (oldInterfaceInfo.getStatus().equals(InterfaceInfoStatusEnum.OFFLINE.getValue())) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口已关闭");
}
// 用户调用接口
User loginUser = userService.getLoginUser(request);
String accessKey = loginUser.getAccessKey();
String secretKey = loginUser.getSecretKey();
MemoryClientService tempMemoryClientService = new MemoryClientServiceImpl(accessKey, secretKey);
// TODO 根据不同地址调用对应接口
String result = interfaceIdSource.invokeInterfaceById(id, userRequestParams, tempMemoryClientService);
return ResultUtils.success(result);
}
# 🥣 个人信息管理
# 信息修改
/**
* 获取当前登录用户
*
* @param request
* @return
*/
@GetMapping("/get/login")
public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
User user = userService.getLoginUser(request);
return ResultUtils.success(userService.getLoginUserVO(user));
}
# 🍨 文件上传
/**
* 文件上传接口
*
* @param file
* @return
*/
@PostMapping("/upload")
public Map<String, Object> upload(@RequestPart("file") MultipartFile file) {
String imgFileStr = fileService.upload(file);
return buildResult(imgFileStr);
}
/**
* 测试返回拼装,根据公司自己封装的统一返回去写
*
* @param str
* @return
*/
private Map<String, Object> buildResult(String str) {
Map<String, Object> result = new HashMap<>();
// 判断字符串用lang3下的StringUtils去判断,这块我就不引入新的依赖了
if (str == null || "".equals(str)) {
result.put("code", 10000);
result.put("msg", "图片上传失败");
result.put("data", null);
} else {
result.put("code", 200);
result.put("msg", "图片上传成功");
result.put("data", str);
}
return result;
}
# AK/SK 管理