文件
fenleixinxi/application/api/controller/AdminComment.php
linda 11174f8693 fix(api): 修复 TP5.0 查询构造器复用与数组入参两处系统性缺陷
1) count() 后复用同一 Query 会清空 options,导致 alias/join/where 丢失
   (SQL 报错 Unknown column/table,或筛选静默失效)
   → 基类新增 pageQuery(callable) 助手,8 个分页接口全部改用闭包重建查询
2) TP5.0 Request::input() 对不含 / 的参数名做字符串强转,值若为数组会抛
   variable type error:array(影响 media[]/benefits[]/permissions[]/icons)
   → 基类 input()/inputs() 改用 post(false)/get(false) 取原始数组
3) 种子数据:佣金调整为 settled 336.4 / 未结算 950,使可提现余额 = 186.4(与前端 mock 对齐)
4) 测试脚本:P1 必填校验断言按 分类→标题→内容 顺序修正
2026-10-15 14:56:06 +08:00

110 行
3.6 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\api\controller;
use think\Db;
/**
* 后台管理端 · 评价管理(Module-ADM-COMMENT)
* 路由:
* GET /api/v1/admin/comments AC1 评价列表
* POST /api/v1/admin/comment/:id/reply AC2 评价回复
* GET /api/v1/admin/reports AC3 举报列表
*/
class AdminComment extends V1
{
/**
* AC1 评价列表
* GET /api/v1/admin/comments 入参 {page,size}
* → 分页 {list:[{id,postId,postTitle,user,score,content,images,reply,top,createdAt}],page,size,total,hasMore}
*/
public function index()
{
// 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象
list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) {
$q = Db::name('fc_comment')->alias('co')
->join('fc_post p', 'p.id = co.post_id', 'LEFT')
->join('user u', 'u.id = co.user_id', 'LEFT');
if ($forCount) {
return $q;
}
return $q->field('co.*, p.title AS post_title, u.nickname AS nickname')
->order('co.id desc');
});
$list = [];
foreach ($rows as $row) {
$list[] = [
'id' => (int)$row['id'],
'postId' => (int)$row['post_id'],
'postTitle' => (string)$row['post_title'],
'user' => (string)$row['nickname'],
'score' => (int)$row['score'],
'content' => (string)$row['content'],
'images' => $this->jsonArr($row['images']),
'reply' => (string)$row['reply'],
'top' => (bool)$row['top'],
'createdAt' => $this->iso($row['createtime']),
];
}
$this->ok($this->paginate($list, $total, $page, $size));
}
/**
* AC2 评价回复
* POST /api/v1/admin/comment/:id/reply 入参 {id?,reply} → {id,reply}
*/
public function reply()
{
$id = (int)$this->request->param('id', 0);
if (!$id) {
$id = (int)$this->input('id', 0);
}
$reply = trim((string)$this->input('reply', ''));
if ($reply === '') {
$this->fail('回复内容不能为空');
}
$comment = Db::name('fc_comment')->where('id', $id)->find();
if (!$comment) {
$this->fail('评价不存在', 404);
}
Db::name('fc_comment')->where('id', $id)->update([
'reply' => $reply,
'replytime' => time(),
]);
$this->notify($comment['user_id'], 'interact', '评价已回复', '您的评价收到商家回复:' . $reply);
$this->adminLog('回复评价 #' . $id);
$this->ok(['id' => (int)$id, 'reply' => $reply]);
}
/**
* AC3 举报列表
* GET /api/v1/admin/reports → 数组 [{id,targetType,targetId,reason,reporter,status,createdAt}]
*/
public function reports()
{
$rows = Db::name('fc_report')->order('createtime desc')->select();
$list = [];
foreach ($rows as $row) {
$list[] = [
'id' => (int)$row['id'],
'targetType' => (string)$row['target_type'],
'targetId' => (int)$row['target_id'],
'reason' => (string)$row['reason'],
'reporter' => (string)$row['reporter_id'],
'status' => (string)$row['status'],
'createdAt' => $this->iso($row['createtime']),
];
}
$this->ok($list);
}
}