Просмотр исходного кода

fix: 批量操作添加用户认证和所有权检查

User 5 месяцев назад
Родитель
Сommit
058e5857dd

+ 6 - 4
zxyj-backend/src/main/java/com/zxyj/controller/task/TaskController.java

@@ -129,13 +129,14 @@ public class TaskController {
 
 
     @Operation(summary = "批量完成任务")
     @Operation(summary = "批量完成任务")
     @PostMapping("/batch-complete")
     @PostMapping("/batch-complete")
-    public Result<Boolean> batchComplete(@RequestBody Map<String, Object> params) {
+    public Result<Boolean> batchComplete(@RequestBody Map<String, Object> params,
+                                         @RequestAttribute("userId") Long userId) {
         List<Long> taskIds = (List<Long>) params.get("taskIds");
         List<Long> taskIds = (List<Long>) params.get("taskIds");
         if (taskIds == null || taskIds.isEmpty()) {
         if (taskIds == null || taskIds.isEmpty()) {
             return Result.error("taskIds不能为空");
             return Result.error("taskIds不能为空");
         }
         }
         try {
         try {
-            taskService.batchComplete(taskIds);
+            taskService.batchComplete(taskIds, userId);
             return Result.success(true);
             return Result.success(true);
         } catch (Exception e) {
         } catch (Exception e) {
             return Result.error("批量操作失败: " + e.getMessage());
             return Result.error("批量操作失败: " + e.getMessage());
@@ -144,13 +145,14 @@ public class TaskController {
 
 
     @Operation(summary = "批量删除任务")
     @Operation(summary = "批量删除任务")
     @PostMapping("/batch-delete")
     @PostMapping("/batch-delete")
-    public Result<Boolean> batchDelete(@RequestBody Map<String, Object> params) {
+    public Result<Boolean> batchDelete(@RequestBody Map<String, Object> params,
+                                       @RequestAttribute("userId") Long userId) {
         List<Long> taskIds = (List<Long>) params.get("taskIds");
         List<Long> taskIds = (List<Long>) params.get("taskIds");
         if (taskIds == null || taskIds.isEmpty()) {
         if (taskIds == null || taskIds.isEmpty()) {
             return Result.error("taskIds不能为空");
             return Result.error("taskIds不能为空");
         }
         }
         try {
         try {
-            taskService.batchDelete(taskIds);
+            taskService.batchDelete(taskIds, userId);
             return Result.success(true);
             return Result.success(true);
         } catch (Exception e) {
         } catch (Exception e) {
             return Result.error("批量操作失败: " + e.getMessage());
             return Result.error("批量操作失败: " + e.getMessage());

+ 6 - 6
zxyj-backend/src/main/java/com/zxyj/entity/Task.java

@@ -76,16 +76,16 @@ private String frequency;
 private Integer maxFrequency;
 private Integer maxFrequency;
 
 
 // 完成方式(多选,JSON格式: ["checkin","text","image"])
 // 完成方式(多选,JSON格式: ["checkin","text","image"])
-    private String completeTypes;
+private String completeTypes;
 
 
-    // 预计时长(分钟)
-    private Integer duration;
+// 预计时长(分钟)
+private Integer duration;
 
 
-    // 最早开始时间
+// 最早开始时间
 private Date earliestStart;
 private Date earliestStart;
 
 
-    // 最晚结束时间
-    private Date latestEnd;
+// 最晚结束时间
+private Date latestEnd;
 
 
     // 小游戏代码(当category为'小游戏类'时使用): schulte/1a2b/sudoku
     // 小游戏代码(当category为'小游戏类'时使用): schulte/1a2b/sudoku
     private String minigameCode;
     private String minigameCode;

+ 8 - 4
zxyj-backend/src/main/java/com/zxyj/service/TaskService.java

@@ -475,16 +475,20 @@ return result;
     return result;
     return result;
   }
   }
 
 
-  public void batchComplete(List<Long> taskIds) {
+  public void batchComplete(List<Long> taskIds, Long userId) {
         taskMapper.update(null, new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Task>()
         taskMapper.update(null, new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Task>()
             .in(Task::getId, taskIds)
             .in(Task::getId, taskIds)
+            .and(w -> w.eq(Task::getCreatorId, userId).or().eq(Task::getExecutorId, userId))
             .set(Task::getStatus, "completed")
             .set(Task::getStatus, "completed")
             .set(Task::getCompletedAt, new Date()));
             .set(Task::getCompletedAt, new Date()));
     }
     }
 
 
-    public void batchDelete(List<Long> taskIds) {
-        taskMapper.delete(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Task>()
-            .in(Task::getId, taskIds));
+    public void batchDelete(List<Long> taskIds, Long userId) {
+        taskMapper.update(null, new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Task>()
+            .in(Task::getId, taskIds)
+            .eq(Task::getCreatorId, userId)
+            .set(Task::getStatus, "cancelled")
+            .set(Task::getUpdatedAt, new Date()));
     }
     }
 
 
     private boolean isSameDay(Date date1, Date date2) {
     private boolean isSameDay(Date date1, Date date2) {