该项目分为三段,客户端,即小程序端,管理端,自媒体段,现在涉及到的为自媒体段和客户端
自媒体端发布文章,文章作为任务进入延迟队列,从延迟队列出来后又调用第三方接口,比如阿里云内容安全或者阿里云智能视觉
以下为自媒体端,提交文章
@Override
public ResponseResult submitNews(WmNewsDto dto) throws Exception {
//0.条件判断
if (dto==null||dto.getContent()==null){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
//1.保存或修改文章
WmNews wmNews = new WmNews();
//属性拷贝
BeanUtils.copyProperties(dto,wmNews);
if (dto.getImages()!=null&&dto.getImages().size()>0){
String imageStr = StringUtils.join(dto.getImages(), ",");
wmNews.setImages(imageStr);
}
//如果当前封面类型为自动 -1
if (dto.getType().equals(WemediaConstants.WM_NEWS_TYPE_AUTO)){
wmNews.setType(null);
}
saveOrUpdateWmNews(wmNews);
//2.判断是否为草稿 如果为草稿结束当前方法
if (dto.getStatus().equals(WmNews.Status.NORMAL.getCode())){
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
//3.不是草稿,保存文章内容与素材的关系
//获取到文章内容中的图片信息
List<String> materials = ectractUrlInfo(dto.getContent());
saveRelativeInfoForContent(materials,wmNews.getId());
//4.不是草稿,保存文章封面图片与素材的关系,如果当前布局是自动,需要匹配封面图片
saveRelativeInfoForCover(dto,wmNews,materials);
//审核文章
// wmNewsAutoScanService.autoScanWmNews(wmNews.getId());
wmNewsTaskService.addNewsToTask(wmNews.getId(),wmNews.getPublishTime());
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
加入任务队列
@Override
@Async
public void addNewsToTask(Integer id, Date publishTime) {
log.info("添加任务到延迟服务中----begin");
Task task = new Task();
task.setExecuteTime(publishTime.getTime());
task.setTaskType(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType());
task.setPriority(TaskTypeEnum.NEWS_SCAN_TIME.getPriority());
WmNews wmNews = new WmNews();
wmNews.setId(id);
task.setParameters(ProtostuffUtil.serialize(wmNews));
scheduleClient.addTask(task);
log.info("添加任务到延迟服务中----end");
}
=======================================
@Override
public long addTask(Task task) {
//1.添加任务到数据库中
boolean success=addTaskToDb(task);
if (success){
//2.添加任务到redis
addTaskToCache(task);
}
return task.getTaskId();
}
================================================
审核任务
@Override
@Async
public void autoScanWmNews(Integer id) throws Exception {
//1.查询自媒体文章
WmNews wmNews = wmNewsMapper.selectById(id);
if (wmNews!=null&&wmNews.getStatus().equals(WmNews.Status.SUBMIT.getCode())){
//从内容中提取纯文本内容和图片
Map<String,Object> textAndImages = handleTextAndImages(wmNews);
//自管理的敏感词过滤
boolean isSensitive = handleSensitiveScan((String) textAndImages.get("content"),wmNews);
if (!isSensitive) return;
//2.审核文本内容 阿里云接口
boolean isTextScan = handleTextScan(textAndImages.get("content"),wmNews);
if (!isTextScan) return;
//3.审核图片 阿里云接口
boolean isImageScan = handleImageScan((List<String>) textAndImages.get("images"),wmNews);
if (!isImageScan) return;
//4.审核成功,保存app端的相关的文章数据
ResponseResult responseResult = saveAppArticle(wmNews);
System.out.println(responseResult);
if (responseResult.getCode().equals(200)){
//回填article_id
Object data = responseResult.getData();
Long l = Long.valueOf(String.valueOf(data));
wmNews.setArticleId(l);
updateWmNews(wmNews,(short) 9,"审核成功");
}
}
}
保存文章
private ResponseResult saveAppArticle(WmNews wmNews) throws Exception {
ArticleDto dto = new ArticleDto();
//属性的拷贝
BeanUtils.copyProperties(wmNews,dto);
//文章的布局
dto.setLayout(wmNews.getType());
//频道
WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
if (wmChannel!=null){
dto.setChannelName(wmChannel.getName());
}
//作者
dto.setAuthorId(wmNews.getUserId().longValue());
WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
if (wmUser!=null){
dto.setAuthorName(wmUser.getName());
}
//设置文章id
if (wmNews.getArticleId()!=null){
dto.setId(wmNews.getArticleId());
}
dto.setCreatedTime(new Date());
ResponseResult responseResult = articleClient.saveArticle(dto);
return responseResult;
}
有问题,或者是要源码和资料一键三连,在下方留言