消息队列存储数据
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.constant.FileConstant;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import com.yupi.springbootinit.manager.CosManager;
|
||||
import com.yupi.springbootinit.model.dto.file.UploadFileRequest;
|
||||
import com.yupi.springbootinit.model.entity.User;
|
||||
import com.yupi.springbootinit.model.enums.FileUploadBizEnum;
|
||||
import com.yupi.springbootinit.service.UserService;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 文件接口
|
||||
*
|
||||
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/file")
|
||||
@Slf4j
|
||||
public class FileController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private CosManager cosManager;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param multipartFile
|
||||
* @param uploadFileRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
public BaseResponse<String> uploadFile(@RequestPart("file") MultipartFile multipartFile,
|
||||
UploadFileRequest uploadFileRequest, HttpServletRequest request) {
|
||||
String biz = uploadFileRequest.getBiz();
|
||||
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
||||
if (fileUploadBizEnum == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
validFile(multipartFile, fileUploadBizEnum);
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
// 文件目录:根据业务、用户来划分
|
||||
String uuid = RandomStringUtils.randomAlphanumeric(8);
|
||||
String filename = uuid + "-" + multipartFile.getOriginalFilename();
|
||||
String filepath = String.format("/%s/%s/%s", fileUploadBizEnum.getValue(), loginUser.getId(), filename);
|
||||
File file = null;
|
||||
try {
|
||||
// 上传文件
|
||||
file = File.createTempFile(filepath, null);
|
||||
multipartFile.transferTo(file);
|
||||
cosManager.putObject(filepath, file);
|
||||
// 返回可访问地址
|
||||
return ResultUtils.success(FileConstant.COS_HOST + filepath);
|
||||
} catch (Exception e) {
|
||||
log.error("file upload error, filepath = " + filepath, e);
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "上传失败");
|
||||
} finally {
|
||||
if (file != null) {
|
||||
// 删除临时文件
|
||||
boolean delete = file.delete();
|
||||
if (!delete) {
|
||||
log.error("file delete error, filepath = {}", filepath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验文件
|
||||
*
|
||||
* @param multipartFile
|
||||
* @param fileUploadBizEnum 业务类型
|
||||
*/
|
||||
private void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
|
||||
// 文件大小
|
||||
long fileSize = multipartFile.getSize();
|
||||
// 文件后缀
|
||||
String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
|
||||
final long ONE_M = 1024 * 1024L;
|
||||
if (FileUploadBizEnum.USER_AVATAR.equals(fileUploadBizEnum)) {
|
||||
if (fileSize > ONE_M) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件大小不能超过 1M");
|
||||
}
|
||||
if (!Arrays.asList("jpeg", "jpg", "svg", "png", "webp").contains(fileSuffix)) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件类型错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.model.entity.NewHosts;
|
||||
import com.yupi.springbootinit.rabbitMQ.MQSender;
|
||||
import com.yupi.springbootinit.service.HostInfoService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/10 17:09
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/save_data")
|
||||
@Slf4j
|
||||
public class HostInfoController {
|
||||
|
||||
@Resource
|
||||
private MQSender mqSender;
|
||||
|
||||
|
||||
@PostMapping("add_host")
|
||||
public BaseResponse<Boolean> addHost(@RequestBody List<NewHosts> newHosts){
|
||||
mqSender.send(newHosts);
|
||||
return ResultUtils.success(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yupi.springbootinit.annotation.AuthCheck;
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.DeleteRequest;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.constant.UserConstant;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import com.yupi.springbootinit.exception.ThrowUtils;
|
||||
import com.yupi.springbootinit.model.dto.post.PostAddRequest;
|
||||
import com.yupi.springbootinit.model.dto.post.PostEditRequest;
|
||||
import com.yupi.springbootinit.model.dto.post.PostQueryRequest;
|
||||
import com.yupi.springbootinit.model.dto.post.PostUpdateRequest;
|
||||
import com.yupi.springbootinit.model.entity.Post;
|
||||
import com.yupi.springbootinit.model.entity.User;
|
||||
import com.yupi.springbootinit.model.vo.PostVO;
|
||||
import com.yupi.springbootinit.service.PostService;
|
||||
import com.yupi.springbootinit.service.UserService;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 帖子接口
|
||||
*
|
||||
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/post")
|
||||
@Slf4j
|
||||
public class PostController {
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
// region 增删改查
|
||||
|
||||
/**
|
||||
* 创建
|
||||
*
|
||||
* @param postAddRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/add")
|
||||
public BaseResponse<Long> addPost(@RequestBody PostAddRequest postAddRequest, HttpServletRequest request) {
|
||||
if (postAddRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postAddRequest, post);
|
||||
List<String> tags = postAddRequest.getTags();
|
||||
if (tags != null) {
|
||||
post.setTags(JSONUtil.toJsonStr(tags));
|
||||
}
|
||||
postService.validPost(post, true);
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
post.setUserId(loginUser.getId());
|
||||
post.setFavourNum(0);
|
||||
post.setThumbNum(0);
|
||||
boolean result = postService.save(post);
|
||||
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
|
||||
long newPostId = post.getId();
|
||||
return ResultUtils.success(newPostId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param deleteRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/delete")
|
||||
public BaseResponse<Boolean> deletePost(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
|
||||
if (deleteRequest == null || deleteRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User user = userService.getLoginUser(request);
|
||||
long id = deleteRequest.getId();
|
||||
// 判断是否存在
|
||||
Post oldPost = postService.getById(id);
|
||||
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
|
||||
// 仅本人或管理员可删除
|
||||
if (!oldPost.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
boolean b = postService.removeById(id);
|
||||
return ResultUtils.success(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新(仅管理员)
|
||||
*
|
||||
* @param postUpdateRequest
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/update")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Boolean> updatePost(@RequestBody PostUpdateRequest postUpdateRequest) {
|
||||
if (postUpdateRequest == null || postUpdateRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postUpdateRequest, post);
|
||||
List<String> tags = postUpdateRequest.getTags();
|
||||
if (tags != null) {
|
||||
post.setTags(JSONUtil.toJsonStr(tags));
|
||||
}
|
||||
// 参数校验
|
||||
postService.validPost(post, false);
|
||||
long id = postUpdateRequest.getId();
|
||||
// 判断是否存在
|
||||
Post oldPost = postService.getById(id);
|
||||
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
|
||||
boolean result = postService.updateById(post);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 id 获取
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get/vo")
|
||||
public BaseResponse<PostVO> getPostVOById(long id, HttpServletRequest request) {
|
||||
if (id <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = postService.getById(id);
|
||||
if (post == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
|
||||
}
|
||||
return ResultUtils.success(postService.getPostVO(post, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取列表(仅管理员)
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/list/page")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Page<Post>> listPostByPage(@RequestBody PostQueryRequest postQueryRequest) {
|
||||
long current = postQueryRequest.getCurrent();
|
||||
long size = postQueryRequest.getPageSize();
|
||||
Page<Post> postPage = postService.page(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postQueryRequest));
|
||||
return ResultUtils.success(postPage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取列表(封装类)
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/list/page/vo")
|
||||
public BaseResponse<Page<PostVO>> listPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
long current = postQueryRequest.getCurrent();
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postService.page(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postQueryRequest));
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取当前用户创建的资源列表
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/my/list/page/vo")
|
||||
public BaseResponse<Page<PostVO>> listMyPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postQueryRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
postQueryRequest.setUserId(loginUser.getId());
|
||||
long current = postQueryRequest.getCurrent();
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postService.page(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postQueryRequest));
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 分页搜索(从 ES 查询,封装类)
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/search/page/vo")
|
||||
public BaseResponse<Page<PostVO>> searchPostVOByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postService.searchFromEs(postQueryRequest);
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑(用户)
|
||||
*
|
||||
* @param postEditRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/edit")
|
||||
public BaseResponse<Boolean> editPost(@RequestBody PostEditRequest postEditRequest, HttpServletRequest request) {
|
||||
if (postEditRequest == null || postEditRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Post post = new Post();
|
||||
BeanUtils.copyProperties(postEditRequest, post);
|
||||
List<String> tags = postEditRequest.getTags();
|
||||
if (tags != null) {
|
||||
post.setTags(JSONUtil.toJsonStr(tags));
|
||||
}
|
||||
// 参数校验
|
||||
postService.validPost(post, false);
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
long id = postEditRequest.getId();
|
||||
// 判断是否存在
|
||||
Post oldPost = postService.getById(id);
|
||||
ThrowUtils.throwIf(oldPost == null, ErrorCode.NOT_FOUND_ERROR);
|
||||
// 仅本人或管理员可编辑
|
||||
if (!oldPost.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
boolean result = postService.updateById(post);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import com.yupi.springbootinit.exception.ThrowUtils;
|
||||
import com.yupi.springbootinit.model.dto.post.PostQueryRequest;
|
||||
import com.yupi.springbootinit.model.dto.postfavour.PostFavourAddRequest;
|
||||
import com.yupi.springbootinit.model.dto.postfavour.PostFavourQueryRequest;
|
||||
import com.yupi.springbootinit.model.entity.Post;
|
||||
import com.yupi.springbootinit.model.entity.User;
|
||||
import com.yupi.springbootinit.model.vo.PostVO;
|
||||
import com.yupi.springbootinit.service.PostFavourService;
|
||||
import com.yupi.springbootinit.service.PostService;
|
||||
import com.yupi.springbootinit.service.UserService;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 帖子收藏接口
|
||||
*
|
||||
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/post_favour")
|
||||
@Slf4j
|
||||
public class PostFavourController {
|
||||
|
||||
@Resource
|
||||
private PostFavourService postFavourService;
|
||||
|
||||
@Resource
|
||||
private PostService postService;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 收藏 / 取消收藏
|
||||
*
|
||||
* @param postFavourAddRequest
|
||||
* @param request
|
||||
* @return resultNum 收藏变化数
|
||||
*/
|
||||
@PostMapping("/")
|
||||
public BaseResponse<Integer> doPostFavour(@RequestBody PostFavourAddRequest postFavourAddRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postFavourAddRequest == null || postFavourAddRequest.getPostId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
// 登录才能操作
|
||||
final User loginUser = userService.getLoginUser(request);
|
||||
long postId = postFavourAddRequest.getPostId();
|
||||
int result = postFavourService.doPostFavour(postId, loginUser);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取我收藏的帖子列表
|
||||
*
|
||||
* @param postQueryRequest
|
||||
* @param request
|
||||
*/
|
||||
@PostMapping("/my/list/page")
|
||||
public BaseResponse<Page<PostVO>> listMyFavourPostByPage(@RequestBody PostQueryRequest postQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postQueryRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
long current = postQueryRequest.getCurrent();
|
||||
long size = postQueryRequest.getPageSize();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postFavourService.listFavourPostByPage(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postQueryRequest), loginUser.getId());
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户收藏的帖子列表
|
||||
*
|
||||
* @param postFavourQueryRequest
|
||||
* @param request
|
||||
*/
|
||||
@PostMapping("/list/page")
|
||||
public BaseResponse<Page<PostVO>> listFavourPostByPage(@RequestBody PostFavourQueryRequest postFavourQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postFavourQueryRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
long current = postFavourQueryRequest.getCurrent();
|
||||
long size = postFavourQueryRequest.getPageSize();
|
||||
Long userId = postFavourQueryRequest.getUserId();
|
||||
// 限制爬虫
|
||||
ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
|
||||
Page<Post> postPage = postFavourService.listFavourPostByPage(new Page<>(current, size),
|
||||
postService.getQueryWrapper(postFavourQueryRequest.getPostQueryRequest()), userId);
|
||||
return ResultUtils.success(postService.getPostVOPage(postPage, request));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import com.yupi.springbootinit.model.dto.postthumb.PostThumbAddRequest;
|
||||
import com.yupi.springbootinit.model.entity.User;
|
||||
import com.yupi.springbootinit.service.PostThumbService;
|
||||
import com.yupi.springbootinit.service.UserService;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 帖子点赞接口
|
||||
*
|
||||
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/post_thumb")
|
||||
@Slf4j
|
||||
public class PostThumbController {
|
||||
|
||||
@Resource
|
||||
private PostThumbService postThumbService;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 点赞 / 取消点赞
|
||||
*
|
||||
* @param postThumbAddRequest
|
||||
* @param request
|
||||
* @return resultNum 本次点赞变化数
|
||||
*/
|
||||
@PostMapping("/")
|
||||
public BaseResponse<Integer> doThumb(@RequestBody PostThumbAddRequest postThumbAddRequest,
|
||||
HttpServletRequest request) {
|
||||
if (postThumbAddRequest == null || postThumbAddRequest.getPostId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
// 登录才能点赞
|
||||
final User loginUser = userService.getLoginUser(request);
|
||||
long postId = postThumbAddRequest.getPostId();
|
||||
int result = postThumbService.doPostThumb(postId, loginUser);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,320 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yupi.springbootinit.annotation.AuthCheck;
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.DeleteRequest;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.config.WxOpenConfig;
|
||||
import com.yupi.springbootinit.constant.UserConstant;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import com.yupi.springbootinit.exception.ThrowUtils;
|
||||
import com.yupi.springbootinit.model.dto.user.UserAddRequest;
|
||||
import com.yupi.springbootinit.model.dto.user.UserLoginRequest;
|
||||
import com.yupi.springbootinit.model.dto.user.UserQueryRequest;
|
||||
import com.yupi.springbootinit.model.dto.user.UserRegisterRequest;
|
||||
import com.yupi.springbootinit.model.dto.user.UserUpdateMyRequest;
|
||||
import com.yupi.springbootinit.model.dto.user.UserUpdateRequest;
|
||||
import com.yupi.springbootinit.model.entity.User;
|
||||
import com.yupi.springbootinit.model.vo.LoginUserVO;
|
||||
import com.yupi.springbootinit.model.vo.UserVO;
|
||||
import com.yupi.springbootinit.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
|
||||
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static com.yupi.springbootinit.service.impl.UserServiceImpl.SALT;
|
||||
|
||||
/**
|
||||
* 用户接口
|
||||
*
|
||||
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@Slf4j
|
||||
public class UserController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private WxOpenConfig wxOpenConfig;
|
||||
|
||||
// region 登录相关
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param userRegisterRequest
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
|
||||
if (userRegisterRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
String userAccount = userRegisterRequest.getUserAccount();
|
||||
String userPassword = userRegisterRequest.getUserPassword();
|
||||
String checkPassword = userRegisterRequest.getCheckPassword();
|
||||
if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
|
||||
return null;
|
||||
}
|
||||
long result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param userLoginRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录(微信开放平台)
|
||||
*/
|
||||
@GetMapping("/login/wx_open")
|
||||
public BaseResponse<LoginUserVO> userLoginByWxOpen(HttpServletRequest request, HttpServletResponse response,
|
||||
@RequestParam("code") String code) {
|
||||
WxOAuth2AccessToken accessToken;
|
||||
try {
|
||||
WxMpService wxService = wxOpenConfig.getWxMpService();
|
||||
accessToken = wxService.getOAuth2Service().getAccessToken(code);
|
||||
WxOAuth2UserInfo userInfo = wxService.getOAuth2Service().getUserInfo(accessToken, code);
|
||||
String unionId = userInfo.getUnionId();
|
||||
String mpOpenId = userInfo.getOpenid();
|
||||
if (StringUtils.isAnyBlank(unionId, mpOpenId)) {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "登录失败,系统错误");
|
||||
}
|
||||
return ResultUtils.success(userService.userLoginByMpOpen(userInfo, request));
|
||||
} catch (Exception e) {
|
||||
log.error("userLoginByWxOpen error", e);
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "登录失败,系统错误");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注销
|
||||
*
|
||||
* @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 request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get/login")
|
||||
public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
|
||||
User user = userService.getLoginUser(request);
|
||||
return ResultUtils.success(userService.getLoginUserVO(user));
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region 增删改查
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
*
|
||||
* @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);
|
||||
// 默认密码 12345678
|
||||
String defaultPassword = "12345678";
|
||||
String encryptPassword = DigestUtils.md5DigestAsHex((SALT + defaultPassword).getBytes());
|
||||
user.setUserPassword(encryptPassword);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 id 获取用户(仅管理员)
|
||||
*
|
||||
* @param id
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<User> getUserById(long id, HttpServletRequest request) {
|
||||
if (id <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User user = userService.getById(id);
|
||||
ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
|
||||
return ResultUtils.success(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 id 获取包装类
|
||||
*
|
||||
* @param id
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get/vo")
|
||||
public BaseResponse<UserVO> getUserVOById(long id, HttpServletRequest request) {
|
||||
BaseResponse<User> response = getUserById(id, request);
|
||||
User user = response.getData();
|
||||
return ResultUtils.success(userService.getUserVO(user));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取用户列表(仅管理员)
|
||||
*
|
||||
* @param userQueryRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/list/page")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Page<User>> listUserByPage(@RequestBody UserQueryRequest userQueryRequest,
|
||||
HttpServletRequest request) {
|
||||
long current = userQueryRequest.getCurrent();
|
||||
long size = userQueryRequest.getPageSize();
|
||||
Page<User> userPage = userService.page(new Page<>(current, size),
|
||||
userService.getQueryWrapper(userQueryRequest));
|
||||
return ResultUtils.success(userPage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取用户封装列表
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 更新个人信息
|
||||
*
|
||||
* @param userUpdateMyRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/update/my")
|
||||
public BaseResponse<Boolean> updateMyUser(@RequestBody UserUpdateMyRequest userUpdateMyRequest,
|
||||
HttpServletRequest request) {
|
||||
if (userUpdateMyRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
User user = new User();
|
||||
BeanUtils.copyProperties(userUpdateMyRequest, user);
|
||||
user.setId(loginUser.getId());
|
||||
boolean result = userService.updateById(user);
|
||||
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
|
||||
return ResultUtils.success(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import com.yupi.springbootinit.wxmp.WxMpConstant;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 微信公众号相关接口
|
||||
*
|
||||
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
**/
|
||||
@RestController
|
||||
@RequestMapping("/")
|
||||
@Slf4j
|
||||
public class WxMpController {
|
||||
|
||||
@Resource
|
||||
private WxMpService wxMpService;
|
||||
|
||||
@Resource
|
||||
private WxMpMessageRouter router;
|
||||
|
||||
@PostMapping("/")
|
||||
public void receiveMessage(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException {
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
// 校验消息签名,判断是否为公众平台发的消息
|
||||
String signature = request.getParameter("signature");
|
||||
String nonce = request.getParameter("nonce");
|
||||
String timestamp = request.getParameter("timestamp");
|
||||
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
|
||||
response.getWriter().println("非法请求");
|
||||
}
|
||||
// 加密类型
|
||||
String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw"
|
||||
: request.getParameter("encrypt_type");
|
||||
// 明文消息
|
||||
if ("raw".equals(encryptType)) {
|
||||
return;
|
||||
}
|
||||
// aes 加密消息
|
||||
if ("aes".equals(encryptType)) {
|
||||
// 解密消息
|
||||
String msgSignature = request.getParameter("msg_signature");
|
||||
WxMpXmlMessage inMessage = WxMpXmlMessage
|
||||
.fromEncryptedXml(request.getInputStream(), wxMpService.getWxMpConfigStorage(), timestamp,
|
||||
nonce,
|
||||
msgSignature);
|
||||
log.info("message content = {}", inMessage.getContent());
|
||||
// 路由消息并处理
|
||||
WxMpXmlOutMessage outMessage = router.route(inMessage);
|
||||
if (outMessage == null) {
|
||||
response.getWriter().write("");
|
||||
} else {
|
||||
response.getWriter().write(outMessage.toEncryptedXml(wxMpService.getWxMpConfigStorage()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
response.getWriter().println("不可识别的加密类型");
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String check(String timestamp, String nonce, String signature, String echostr) {
|
||||
log.info("check");
|
||||
if (wxMpService.checkSignature(timestamp, nonce, signature)) {
|
||||
return echostr;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置公众号菜单
|
||||
*
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
@GetMapping("/setMenu")
|
||||
public String setMenu() throws WxErrorException {
|
||||
log.info("setMenu");
|
||||
WxMenu wxMenu = new WxMenu();
|
||||
// 菜单一
|
||||
WxMenuButton wxMenuButton1 = new WxMenuButton();
|
||||
wxMenuButton1.setType(MenuButtonType.VIEW);
|
||||
wxMenuButton1.setName("主菜单一");
|
||||
// 子菜单
|
||||
WxMenuButton wxMenuButton1SubButton1 = new WxMenuButton();
|
||||
wxMenuButton1SubButton1.setType(MenuButtonType.VIEW);
|
||||
wxMenuButton1SubButton1.setName("跳转页面");
|
||||
wxMenuButton1SubButton1.setUrl(
|
||||
"https://yupi.icu");
|
||||
wxMenuButton1.setSubButtons(Collections.singletonList(wxMenuButton1SubButton1));
|
||||
|
||||
// 菜单二
|
||||
WxMenuButton wxMenuButton2 = new WxMenuButton();
|
||||
wxMenuButton2.setType(MenuButtonType.CLICK);
|
||||
wxMenuButton2.setName("点击事件");
|
||||
wxMenuButton2.setKey(WxMpConstant.CLICK_MENU_KEY);
|
||||
|
||||
// 菜单三
|
||||
WxMenuButton wxMenuButton3 = new WxMenuButton();
|
||||
wxMenuButton3.setType(MenuButtonType.VIEW);
|
||||
wxMenuButton3.setName("主菜单三");
|
||||
WxMenuButton wxMenuButton3SubButton1 = new WxMenuButton();
|
||||
wxMenuButton3SubButton1.setType(MenuButtonType.VIEW);
|
||||
wxMenuButton3SubButton1.setName("编程学习");
|
||||
wxMenuButton3SubButton1.setUrl("https://yupi.icu");
|
||||
wxMenuButton3.setSubButtons(Collections.singletonList(wxMenuButton3SubButton1));
|
||||
|
||||
// 设置主菜单
|
||||
wxMenu.setButtons(Arrays.asList(wxMenuButton1, wxMenuButton2, wxMenuButton3));
|
||||
wxMpService.getMenuService().menuCreate(wxMenu);
|
||||
return "ok";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user