input('type', '')); // 闭包重建查询: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) { $list[] = [ 'id' => (int)$row['id'], 'orderNo' => (string)$row['order_no'], 'type' => (string)$row['type'], 'title' => (string)$row['title'], 'amount' => (float)$row['amount'], 'payType' => (string)$row['pay_type'], 'status' => (string)$row['status'], 'userId' => (int)$row['user_id'], 'userName' => (string)$row['nickname'], 'createdAt' => $this->iso($row['createtime']), ]; } $this->ok($this->paginate($list, $total, $page, $size)); } /** * AO2 订单退款 * POST /api/v1/admin/order/:id/refund 入参 {amount,reason?} → {id,refunded} * 规则 BR-10:退款金额需大于 0 */ public function refund() { $id = (int)$this->request->param('id', 0); $amount = round((float)$this->input('amount', 0), 2); $reason = trim((string)$this->input('reason', '')); if ($amount <= 0) { $this->fail('退款金额需大于 0'); } $order = Db::name('fc_order')->where('id', $id)->find(); if (!$order) { $this->fail('订单不存在', 404); } Db::name('fc_order')->where('id', $id)->update([ 'status' => 'refunded', 'refund_amount' => $amount, 'refund_reason' => $reason, ]); $this->notify($order['user_id'], 'system', '订单已退款', '您的订单「' . $order['title'] . '」已退款 ' . $amount . ' 元。'); $this->adminLog('订单退款 #' . $id . ' 金额' . $amount); $this->ok(['id' => (int)$id, 'refunded' => true]); } /** * AO3 订单补单 * POST /api/v1/admin/order/supplement 入参 {orderNo} → {orderNo,supplemented} * 幂等:已支付直接返回 */ public function supplement() { $orderNo = trim((string)$this->input('orderNo', '')); if ($orderNo === '') { $this->fail('订单号不能为空'); } $order = Db::name('fc_order')->where('order_no', $orderNo)->find(); if (!$order) { $this->fail('订单不存在', 404); } if ($order['status'] !== 'paid') { Db::name('fc_order')->where('id', $order['id'])->update([ 'status' => 'paid', 'paytime' => time(), ]); $this->flow('income', '订单补单', $order['amount'], 0, $order['id']); $this->adminLog('订单补单 #' . $orderNo); } $this->ok(['orderNo' => (string)$orderNo, 'supplemented' => true]); } }