文件上传https

This commit is contained in:
chen-xin-zhi 2025-03-18 07:41:22 +08:00
parent c318d5d7d4
commit 1cfacc6f3c
13 changed files with 438 additions and 9 deletions

View File

@ -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";
}

View File

@ -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, "添加用户成功");
}

View File

@ -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<Boolean> 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<Boolean> 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<Boolean> 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<List<OfficialAccountArticleVO>> queryAllArticle() {
List<OfficialAccountArticle> officialAccountArticleList = weChatOfficialAccountService.list();
List<OfficialAccountArticleVO> officialAccountArticleVOS = commonService.convertList(officialAccountArticleList, OfficialAccountArticleVO.class);
return ResultUtils.success(officialAccountArticleVOS);
}
/**
* Web端管理员分页查看文章
* @return 用户地址列表
*/
@GetMapping("/query")
@Operation(summary = "Web端管理员分页查看文章", description = "参数权限web端管理员方法名:listArticleByPage")
public BaseResponse<Page<OfficialAccountArticleVO>> listArticleByPage(@RequestBody OfficialAccountArticleQueryRequest officialAccountArticleQueryRequest) {
if (officialAccountArticleQueryRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
long current = officialAccountArticleQueryRequest.getCurrent();
long pageSize = officialAccountArticleQueryRequest.getPageSize();
Page<OfficialAccountArticle> page = weChatOfficialAccountService.page(new Page<>(current, pageSize),null);
List<OfficialAccountArticle> officialAccountArticleList = page.getRecords();
List<OfficialAccountArticleVO> officialAccountArticleVOS = commonService.convertList(officialAccountArticleList, OfficialAccountArticleVO.class);
Page<OfficialAccountArticleVO> 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<Map<String, Object>> getArticleInfo() {
String result1 = null;
try {
@ -106,5 +250,10 @@ public class WeChatController {
}

View File

@ -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<OfficialAccountArticle> {
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -91,6 +91,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> 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用户");
}
}

View File

@ -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<OfficialAccountArticle> {
/**
* 校验
*/
void validArticle(OfficialAccountArticle officialAccountArticle, boolean update);
}

View File

@ -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<OfficialAccountArticleMapper, OfficialAccountArticle> 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)) {
}
}
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cultural.heritage.mapper.OfficialAccountArticleMapper">
</mapper>