auth(); $targetType = strtolower(trim((string)$this->input('targetType', ''))); if (!in_array($targetType, ['post', 'comment'], true)) { $this->fail('举报类型不支持'); } $reason = trim((string)$this->input('reason', '')); if ($reason === '') { $this->fail('请选择举报原因'); } if (mb_strlen($reason) > 60) { $reason = mb_substr($reason, 0, 60); } $targetId = (int)$this->input('targetId', 0); if ($targetId <= 0) { $this->fail('举报对象不存在'); } if ($targetType === 'post') { $target = Db::name('fc_post')->where('id', $targetId)->whereNull('deletetime')->find(); if (!$target) { $this->fail('信息不存在', 404); } } else { $target = Db::name('fc_comment')->where('id', $targetId)->find(); if (!$target) { $this->fail('评价不存在', 404); } } // 防重复举报:同一用户对同一条内容若已有待核实的举报,不允许重复提交 $existsPending = Db::name('fc_report') ->where('reporter_id', (int)$user['id']) ->where('target_type', $targetType) ->where('target_id', $targetId) ->where('status', 'pending') ->find(); if ($existsPending) { $this->fail('您已提交过该举报,平台正在核实中,请勿重复提交'); } // 频控保护:单日单个用户举报上限 15 次 $todayStart = strtotime('today'); $todayCount = Db::name('fc_report') ->where('reporter_id', (int)$user['id']) ->where('createtime', '>=', $todayStart) ->count(); if ($todayCount >= 15) { $this->fail('今日举报次数已达上限,感谢您的监督'); } $detail = trim((string)$this->input('detail', '')); $rawImages = $this->input('images', ''); if (is_array($rawImages)) { $images = implode(',', array_filter(array_map('trim', $rawImages))); } else { $images = trim((string)$rawImages); } if (mb_strlen($images) > 1000) { $images = mb_substr($images, 0, 1000); } $now = time(); $reportId = Db::name('fc_report')->insertGetId([ 'target_type' => $targetType, 'target_id' => $targetId, 'reason' => $reason, 'images' => $images, 'reporter_id' => (int)$user['id'], 'status' => 'pending', 'remark' => mb_substr($detail, 0, 200), 'handle_remark' => '', 'createtime' => $now, 'updatetime' => $now, ]); $this->ok(['reportId' => $reportId]); } /** * 获取当前用户的举报历史记录 * GET /api/v1/report/my * 入参:{page, size} * 出参:分页对象 {list:[...], page, size, total, hasMore} * * @return void */ public function my() { $user = $this->auth(); list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($user) { $q = Db::name('fc_report')->where('reporter_id', (int)$user['id']); if ($forCount) { return $q; } return $q->order('id desc'); }); $list = []; foreach ($rows as $row) { $targetTitle = ''; if ($row['target_type'] === 'post') { $postTitle = Db::name('fc_post')->where('id', $row['target_id'])->value('title'); $targetTitle = $postTitle ? (string)$postTitle : '信息已下架或删除'; } elseif ($row['target_type'] === 'comment') { $commentContent = Db::name('fc_comment')->where('id', $row['target_id'])->value('content'); $targetTitle = $commentContent ? (mb_substr($commentContent, 0, 30) . (mb_strlen($commentContent) > 30 ? '...' : '')) : '评价已删除'; } $rawImages = !empty($row['images']) ? explode(',', (string)$row['images']) : []; $imgUrls = []; foreach ($rawImages as $img) { $img = trim((string)$img); if ($img !== '') { $imgUrls[] = cdnurl($img, true); } } $list[] = [ 'id' => (int)$row['id'], 'targetType' => (string)$row['target_type'], 'targetId' => (int)$row['target_id'], 'targetTitle' => $targetTitle, 'reason' => (string)$row['reason'], 'images' => $imgUrls, 'detail' => (string)$row['remark'], 'status' => (string)$row['status'], 'statusText' => $row['status'] === 'handled' ? '已处理' : '核实中', 'handleRemark' => (string)($row['handle_remark'] ?? ''), 'createdAt' => $this->iso($row['createtime']), 'updatedAt' => $this->iso($row['updatetime']), ]; } $this->ok($this->paginate($list, $total, $page, $size)); } }