diff --git a/application/admin/command/FcInstall.php b/application/admin/command/FcInstall.php index ae4fd86..52f3612 100644 --- a/application/admin/command/FcInstall.php +++ b/application/admin/command/FcInstall.php @@ -650,17 +650,34 @@ class FcInstall extends Command ]); } - /* ---------- 佣金(1001 的收益明细) ---------- */ - Db::name('fc_commission')->insert([ - 'user_id' => 1001, 'from_user_id' => 1002, 'order_id' => 3002, 'order_no' => 'MO20240102002', - 'order_type' => 'member', 'amount' => 33.6, 'rate' => 20, 'level' => 1, 'status' => 0, - 'createtime' => $t(2), - ]); - Db::name('fc_commission')->insert([ - 'user_id' => 1001, 'from_user_id' => 2003, 'order_id' => 3001, 'order_no' => 'PO20240101001', - 'order_type' => 'post', 'amount' => 1.0, 'rate' => 20, 'level' => 1, 'status' => 1, - 'settletime' => $t(1), 'createtime' => $t(3), - ]); + /* ---------- 佣金(1001 的收益明细) ---------- + * 数值与小程序 mock 的 distOverview 对齐: + * totalCommission 1286.4 = settled 336.4 + unsettled 950 + * 已申请提现 150(auditing 50 + paid 100) + * → withdrawable = 336.4 - 150 = 186.4 + */ + $commissions = [ + [1002, 3002, 'MO20240102002', 'member', 900.0, 20, 1, 0, 2], + [2003, 3001, 'PO20240101001', 'post', 50.0, 20, 1, 0, 0], + [2004, 3003, 'RO20240103003', 'recharge', 1.0, 20, 1, 1, 3], + [1003, 3002, 'MO20240102002', 'member', 300.0, 10, 2, 1, 5], + [2005, 3003, 'RO20240103003', 'recharge', 35.4, 10, 2, 1, 6], + ]; + foreach ($commissions as $c) { + Db::name('fc_commission')->insert([ + 'user_id' => 1001, + 'from_user_id' => $c[0], + 'order_id' => $c[1], + 'order_no' => $c[2], + 'order_type' => $c[3], + 'amount' => $c[4], + 'rate' => $c[5], + 'level' => $c[6], + 'status' => $c[7], + 'settletime' => $c[7] ? $t($c[8]) : null, + 'createtime' => $t($c[8]), + ]); + } /* ---------- 提现(含待审/已打款) ---------- */ $withdraws = [ diff --git a/application/api/controller/AdminComment.php b/application/api/controller/AdminComment.php index 8fa8bc7..a0907eb 100644 --- a/application/api/controller/AdminComment.php +++ b/application/api/controller/AdminComment.php @@ -20,17 +20,17 @@ class AdminComment extends V1 */ public function index() { - list($page, $size) = $this->pager(); - - $query = 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'); - - $total = $query->count(); - $rows = $query->field('co.*, p.title AS post_title, u.nickname AS nickname') - ->order('co.id desc') - ->page($page, $size) - ->select(); + // 闭包重建查询: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) { diff --git a/application/api/controller/AdminFinance.php b/application/api/controller/AdminFinance.php index 9b71d41..3c39dec 100644 --- a/application/api/controller/AdminFinance.php +++ b/application/api/controller/AdminFinance.php @@ -19,16 +19,19 @@ class AdminFinance extends V1 */ public function flows() { - list($page, $size) = $this->pager(); $type = trim((string)$this->input('type', '')); - $query = Db::name('fc_finance_flow'); - if ($type !== '') { - $query->where('type', $type); - } - - $total = $query->count(); - $rows = $query->order('createtime desc')->page($page, $size)->select(); + // 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象 + list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($type) { + $q = Db::name('fc_finance_flow'); + if ($type !== '') { + $q->where('type', $type); + } + if ($forCount) { + return $q; + } + return $q->order('createtime desc'); + }); $list = []; foreach ($rows as $row) { diff --git a/application/api/controller/AdminMember.php b/application/api/controller/AdminMember.php index 1d7e59d..4116f67 100644 --- a/application/api/controller/AdminMember.php +++ b/application/api/controller/AdminMember.php @@ -19,15 +19,16 @@ class AdminMember extends V1 */ public function users() { - list($page, $size) = $this->pager(); - - $query = Db::name('user')->alias('u') - ->join('fc_user_ext e', 'e.user_id = u.id', 'LEFT'); - $total = (int)$query->count(); - $rows = $query->field('u.id,u.nickname,u.mobile,u.status,u.jointime,e.user_type,e.is_member,e.member_expiretime') - ->order('u.id desc') - ->page($page, $size) - ->select(); + // 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象 + list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) { + $q = Db::name('user')->alias('u') + ->join('fc_user_ext e', 'e.user_id = u.id', 'LEFT'); + if ($forCount) { + return $q; + } + return $q->field('u.id,u.nickname,u.mobile,u.status,u.jointime,e.user_type,e.is_member,e.member_expiretime') + ->order('u.id desc'); + }); $now = time(); $list = []; @@ -79,7 +80,10 @@ class AdminMember extends V1 $current['distRate'] = (string)$distRate; } - $benefits = $this->input('benefits', null); + // 注意:benefits 可能是数组,不能走 $this->input()(TP5.0 Request::input 默认按字符串强转, + // 传入数组会抛 "variable type error:array");统一从 inputs() 取原始值。 + $allInputs = $this->inputs(); + $benefits = array_key_exists('benefits', $allInputs) ? $allInputs['benefits'] : null; if ($benefits !== null && $benefits !== '') { if (is_array($benefits)) { $current['benefits'] = array_values($benefits); diff --git a/application/api/controller/AdminMerchant.php b/application/api/controller/AdminMerchant.php index caf9a62..3bceb71 100644 --- a/application/api/controller/AdminMerchant.php +++ b/application/api/controller/AdminMerchant.php @@ -20,19 +20,23 @@ class AdminMerchant extends V1 */ public function index() { - list($page, $size) = $this->pager(); $status = $this->input('status', ''); $keyword = trim((string)$this->input('keyword', '')); - $query = Db::name('fc_merchant'); - if ($status !== '' && $status !== null) { - $query->where('audit_status', (int)$status); - } - if ($keyword !== '') { - $query->where('name', 'like', '%' . $keyword . '%'); - } - $total = $query->count(); - $rows = $query->order('id desc')->page($page, $size)->select(); + // 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象 + list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($status, $keyword) { + $q = Db::name('fc_merchant'); + if ($status !== '' && $status !== null) { + $q->where('audit_status', (int)$status); + } + if ($keyword !== '') { + $q->where('name', 'like', '%' . $keyword . '%'); + } + if ($forCount) { + return $q; + } + return $q->order('id desc'); + }); $list = []; foreach ($rows as $row) { diff --git a/application/api/controller/AdminOrder.php b/application/api/controller/AdminOrder.php index 8057f52..5dfb505 100644 --- a/application/api/controller/AdminOrder.php +++ b/application/api/controller/AdminOrder.php @@ -20,21 +20,21 @@ class AdminOrder extends V1 */ public function index() { - list($page, $size) = $this->pager(); $type = trim((string)$this->input('type', '')); - $query = Db::name('fc_order')->alias('o') - ->join('user u', 'u.id = o.user_id', 'LEFT'); - - if ($type !== '') { - $query->where('o.type', $type); - } - - $total = $query->count(); - $rows = $query->field('o.*, u.nickname AS nickname') - ->order('o.createtime desc') - ->page($page, $size) - ->select(); + // 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象 + list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($type) { + $q = Db::name('fc_order')->alias('o') + ->join('user u', 'u.id = o.user_id', 'LEFT'); + if ($type !== '') { + $q->where('o.type', $type); + } + if ($forCount) { + return $q; + } + return $q->field('o.*, u.nickname AS nickname') + ->order('o.createtime desc'); + }); $list = []; foreach ($rows as $row) { diff --git a/application/api/controller/AdminPost.php b/application/api/controller/AdminPost.php index ff53900..32d6b1d 100644 --- a/application/api/controller/AdminPost.php +++ b/application/api/controller/AdminPost.php @@ -21,19 +21,19 @@ class AdminPost extends V1 */ public function pending() { - list($page, $size) = $this->pager(); - - $query = Db::name('fc_post')->alias('p') - ->join('fc_category c', 'c.id = p.category_id', 'LEFT') - ->join('user u', 'u.id = p.user_id', 'LEFT') - ->where('p.audit_status', 0) - ->whereNull('p.deletetime'); - - $total = $query->count(); - $rows = $query->field('p.*, c.name AS category_name, u.nickname AS user_name') - ->order('p.createtime asc') - ->page($page, $size) - ->select(); + // 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象 + list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) { + $q = Db::name('fc_post')->alias('p') + ->join('fc_category c', 'c.id = p.category_id', 'LEFT') + ->join('user u', 'u.id = p.user_id', 'LEFT') + ->where('p.audit_status', 0) + ->whereNull('p.deletetime'); + if ($forCount) { + return $q; + } + return $q->field('p.*, c.name AS category_name, u.nickname AS user_name') + ->order('p.createtime asc'); + }); $list = []; foreach ($rows as $row) { diff --git a/application/api/controller/AdminRbac.php b/application/api/controller/AdminRbac.php index 337a3cc..c7070dd 100644 --- a/application/api/controller/AdminRbac.php +++ b/application/api/controller/AdminRbac.php @@ -57,7 +57,10 @@ class AdminRbac extends V1 $name = trim((string)$this->input('name', '')); $remark = trim((string)$this->input('remark', '')); - $permissions = (array)$this->input('permissions', []); + // 注意:permissions 是数组,不能走 $this->input()(TP5.0 Request::input 默认按字符串强转, + // 传入数组会抛 "variable type error:array");统一从 inputs() 取原始值。 + $allInputs = $this->inputs(); + $permissions = (array)(array_key_exists('permissions', $allInputs) ? $allInputs['permissions'] : []); $permissions = array_values(array_filter(array_map(function ($v) { return (string)$v; }, $permissions), function ($v) { @@ -104,11 +107,17 @@ class AdminRbac extends V1 public function accounts() { $groups = Db::name('auth_group')->column('name', 'id'); - $rows = Db::name('admin')->order('id asc')->select(); + // fa_admin 表本身没有 group_id:角色关系存放于 fa_auth_group_access(uid,group_id), + // 必须显式 join 取出 group_id,否则 $row['group_id'] 未定义。 + $rows = Db::name('admin')->alias('a') + ->join('auth_group_access ga', 'ga.uid = a.id', 'LEFT') + ->field('a.id,a.username,a.logintime,ga.group_id') + ->order('a.id asc') + ->select(); $list = []; foreach ($rows as $row) { - $gid = (int)$row['group_id']; + $gid = isset($row['group_id']) ? (int)$row['group_id'] : 0; $roleName = ($gid && isset($groups[$gid])) ? (string)$groups[$gid] : ''; $list[] = [ 'id' => (int)$row['id'], diff --git a/application/api/controller/AdminSetting.php b/application/api/controller/AdminSetting.php index e970359..9ae5cf8 100644 --- a/application/api/controller/AdminSetting.php +++ b/application/api/controller/AdminSetting.php @@ -168,7 +168,10 @@ class AdminSetting extends V1 $this->setCfg('ui.logo', (string)$logo); } - $icons = $this->input('icons', null); + // 注意:icons 可能是数组,不能走 $this->input()(TP5.0 Request::input 默认按字符串强转, + // 传入数组会抛 "variable type error:array" 导致 HTTP 500);统一从 inputs() 取原始值。 + $allInputs = $this->inputs(); + $icons = array_key_exists('icons', $allInputs) ? $allInputs['icons'] : null; if ($icons !== null) { if (is_string($icons)) { $icons = json_decode($icons, true); diff --git a/application/api/controller/Distribution.php b/application/api/controller/Distribution.php index 4a7581f..286131e 100644 --- a/application/api/controller/Distribution.php +++ b/application/api/controller/Distribution.php @@ -60,20 +60,22 @@ class Distribution extends V1 { $user = $this->auth(); $uid = (int)$user['id']; - list($page, $size) = $this->pager(); $level = $this->input('level', ''); - $query = Db::name('fc_dist_relation')->alias('r') - ->join('user u', 'u.id = r.user_id') - ->where('r.parent_id', $uid); - if ($level !== '' && $level !== null) { - $query->where('r.level', (int)$level); - } - $total = $query->count(); - $rows = $query->field('r.id,r.user_id,r.level,r.createtime,u.nickname') - ->order('r.createtime desc') - ->page($page, $size) - ->select(); + // 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象 + list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($uid, $level) { + $q = Db::name('fc_dist_relation')->alias('r') + ->join('user u', 'u.id = r.user_id') + ->where('r.parent_id', $uid); + if ($level !== '' && $level !== null) { + $q->where('r.level', (int)$level); + } + if ($forCount) { + return $q; + } + return $q->field('r.id,r.user_id,r.level,r.createtime,u.nickname') + ->order('r.createtime desc'); + }); $list = []; foreach ($rows as $row) { diff --git a/application/api/controller/Message.php b/application/api/controller/Message.php index 671c16c..8ed13c5 100644 --- a/application/api/controller/Message.php +++ b/application/api/controller/Message.php @@ -23,15 +23,19 @@ class Message extends V1 { $user = $this->auth(); $uid = (int)$user['id']; - list($page, $size) = $this->pager(); $type = $this->input('type', ''); - $query = Db::name('fc_message')->where('user_id', 'in', [$uid, 0]); - if ($type !== '' && $type !== null) { - $query->where('type', $type); - } - $total = $query->count(); - $rows = $query->order('createtime desc')->page($page, $size)->select(); + // 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象 + list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($uid, $type) { + $q = Db::name('fc_message')->where('user_id', 'in', [$uid, 0]); + if ($type !== '' && $type !== null) { + $q->where('type', $type); + } + if ($forCount) { + return $q; + } + return $q->order('createtime desc'); + }); $list = []; foreach ($rows as $row) { diff --git a/application/api/controller/V1.php b/application/api/controller/V1.php index 60b5274..56e0937 100644 --- a/application/api/controller/V1.php +++ b/application/api/controller/V1.php @@ -133,20 +133,31 @@ class V1 extends Api /* ==================== 入参 ==================== */ - /** 读取单个入参(POST 优先,其次 GET) */ + /** + * 读取单个入参(POST 优先,其次 GET) + * + * 注意:不能使用 $this->request->post($key) —— TP5.0 的 Request::input() 在参数名 + * 不含 '/' 时会按字符串强转,若值本身是数组(如 media[]/benefits[]/icons) + * 会抛 InvalidArgumentException('variable type error:array')。 + * 这里改用 post(false)/get(false) 取原始数组,天然支持数组与 JSON 请求体。 + */ protected function input($key, $default = null) { - $val = $this->request->post($key, null); - if ($val === null || $val === '') { - $val = $this->request->get($key, $default); + $raw = array_merge((array)$this->request->get(false), (array)$this->request->post(false)); + if (!array_key_exists($key, $raw)) { + return $default; } - return $val === null ? $default : $val; + $val = $raw[$key]; + if ($val === '' || $val === null) { + return $default; + } + return $val; } - /** 全部入参 */ + /** 全部入参(数组安全) */ protected function inputs() { - return array_merge((array)$this->request->get(), (array)$this->request->post()); + return array_merge((array)$this->request->get(false), (array)$this->request->post(false)); } /** 必填校验,失败直接返回业务错误 */ @@ -204,6 +215,31 @@ class V1 extends Api return $this->paginate(array_slice($items, ($page - 1) * $size, $size), $total, $page, $size); } + /** + * 通用「数据库分页查询」(正确处理 TP5.0 的查询构造器复用陷阱) + * + * 注意:TP5.0 的 count() 内部会执行 $this->options = [], + * 因此**绝不能**复用同一个 Query 对象先 count() 再 select(), + * 否则 alias / join / where 全部丢失(表现为 SQL 报错或筛选失效)。 + * + * 用法: + * list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($x) { + * $q = Db::name('fc_xxx')->alias('t')->join('...')->where(...); + * return $forCount ? $q : $q->field('...')->order('...'); + * }); + * + * @param callable $builder function(bool $forCount): \think\db\Query + * @param int $defaultSize + * @return array [rows, total, page, size] + */ + protected function pageQuery(callable $builder, $defaultSize = 10) + { + list($page, $size) = $this->pager($defaultSize); + $total = (int)$builder(true)->count(); + $rows = $builder(false)->page($page, $size)->select(); + return [$rows, $total, $page, $size]; + } + /* ==================== 业务配置(fa_fc_config) ==================== */ /** diff --git a/tools/verify-api.mjs b/tools/verify-api.mjs index 7409250..1410d53 100644 --- a/tools/verify-api.mjs +++ b/tools/verify-api.mjs @@ -150,8 +150,9 @@ async function userPost() { true, (d) => (d && d.postId && Number(d.auditStatus) === 0 ? '' : 'postId/auditStatus 不符(BR-04 应为 0)')) newPostId = created && created.postId ? Number(created.postId) : 0 - await expectFail('P1 · 标题必填 (BR-03)', 'POST', '/post/create', { content: 'x' }, true, '标题不能为空') - await expectFail('P1 · 内容必填 (BR-03)', 'POST', '/post/create', { title: 'x' }, true, '内容不能为空') + await expectFail('P1 · 分类必填 (BR-03)', 'POST', '/post/create', { title: 'x', content: 'x' }, true, '分类不能为空') + await expectFail('P1 · 标题必填 (BR-03)', 'POST', '/post/create', { categoryId: 3, content: 'x' }, true, '标题不能为空') + await expectFail('P1 · 内容必填 (BR-03)', 'POST', '/post/create', { categoryId: 3, title: 'x' }, true, '内容不能为空') await ok200('P4 POST /post/draft', 'POST', '/post/draft', { title: '草稿·验证', content: '草稿内容', categoryId: 2 }, true,