From 1cfacc6f3cdb12adf17ec440d220ca0c9334723c Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Tue, 18 Mar 2025 07:41:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0https?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../heritage/constant/FileConstant.java | 3 +- .../controller/user/UserController.java | 4 +- .../controller/wx/WeChatController.java | 159 +++++++++++++++++- .../mapper/OfficialAccountArticleMapper.java | 8 + .../OfficialAccountArticleAddRequest.java | 47 ++++++ .../OfficialAccountArticleQueryRequest.java | 19 +++ .../OfficialAccountArticleUpdateRequest.java | 53 ++++++ .../model/entity/OfficialAccountArticle.java | 53 ++++++ .../vo/article/OfficialAccountArticleVO.java | 43 +++++ .../service/user/impl/UserServiceImpl.java | 4 + .../wx/WeChatOfficialAccountService.java | 12 ++ .../WeChatOfficialAccountServiceImpl.java | 35 ++++ .../mapper/OfficialAccountArticleMapper.xml | 7 + 13 files changed, 438 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/mapper/OfficialAccountArticleMapper.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleAddRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleQueryRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleUpdateRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/entity/OfficialAccountArticle.java create mode 100644 src/main/java/com/cultural/heritage/model/vo/article/OfficialAccountArticleVO.java create mode 100644 src/main/java/com/cultural/heritage/service/wx/WeChatOfficialAccountService.java create mode 100644 src/main/java/com/cultural/heritage/service/wx/impl/WeChatOfficialAccountServiceImpl.java create mode 100644 src/main/resources/mapper/OfficialAccountArticleMapper.xml diff --git a/src/main/java/com/cultural/heritage/constant/FileConstant.java b/src/main/java/com/cultural/heritage/constant/FileConstant.java index 835dc4a..8cf5a1c 100644 --- a/src/main/java/com/cultural/heritage/constant/FileConstant.java +++ b/src/main/java/com/cultural/heritage/constant/FileConstant.java @@ -11,6 +11,7 @@ public interface FileConstant { // String SERVER_HOST = "https://www.carboner.cn:8888/api/file/downloadFile?objectKey="; - String SERVER_HOST = ""; + // 公众号文章路径 + String OFFICIAL_ACCOUNT_ARTICLE = "https://mp.weixin.qq.com"; } diff --git a/src/main/java/com/cultural/heritage/controller/user/UserController.java b/src/main/java/com/cultural/heritage/controller/user/UserController.java index 0c0d858..e9a87bc 100644 --- a/src/main/java/com/cultural/heritage/controller/user/UserController.java +++ b/src/main/java/com/cultural/heritage/controller/user/UserController.java @@ -210,9 +210,7 @@ public class UserController { user.setUserPassword(encryptPassword); userService.validUser(user, false); boolean save = userService.save(user); - if (!save) { - throw new BusinessException(ErrorCode.SYSTEM_ERROR); - } + ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR, "用户添加失败"); return ResultUtils.success(user, "添加用户成功"); } diff --git a/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java b/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java index 1818a8a..030b1eb 100644 --- a/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java +++ b/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java @@ -3,32 +3,176 @@ package com.cultural.heritage.controller.wx; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.cultural.heritage.annotation.AuthCheck; import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; +import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; +import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.model.dto.CommonRequest; +import com.cultural.heritage.model.dto.article.OfficialAccountArticleAddRequest; +import com.cultural.heritage.model.dto.article.OfficialAccountArticleQueryRequest; +import com.cultural.heritage.model.dto.article.OfficialAccountArticleUpdateRequest; +import com.cultural.heritage.model.entity.OfficialAccountArticle; +import com.cultural.heritage.model.vo.article.OfficialAccountArticleVO; +import com.cultural.heritage.service.common.CommonService; +import com.cultural.heritage.service.wx.WeChatOfficialAccountService; import com.cultural.heritage.utils.ReflexObjectUtil; +import io.swagger.v3.oas.annotations.Hidden; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.beans.BeanUtils; +import org.springframework.web.bind.annotation.*; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; +import java.util.List; import java.util.Map; @RestController -@RequestMapping("/wx") +@RequestMapping("/article") @Slf4j -@Tag(name = "小程序获取公众号文章") +@Tag(name = "公众号模块") public class WeChatController { + + @Resource + private WeChatOfficialAccountService weChatOfficialAccountService; + + + + @Resource + private CommonService commonService; + + + /** + * web端管理员添加文章 + * @param officialAccountArticleAddRequest 文章添加请求体 + * @return 是否添加成功 + */ + @PostMapping("/add") + @Operation(summary = "web端管理员添加文章", description = "参数:地址添加请求体,权限:所有人,方法名:addArticle") + @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) + public BaseResponse addArticle(@RequestBody OfficialAccountArticleAddRequest officialAccountArticleAddRequest) { + if (officialAccountArticleAddRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + OfficialAccountArticle officialAccountArticle = new OfficialAccountArticle(); + BeanUtils.copyProperties(officialAccountArticleAddRequest, officialAccountArticle); + weChatOfficialAccountService.validArticle(officialAccountArticle, false); + boolean result = weChatOfficialAccountService.save(officialAccountArticle); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "文章添加失败"); + return ResultUtils.success(true); + } + + + + /** + * web端管理员删除文章 + * @param deleteRequest 文章id + * @return 是否成功删除 + */ + @PostMapping("/delete") + @Operation(summary = "web端管理员删除文章", description = "参数:文章id,权限:所有人,方法名:delArticle") + @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) + public BaseResponse delArticle(@RequestBody CommonRequest deleteRequest) { + if (deleteRequest == null || deleteRequest.getId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Long id = deleteRequest.getId(); + boolean result = weChatOfficialAccountService.removeById(id); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "文章删除失败"); + return ResultUtils.success(true); + } + + + + + /** + * web端管理员更新文章 + * @param officialAccountArticleUpdateRequest 文章更新请求体 + * @return 是否更新成功 + */ + @PostMapping("/update") + @Operation(summary = "web端管理员更新文章", description = "参数:文章更新请求体,权限:所有人,方法名:updateArticle") + @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) + public BaseResponse updateArticle(@RequestBody OfficialAccountArticleUpdateRequest officialAccountArticleUpdateRequest) { + if (officialAccountArticleUpdateRequest == null || officialAccountArticleUpdateRequest.getId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + OfficialAccountArticle officialAccountArticle = new OfficialAccountArticle(); + BeanUtils.copyProperties(officialAccountArticleUpdateRequest, officialAccountArticle); + weChatOfficialAccountService.validArticle(officialAccountArticle, true); + boolean result = weChatOfficialAccountService.updateById(officialAccountArticle); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "文章更新失败"); + return ResultUtils.success(true); + } + + + + + /** + * 小程序用户查看文章 + * @return 用户地址列表 + */ + @GetMapping("/query") + @Operation(summary = "小程序用户查看文章", description = "参数:无,权限:所有人,方法名:queryAllArticle") + public BaseResponse> queryAllArticle() { + List officialAccountArticleList = weChatOfficialAccountService.list(); + List officialAccountArticleVOS = commonService.convertList(officialAccountArticleList, OfficialAccountArticleVO.class); + return ResultUtils.success(officialAccountArticleVOS); + } + + + + + /** + * Web端管理员分页查看文章 + * @return 用户地址列表 + */ + @GetMapping("/query") + @Operation(summary = "Web端管理员分页查看文章", description = "参数:无,权限:web端管理员,方法名:listArticleByPage") + public BaseResponse> listArticleByPage(@RequestBody OfficialAccountArticleQueryRequest officialAccountArticleQueryRequest) { + if (officialAccountArticleQueryRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR); + long current = officialAccountArticleQueryRequest.getCurrent(); + long pageSize = officialAccountArticleQueryRequest.getPageSize(); + Page page = weChatOfficialAccountService.page(new Page<>(current, pageSize),null); + List officialAccountArticleList = page.getRecords(); + List officialAccountArticleVOS = commonService.convertList(officialAccountArticleList, OfficialAccountArticleVO.class); + Page voPage = new Page<>(); + voPage.setRecords(officialAccountArticleVOS); + voPage.setPages(page.getPages()); + voPage.setCurrent(page.getCurrent()); + voPage.setTotal(page.getTotal()); + voPage.setSize(page.getSize()); + return ResultUtils.success(voPage); + } + + + + + + + + + + + + + + + + @PostMapping("/getArticle") + @Hidden public BaseResponse> getArticleInfo() { String result1 = null; try { @@ -106,5 +250,10 @@ public class WeChatController { + + + + + } diff --git a/src/main/java/com/cultural/heritage/mapper/OfficialAccountArticleMapper.java b/src/main/java/com/cultural/heritage/mapper/OfficialAccountArticleMapper.java new file mode 100644 index 0000000..fc94f36 --- /dev/null +++ b/src/main/java/com/cultural/heritage/mapper/OfficialAccountArticleMapper.java @@ -0,0 +1,8 @@ +package com.cultural.heritage.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.cultural.heritage.model.entity.OfficialAccountArticle; + +public interface OfficialAccountArticleMapper extends BaseMapper { + +} diff --git a/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleAddRequest.java new file mode 100644 index 0000000..432bf39 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleAddRequest.java @@ -0,0 +1,47 @@ +package com.cultural.heritage.model.dto.article; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +@Schema(description = "公众号文章添加请求体", requiredProperties = { + "title", "image", "publishTime", "url" +}) +public class OfficialAccountArticleAddRequest implements Serializable { + + + /** + * 文章标题 + */ + @Schema(description = "文章标题") + private String title; + + + /** + * 文章图片 + */ + @Schema(description = "文章图片") + private String image; + + + /** + * 文章发布时间 + */ + @Schema(description = "文章发布时间") + private String publishTime; + + + /** + * 文章链接 + */ + @Schema(description = "文章链接") + private String url; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleQueryRequest.java b/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleQueryRequest.java new file mode 100644 index 0000000..8a8653e --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleQueryRequest.java @@ -0,0 +1,19 @@ +package com.cultural.heritage.model.dto.article; + +import com.cultural.heritage.common.PageRequest; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + + +@Data +public class OfficialAccountArticleQueryRequest extends PageRequest implements Serializable { + + + + + @Serial + private static final long serialVersionUID = 1L; + +} diff --git a/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleUpdateRequest.java new file mode 100644 index 0000000..cb440db --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/article/OfficialAccountArticleUpdateRequest.java @@ -0,0 +1,53 @@ +package com.cultural.heritage.model.dto.article; + + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + + +@Data +@Schema(description = "公众号文章更新请求体", requiredProperties = { + "id", "title", "image", "publishTime", "url" +}) +public class OfficialAccountArticleUpdateRequest implements Serializable { + + /** + * id + */ + @Schema(description = "id") + private Long id; + + /** + * 文章标题 + */ + @Schema(description = "文章标题") + private String title; + + + /** + * 文章图片 + */ + @Schema(description = "文章图片") + private String image; + + + /** + * 文章发布时间 + */ + @Schema(description = "文章发布时间") + private String publishTime; + + + /** + * 文章链接 + */ + @Schema(description = "文章链接") + private String url; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/OfficialAccountArticle.java b/src/main/java/com/cultural/heritage/model/entity/OfficialAccountArticle.java new file mode 100644 index 0000000..6fbbe86 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/OfficialAccountArticle.java @@ -0,0 +1,53 @@ +package com.cultural.heritage.model.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + + +/** + * 商品 + * @TableName official_account_article + */ +@Data +@TableName(value = "official_account_article") +public class OfficialAccountArticle implements Serializable { + + /** + * 文章id + */ + @TableId(type = IdType.AUTO) + private Long id; + + /** + * 文章标题 + */ + private String title; + + + /** + * 文章图片 + */ + private String image; + + + /** + * 发布时间 + */ + private String publishTime; + + + /** + * 文章链接 + */ + private String url; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/vo/article/OfficialAccountArticleVO.java b/src/main/java/com/cultural/heritage/model/vo/article/OfficialAccountArticleVO.java new file mode 100644 index 0000000..dacdc04 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/vo/article/OfficialAccountArticleVO.java @@ -0,0 +1,43 @@ +package com.cultural.heritage.model.vo.article; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class OfficialAccountArticleVO implements Serializable { + + /** + * 文章id + */ + private Long id; + + /** + * 文章标题 + */ + private String title; + + + /** + * 文章图片 + */ + private String image; + + + /** + * 发布时间 + */ + private String publishTime; + + + /** + * 文章链接 + */ + private String url; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java b/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java index b80aa73..1197547 100644 --- a/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/user/impl/UserServiceImpl.java @@ -91,6 +91,10 @@ public class UserServiceImpl extends ServiceImpl implements Us if (StringUtils.isAnyBlank(userName, userAvatar, userRole, userPassword)) { throw new BusinessException(ErrorCode.PARAMS_ERROR, "存在参数为空"); } + UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValues(userRole); + if (UserRoleEnum.BOSS.equals(userRoleEnum)) { + throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无法创建boss用户"); + } } diff --git a/src/main/java/com/cultural/heritage/service/wx/WeChatOfficialAccountService.java b/src/main/java/com/cultural/heritage/service/wx/WeChatOfficialAccountService.java new file mode 100644 index 0000000..3e72cdb --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/wx/WeChatOfficialAccountService.java @@ -0,0 +1,12 @@ +package com.cultural.heritage.service.wx; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.entity.OfficialAccountArticle; + +public interface WeChatOfficialAccountService extends IService { + + /** + * 校验 + */ + void validArticle(OfficialAccountArticle officialAccountArticle, boolean update); +} diff --git a/src/main/java/com/cultural/heritage/service/wx/impl/WeChatOfficialAccountServiceImpl.java b/src/main/java/com/cultural/heritage/service/wx/impl/WeChatOfficialAccountServiceImpl.java new file mode 100644 index 0000000..99a764a --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/wx/impl/WeChatOfficialAccountServiceImpl.java @@ -0,0 +1,35 @@ +package com.cultural.heritage.service.wx.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.mapper.OfficialAccountArticleMapper; +import com.cultural.heritage.model.entity.OfficialAccountArticle; +import com.cultural.heritage.service.wx.WeChatOfficialAccountService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +@Service +public class WeChatOfficialAccountServiceImpl extends ServiceImpl implements WeChatOfficialAccountService { + + + /** + * 校验 + */ + @Override + public void validArticle(OfficialAccountArticle officialAccountArticle, boolean update) { + Long id = officialAccountArticle.getId(); + String title = officialAccountArticle.getTitle(); + String publishTime = officialAccountArticle.getPublishTime(); + String url = officialAccountArticle.getUrl(); + String image = officialAccountArticle.getImage(); + + ThrowUtils.throwIf(update && id == null, ErrorCode.OPERATION_ERROR, "id参数错误"); + + if (StringUtils.isAnyBlank(title, publishTime, url, image)) { + + } + + + } +} diff --git a/src/main/resources/mapper/OfficialAccountArticleMapper.xml b/src/main/resources/mapper/OfficialAccountArticleMapper.xml new file mode 100644 index 0000000..9c9dda2 --- /dev/null +++ b/src/main/resources/mapper/OfficialAccountArticleMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file