Commit 6eed8780 authored by 万成波's avatar 万成波

优化动态发布

parent e3cd8cd3
package com.tangguo.common.utils;
import javax.servlet.http.HttpServletRequest;
public class ServerConfigUtils {
public static String getDomain(HttpServletRequest request) {
StringBuffer url = request.getRequestURL();
String contextPath = request.getServletContext().getContextPath();
return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
}
/**
* 获取完整的请求路径,包括:域名,端口,上下文访问路径
*
* @return 服务地址
*/
public static String getUrl() {
HttpServletRequest request = ServletUtils.getRequest();
return getDomain(request);
}
}
package com.tangguo.controller.mobile;
import com.tangguo.common.config.RuoYiConfig;
import com.tangguo.common.core.domain.AjaxResult;
import com.tangguo.common.utils.ServerConfigUtils;
import com.tangguo.common.utils.file.FileUploadUtils;
import com.tangguo.common.utils.file.FileUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* 移动端通用控制器
*
* @author 谈笑
* @createTime 2025-09-03 16:51:34 星期三
*/
@RestController
@RequestMapping("/bbs/mobile/common")
public class MCommonController {
/**
* 通用上传请求(单个)
*/
@PostMapping("/upload")
public AjaxResult uploadFile(MultipartFile file) {
try {
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = ServerConfigUtils.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("url", url);
ajax.put("fileName", fileName);
ajax.put("newFileName", FileUtils.getName(fileName));
ajax.put("originalFilename", file.getOriginalFilename());
return ajax;
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
}
...@@ -16,13 +16,13 @@ public class CreateMomentBO { ...@@ -16,13 +16,13 @@ public class CreateMomentBO {
/** 动态内容 */ /** 动态内容 */
private String content; private String content;
/** 动态类型:IMAGE 图片动态、VIDEO 视频动态 */ /** 动态类型:IMAGE 图片动态、VIDEO 视频动态、 URL 链接动态*/
private String type; private String type;
/** 话题名称(逗号分割) */ /** 话题名称*/
private List<String> topicNames; private List<String> topicNames;
/** 话题Ids(逗号分割) */ /** 话题Ids */
private List<String> topicIds; private List<String> topicIds;
/** 外部链接地址 */ /** 外部链接地址 */
...@@ -46,9 +46,6 @@ public class CreateMomentBO { ...@@ -46,9 +46,6 @@ public class CreateMomentBO {
/** 投票选项类型:TEXT 文字、IMAGE 图片 */ /** 投票选项类型:TEXT 文字、IMAGE 图片 */
private String voteOptionType; private String voteOptionType;
/** 动态附件 */
private List<Attachment> attachments;
/** 动态投票选项 */ /** 动态投票选项 */
private List<VoteOption> voteOptions; private List<VoteOption> voteOptions;
......
package com.tangguo.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* 动态类型
*
* @author 谈笑
* @createTime 2025-09-03 17:11:39 星期三
*/
@Getter
@AllArgsConstructor
public enum MomentType {
IMAGE("图片动态"), VIDEO("视频动态"), URL("链接动态");
private final String desc;
public static MomentType getMomentType(String type) {
return Arrays.stream(MomentType.values())
.filter(t -> t.name().equals(type)).findFirst().orElse(null);
}
}
package com.tangguo.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* 动态选项投票类型
*
* @author 谈笑
* @createTime 2025-09-03 17:11:39 星期三
*/
@Getter
@AllArgsConstructor
public enum VoteOptionType {
TEXT("文本投票"), IMAGE("图片投票");
private final String desc;
public static VoteOptionType getVoteOptionType(String type) {
return Arrays.stream(VoteOptionType.values())
.filter(t -> t.name().equals(type)).findFirst().orElse(null);
}
}
...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.tangguo.domain.BbsMomentAttachment; import com.tangguo.domain.BbsMomentAttachment;
import com.tangguo.domain.BbsMomentVoteOption; import com.tangguo.domain.BbsMomentVoteOption;
import com.tangguo.domain.bo.CreateMomentBO; import com.tangguo.domain.bo.CreateMomentBO;
import com.tangguo.enums.VoteOptionType;
import java.util.List; import java.util.List;
...@@ -40,5 +41,4 @@ public interface IBbsMomentVoteOptionService extends IService<BbsMomentVoteOptio ...@@ -40,5 +41,4 @@ public interface IBbsMomentVoteOptionService extends IService<BbsMomentVoteOptio
*/ */
void deleteVoteOptions(Long momentId); void deleteVoteOptions(Long momentId);
} }
package com.tangguo.service.impl; package com.tangguo.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.exception.ServiceException; import com.tangguo.common.exception.ServiceException;
...@@ -9,16 +10,18 @@ import com.tangguo.domain.BbsMoment; ...@@ -9,16 +10,18 @@ import com.tangguo.domain.BbsMoment;
import com.tangguo.domain.bo.CreateMomentBO; import com.tangguo.domain.bo.CreateMomentBO;
import com.tangguo.domain.vo.BbsUserMomentCountVO; import com.tangguo.domain.vo.BbsUserMomentCountVO;
import com.tangguo.enums.EnableStatus; import com.tangguo.enums.EnableStatus;
import com.tangguo.enums.MomentType;
import com.tangguo.enums.VoteOptionType;
import com.tangguo.mapper.BbsMomentMapper; import com.tangguo.mapper.BbsMomentMapper;
import com.tangguo.service.IBbsMomentAttachmentService; import com.tangguo.service.IBbsMomentAttachmentService;
import com.tangguo.service.IBbsMomentService; import com.tangguo.service.IBbsMomentService;
import com.tangguo.service.IBbsMomentVoteOptionService; import com.tangguo.service.IBbsMomentVoteOptionService;
import org.apache.ibatis.parsing.TokenHandler;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* 动态Service业务层处理 * 动态Service业务层处理
...@@ -77,10 +80,6 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -77,10 +80,6 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
newMoment.setUserName(username); newMoment.setUserName(username);
this.save(newMoment); this.save(newMoment);
// 添加动态附件
List<CreateMomentBO.Attachment> attachments = bo.getAttachments();
this.attachmentsService.addAttachments(username, newMoment.getId(), attachments);
// 添加动态投票选项 // 添加动态投票选项
if (EnableStatus.QY.getStatus() == bo.getIsEnableVote()) { if (EnableStatus.QY.getStatus() == bo.getIsEnableVote()) {
List<CreateMomentBO.VoteOption> voteOptions = bo.getVoteOptions(); List<CreateMomentBO.VoteOption> voteOptions = bo.getVoteOptions();
...@@ -96,46 +95,88 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -96,46 +95,88 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
* @return 动态实体 * @return 动态实体
*/ */
private BbsMoment buildBbsMoment(CreateMomentBO bo) { private BbsMoment buildBbsMoment(CreateMomentBO bo) {
final String separator = "|";
// 动态类型
MomentType momentType = MomentType.getMomentType(bo.getType());
if (Objects.isNull(momentType)) {
throw new ServiceException("发布失败,动态类型错误。");
}
// 动态附件
List<String> attachmentUrls = bo.getAttachmentUrls();
String linkUrl = bo.getLinkUrl();
if (MomentType.IMAGE == momentType || MomentType.VIDEO == momentType) {
if (CollUtil.isEmpty(attachmentUrls)) {
throw new ServiceException("发布失败,动态附件不能为空。");
}
}
else if (MomentType.URL == momentType) {
if (StrUtil.isBlank(linkUrl)) {
throw new ServiceException("发布失败,动态链接不能为空。");
}
}
BbsMoment newMoment = new BbsMoment(); BbsMoment newMoment = new BbsMoment();
newMoment.setType(momentType.name());
newMoment.setContent(bo.getContent()); newMoment.setContent(bo.getContent());
newMoment.setType(bo.getType()); newMoment.setLinkUrl(linkUrl);
newMoment.setLinkUrl(bo.getLinkUrl()); newMoment.setAttachmentUrls(String.join(separator, attachmentUrls));
newMoment.setIsEnableComment(bo.getIsEnableComment()); newMoment.setIsEnableComment(bo.getIsEnableComment());
newMoment.setIsEnableFeaturedComment(bo.getIsEnableFeaturedComment()); newMoment.setIsEnableFeaturedComment(bo.getIsEnableFeaturedComment());
// 动态主题 // 动态主题
List<String> topicNames = bo.getTopicNames(); List<String> topicNames = bo.getTopicNames();
if (CollUtil.isNotEmpty(topicNames)) {
newMoment.setTopicNames(String.join(",", topicNames));
}
List<String> topicIds = bo.getTopicIds(); List<String> topicIds = bo.getTopicIds();
if (CollUtil.isNotEmpty(topicIds)) { if (CollUtil.isNotEmpty(topicNames) && CollUtil.isNotEmpty(topicIds)) {
newMoment.setTopicIds(String.join(",", topicIds)); newMoment.setTopicIds(String.join(separator, topicIds));
} newMoment.setTopicNames(String.join(separator, topicNames));
// 动态附件
List<String> attachmentUrls = bo.getAttachmentUrls();
if (CollUtil.isNotEmpty(attachmentUrls)) {
newMoment.setAttachmentUrls(String.join(",", attachmentUrls));
} }
// 动态投票 // 动态投票
Integer isEnableVote = bo.getIsEnableVote(); Integer isEnableVote = bo.getIsEnableVote();
newMoment.setIsEnableVote(isEnableVote); newMoment.setIsEnableVote(isEnableVote);
if (EnableStatus.QY.getStatus() == isEnableVote) { if (EnableStatus.QY.getStatus() == isEnableVote) {
newMoment.setVoteTitle(bo.getVoteTitle()); VoteOptionType optionType = VoteOptionType.getVoteOptionType(bo.getVoteOptionType());
newMoment.setVoteOptionType(bo.getVoteOptionType()); if (Objects.isNull(optionType)) {
List<CreateMomentBO.VoteOption> voteOptions = bo.getVoteOptions(); throw new ServiceException("发布失败,动态投票选项类型错误。");
for (int i = 0; i < voteOptions.size(); i++) { }
int index = i + 1; List<CreateMomentBO.VoteOption> options = bo.getVoteOptions();
CreateMomentBO.VoteOption option = voteOptions.get(i); if (CollUtil.isEmpty(options)) {
option.setType(bo.getVoteOptionType()); throw new ServiceException("发布失败,动态投票选项不能为空。");
option.setCode(String.valueOf(index));
option.setSort(index);
} }
newMoment.setVoteOptions(JSONUtil.toJsonStr(voteOptions)); newMoment.setVoteTitle(bo.getVoteTitle());
newMoment.setVoteOptionType(optionType.name());
String optionsJson = this.buildVoteOptionsJson(optionType, options);
newMoment.setVoteOptions(optionsJson);
} }
return newMoment; return newMoment;
} }
/**
* 构建动态投票选项Json
*
* @param type 选项类型
* @param options 选项
* @return 选项Json
*/
public String buildVoteOptionsJson(VoteOptionType type, List<CreateMomentBO.VoteOption> options) {
if (CollUtil.isEmpty(options)) {
throw new ServiceException("发布失败,动态投票选项不能为空。");
}
for (int i = 0; i < options.size(); i++) {
CreateMomentBO.VoteOption option = options.get(i);
long optionCount = options.stream().filter(o -> o.getName().equals(option.getName())).count();
if (optionCount > 1) {
throw new ServiceException("发布失败,动态投票选项重复。");
}
option.setType(type.name());
int optionIndex = i + 1;
option.setCode(String.valueOf(optionIndex));
option.setSort(optionIndex);
}
return JSONUtil.toJsonStr(options);
}
} }
package com.tangguo.service.impl; package com.tangguo.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.exception.ServiceException;
import com.tangguo.domain.BbsMomentVoteOption; import com.tangguo.domain.BbsMomentVoteOption;
import com.tangguo.domain.bo.CreateMomentBO; import com.tangguo.domain.bo.CreateMomentBO;
import com.tangguo.enums.VoteOptionType;
import com.tangguo.mapper.BbsMomentVoteOptionMapper; import com.tangguo.mapper.BbsMomentVoteOptionMapper;
import com.tangguo.service.IBbsMomentVoteOptionService; import com.tangguo.service.IBbsMomentVoteOptionService;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment