diff --git a/msh_crmeb_22/crmeb-front/src/main/java/com/zbkj/front/controller/ToolController.java b/msh_crmeb_22/crmeb-front/src/main/java/com/zbkj/front/controller/ToolController.java index 8fea090..c5f3013 100644 --- a/msh_crmeb_22/crmeb-front/src/main/java/com/zbkj/front/controller/ToolController.java +++ b/msh_crmeb_22/crmeb-front/src/main/java/com/zbkj/front/controller/ToolController.java @@ -7,6 +7,7 @@ import com.zbkj.common.request.PageParamRequest; import com.zbkj.common.response.NutritionAdoptResponse; import com.zbkj.common.response.NutritionCalculateResponse; import com.zbkj.common.result.CommonResult; +import com.zbkj.common.token.FrontTokenComponent; import com.zbkj.service.service.tool.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -65,6 +66,9 @@ public class ToolController { @Autowired private ToolUploadService toolUploadService; + @Autowired + private FrontTokenComponent frontTokenComponent; + // ==================== 食谱计算器相关 ==================== /** @@ -230,6 +234,17 @@ public class ToolController { return CommonResult.success(toolCheckinService.learn((Long) data.get("sourcePostId"), data)); } + /** + * 分享打卡记录到社区 + */ + @ApiOperation(value = "分享打卡记录到社区") + @PostMapping("/checkin/{checkinId}/share-to-community") + public CommonResult> shareCheckinToCommunity( + @ApiParam(value = "打卡记录ID", required = true) @PathVariable Integer checkinId) { + Integer userId = frontTokenComponent.getUserId(); + return CommonResult.success(toolCheckinService.shareCheckinToCommunity(checkinId, userId)); + } + // ==================== 食物百科相关 ==================== /** diff --git a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCheckinServiceImpl.java b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCheckinServiceImpl.java index 732c641..3311a3d 100644 --- a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCheckinServiceImpl.java +++ b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCheckinServiceImpl.java @@ -663,4 +663,108 @@ public class ToolCheckinServiceImpl implements ToolCheckinService { return "饮食"; } } + + /** + * 分享打卡记录到社区 + * + * @param checkinId 打卡记录ID + * @param userId 当前用户ID + * @return 创建的帖子信息 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public Map shareCheckinToCommunity(Integer checkinId, Integer userId) { + if (ObjectUtil.isNull(checkinId)) { + throw new CrmebException("打卡记录ID不能为空"); + } + if (ObjectUtil.isNull(userId)) { + throw new CrmebException("用户ID不能为空"); + } + + // 1. 获取打卡记录详情 + UserSign sign = userSignDao.selectById(checkinId); + if (sign == null) { + throw new CrmebException("打卡记录不存在"); + } + + // 2. 检查打卡记录是否属于当前用户 + if (!sign.getUid().equals(userId)) { + throw new CrmebException("无权分享他人的打卡记录"); + } + + // 3. 检查是否已分享过(可选:根据需求可以更新或报错) + LambdaQueryWrapper existingQuery = new LambdaQueryWrapper() + .eq(V2CommunityPost::getCheckInRecordId, checkinId) + .eq(V2CommunityPost::getUserId, userId.longValue()) + .orderByDesc(V2CommunityPost::getCreatedAt) + .last("LIMIT 1"); + V2CommunityPost existingPost = v2CommunityPostDao.selectOne(existingQuery); + + if (existingPost != null) { + // 已分享过,返回已有的帖子ID + Map result = new HashMap<>(); + result.put("postId", existingPost.getPostId()); + result.put("message", "该打卡记录已分享到社区"); + return result; + } + + // 4. 生成标题 + String mealTypeLabel = getMealTypeLabel(sign.getMealType()); + String title = mealTypeLabel + "打卡"; + + // 5. 准备营养数据JSON(如果有AI分析结果) + String nutritionDataJson = null; + if (StrUtil.isNotBlank(sign.getAiRecognizedFoodsJson())) { + try { + Map nutritionData = new HashMap<>(); + nutritionData.put("aiRecognizedFoods", sign.getAiRecognizedFoodsJson()); + nutritionData.put("aiStatus", sign.getAiRecognitionStatus()); + nutritionData.put("actualProtein", sign.getActualProtein()); + nutritionData.put("actualEnergy", sign.getActualEnergy()); + nutritionData.put("nutritionScore", sign.getNutritionScore()); + nutritionDataJson = com.alibaba.fastjson.JSON.toJSONString(nutritionData); + } catch (Exception e) { + log.warn("生成营养数据JSON失败: {}", e.getMessage()); + } + } + + // 6. 解析封面图 + String coverImage = parseFirstImage(sign.getPhotosJson()); + + // 7. 创建社区帖子 + V2CommunityPost communityPost = new V2CommunityPost(); + communityPost.setUserId(userId.longValue()); + communityPost.setCheckInRecordId(checkinId); + communityPost.setTitle(title); + communityPost.setContent(sign.getNotes()); + communityPost.setCoverImage(coverImage); + communityPost.setImagesJson(sign.getPhotosJson()); + communityPost.setNutritionDataJson(nutritionDataJson); + communityPost.setStatus("published"); + communityPost.setPrivacy("public"); + communityPost.setAuditStatus("approved"); // 自动审核通过 + communityPost.setCreatedAt(new Date()); + communityPost.setUpdatedAt(new Date()); + communityPost.setLikeCount(0); + communityPost.setCommentCount(0); + communityPost.setCollectCount(0); + communityPost.setShareCount(0); + communityPost.setViewCount(0); + communityPost.setQuickCheckinCount(0); + communityPost.setAiVideoCount(0); + communityPost.setRecommendScore(new BigDecimal("0.00")); + communityPost.setHotScore(new BigDecimal("0.00")); + + v2CommunityPostDao.insert(communityPost); + + log.info("分享打卡记录到社区成功,postId: {}, checkinId: {}, userId: {}", + communityPost.getPostId(), checkinId, userId); + + // 8. 返回结果 + Map result = new HashMap<>(); + result.put("postId", communityPost.getPostId()); + result.put("title", title); + result.put("message", "分享成功"); + return result; + } } diff --git a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/tool/ToolCheckinService.java b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/tool/ToolCheckinService.java index e49e4e5..5757cc4 100644 --- a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/tool/ToolCheckinService.java +++ b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/tool/ToolCheckinService.java @@ -70,5 +70,13 @@ public interface ToolCheckinService { * @return 新打卡记录信息 */ Map learn(Long sourcePostId, Map data); + + /** + * 分享打卡记录到社区 + * @param checkinId 打卡记录ID + * @param userId 当前用户ID + * @return 创建的帖子信息 + */ + Map shareCheckinToCommunity(Integer checkinId, Integer userId); } diff --git a/msh_single_uniapp/api/tool.js b/msh_single_uniapp/api/tool.js index a46be5f..48b1340 100644 --- a/msh_single_uniapp/api/tool.js +++ b/msh_single_uniapp/api/tool.js @@ -177,6 +177,14 @@ export function learnCheckin(sourcePostId, data) { }); } +/** + * 分享打卡记录到社区 + * @param {Number} checkinId - 打卡记录ID + */ +export function shareCheckinToCommunity(checkinId) { + return request.post('tool/checkin/' + checkinId + '/share-to-community'); +} + // ==================== 食物百科相关 ==================== /** diff --git a/msh_single_uniapp/pages/tool/checkin-detail.vue b/msh_single_uniapp/pages/tool/checkin-detail.vue index cd19a69..7246a3e 100644 --- a/msh_single_uniapp/pages/tool/checkin-detail.vue +++ b/msh_single_uniapp/pages/tool/checkin-detail.vue @@ -101,7 +101,7 @@