MiniGameController.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.zxyj.controller;
  2. import com.zxyj.common.Result;
  3. import com.zxyj.entity.MiniGame;
  4. import com.zxyj.entity.User;
  5. import com.zxyj.entity.Child;
  6. import com.zxyj.mapper.UserMapper;
  7. import com.zxyj.mapper.ChildMapper;
  8. import com.zxyj.service.MiniGameService;
  9. import io.swagger.v3.oas.annotations.Operation;
  10. import io.swagger.v3.oas.annotations.tags.Tag;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.*;
  13. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  14. import java.util.List;
  15. import java.util.Map;
  16. @Tag(name = "小游戏管理", description = "小游戏相关接口")
  17. @RestController
  18. @RequestMapping("/api/mini-game")
  19. public class MiniGameController {
  20. @Autowired
  21. private MiniGameService miniGameService;
  22. @Autowired
  23. private UserMapper userMapper;
  24. @Autowired
  25. private ChildMapper childMapper;
  26. /**
  27. * 获取启用的小游戏列表
  28. */
  29. @Operation(summary = "获取小游戏列表")
  30. @GetMapping("/list")
  31. public Result<List<MiniGame>> getGameList() {
  32. List<MiniGame> games = miniGameService.getEnabledGames();
  33. return Result.success(games);
  34. }
  35. /**
  36. * 根据游戏代码获取游戏详情
  37. */
  38. @Operation(summary = "获取游戏详情")
  39. @GetMapping("/{gameCode}")
  40. public Result<MiniGame> getGameByCode(@PathVariable String gameCode) {
  41. MiniGame game = miniGameService.getGameByCode(gameCode);
  42. if (game == null) {
  43. return Result.error("游戏不存在");
  44. }
  45. return Result.success(game);
  46. }
  47. /**
  48. * 获取所有小游戏(管理端)
  49. */
  50. @Operation(summary = "获取所有小游戏(管理端)")
  51. @GetMapping("/all")
  52. public Result<List<MiniGame>> getAllGames() {
  53. List<MiniGame> games = miniGameService.getAllGames();
  54. return Result.success(games);
  55. }
  56. /**
  57. * 更新游戏状态
  58. */
  59. @Operation(summary = "更新游戏状态")
  60. @PutMapping("/{id}/status")
  61. public Result<Boolean> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
  62. boolean success = miniGameService.updateGameStatus(id, status);
  63. return Result.success(success);
  64. }
  65. /**
  66. * 完成任务并获得积分
  67. */
  68. @Operation(summary = "游戏完成任务")
  69. @PostMapping("/complete")
  70. public Result<Map<String, Object>> completeGame(
  71. @RequestAttribute("userId") Long userId,
  72. @RequestBody Map<String, Object> body) {
  73. Long childId = Long.valueOf(body.get("childId").toString());
  74. String gameCode = (String) body.get("gameCode");
  75. Integer completionTime = body.get("completionTime") != null ?
  76. Integer.valueOf(body.get("completionTime").toString()) : null;
  77. Integer score = body.get("score") != null ?
  78. Integer.valueOf(body.get("score").toString()) : null;
  79. // 权限校验:验证用户是否有权限操作该孩子
  80. User user = userMapper.selectById(userId);
  81. if (user == null) {
  82. return Result.error("用户不存在");
  83. }
  84. Child child = childMapper.selectById(childId);
  85. if (child == null) {
  86. return Result.error("孩子不存在");
  87. }
  88. // 验证孩子是否属于用户的家庭
  89. if (!child.getFamilyId().equals(user.getFamilyId())) {
  90. return Result.error("无权限操作该孩子数据");
  91. }
  92. try {
  93. Map<String, Object> result = miniGameService.completeGame(childId, gameCode, completionTime, score);
  94. return Result.success(result);
  95. } catch (Exception e) {
  96. return Result.error(e.getMessage());
  97. }
  98. }
  99. }