248 行
8.5 KiB
PHP
248 行
8.5 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 用户端 · 举报(Module-REPORT)
|
||
* 路由:
|
||
* POST /api/v1/report 举报信息/评价
|
||
* GET /api/v1/report/my 我的举报记录
|
||
* GET /api/v1/report/:id 我的举报记录详情
|
||
*
|
||
* 说明:举报原因字典由后台配置 fc_config 的 report.reasons(JSON 数组)维护,
|
||
* 小程序端从站点配置读取;本接口做「必填 + 类型合法 + 对象存在 + 防重复提交」校验,
|
||
* 允许举报原因使用字典外的自定义文案(前端传字典项,`detail` 传补充说明,`images` 传截图凭证)。
|
||
*/
|
||
class Report extends V1
|
||
{
|
||
/**
|
||
* 提交举报
|
||
* POST /api/v1/report
|
||
* 入参:{targetType:'post'|'comment', targetId, reason, detail?, images?}
|
||
* 出参:{reportId}
|
||
*
|
||
* 业务规则:
|
||
* 1. 必须登录;
|
||
* 2. targetType 仅支持 'post' 或 'comment';
|
||
* 3. reason 必填;
|
||
* 4. 被举报对象(信息/评价)必须真实存在;
|
||
* 5. 防重复提交:同用户对同一目标存在 pending 状态举报时拦截;
|
||
* 6. 频控保护:单日举报上限 15 次,防止恶意刷库。
|
||
*
|
||
* @return void
|
||
*/
|
||
public function create()
|
||
{
|
||
$user = $this->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();
|
||
$status = trim((string)$this->input('status', ''));
|
||
|
||
list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($user, $status) {
|
||
$q = Db::name('fc_report')->where('reporter_id', (int)$user['id']);
|
||
if (in_array($status, ['pending', 'handled'], true)) {
|
||
$q->where('status', $status);
|
||
}
|
||
if ($forCount) {
|
||
return $q;
|
||
}
|
||
return $q->order('id desc');
|
||
});
|
||
|
||
$list = [];
|
||
foreach ($rows as $row) {
|
||
$list[] = $this->formatReportRow($row, false);
|
||
}
|
||
|
||
$this->ok($this->paginate($list, $total, $page, $size));
|
||
}
|
||
|
||
/**
|
||
* 获取当前用户的单条举报记录详情
|
||
* GET /api/v1/report/:id
|
||
*
|
||
* @param int $id 举报记录 ID
|
||
* @return void
|
||
*/
|
||
public function detail($id = 0)
|
||
{
|
||
$user = $this->auth();
|
||
$reportId = (int)$id ?: (int)$this->input('id', 0);
|
||
if ($reportId <= 0) {
|
||
$this->fail('举报记录不存在', 404);
|
||
}
|
||
|
||
$row = Db::name('fc_report')
|
||
->where('id', $reportId)
|
||
->where('reporter_id', (int)$user['id'])
|
||
->find();
|
||
if (!$row) {
|
||
$this->fail('举报记录不存在', 404);
|
||
}
|
||
|
||
$this->ok($this->formatReportRow($row, true));
|
||
}
|
||
|
||
/**
|
||
* 格式化单条举报记录输出结构(列表与详情共用)
|
||
*
|
||
* @param array $row fc_report 数据行
|
||
* @param bool $includeFullTarget 是否附带完整被举报对象内容与存在状态
|
||
* @return array
|
||
*/
|
||
private function formatReportRow(array $row, $includeFullTarget = false)
|
||
{
|
||
$targetTitle = '';
|
||
$targetContent = '';
|
||
$targetExists = false;
|
||
|
||
if ($row['target_type'] === 'post') {
|
||
$post = Db::name('fc_post')
|
||
->where('id', $row['target_id'])
|
||
->field('id,title,content,deletetime,status')
|
||
->find();
|
||
$targetExists = !empty($post) && empty($post['deletetime']);
|
||
$targetTitle = !empty($post['title']) ? (string)$post['title'] : '信息已下架或删除';
|
||
if ($includeFullTarget && !empty($post['content'])) {
|
||
$targetContent = (string)$post['content'];
|
||
}
|
||
} elseif ($row['target_type'] === 'comment') {
|
||
$comment = Db::name('fc_comment')
|
||
->where('id', $row['target_id'])
|
||
->field('id,content')
|
||
->find();
|
||
$targetExists = !empty($comment);
|
||
$commentContent = !empty($comment['content']) ? (string)$comment['content'] : '';
|
||
$targetTitle = $commentContent !== ''
|
||
? (mb_substr($commentContent, 0, 30) . (mb_strlen($commentContent) > 30 ? '...' : ''))
|
||
: '评价已删除';
|
||
if ($includeFullTarget) {
|
||
$targetContent = $commentContent;
|
||
}
|
||
}
|
||
|
||
$rawImages = !empty($row['images']) ? explode(',', (string)$row['images']) : [];
|
||
$imgUrls = [];
|
||
foreach ($rawImages as $img) {
|
||
$img = trim((string)$img);
|
||
if ($img !== '') {
|
||
$imgUrls[] = cdnurl($img, true);
|
||
}
|
||
}
|
||
|
||
$item = [
|
||
'id' => (int)$row['id'],
|
||
'targetType' => (string)$row['target_type'],
|
||
'targetId' => (int)$row['target_id'],
|
||
'targetTitle' => $targetTitle,
|
||
'targetExists' => $targetExists,
|
||
'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']),
|
||
];
|
||
|
||
if ($includeFullTarget) {
|
||
$item['targetContent'] = $targetContent;
|
||
}
|
||
|
||
return $item;
|
||
}
|
||
}
|