Files
lgp-admin-plus-api/app/Service/wx/WxAfterSaleService.php
2026-08-20 19:16:49 +08:00

83 lines
3.0 KiB
PHP

<?php
namespace App\Service\wx;
use App\BaseApp\BaseWxService;
use App\Models\business\AfterSaleModel;
use App\Models\business\OrderModel;
use App\Service\business\PriceService;
/**
* 小程序售后:已付款/已发货/已完成可申请,后台审核,不自动退微信款。
*/
class WxAfterSaleService extends BaseWxService
{
public function list(): array
{
$price = PriceService::getInstance();
$rows = AfterSaleModel::with('order')
->where('user_id', $this->userId)
->where('deleted_at', 0)
->orderByDesc('id')
->get()
->toArray();
foreach ($rows as &$row) {
$row['amount_text'] = $price->centsToYuan((int) ($row['amount'] ?? 0));
$row['order_no'] = $row['order']['order_no'] ?? '';
$row['status_text'] = match ((int) ($row['status'] ?? 0)) {
AfterSaleModel::STATUS_APPROVED => '已通过',
AfterSaleModel::STATUS_REJECTED => '已拒绝',
default => '待审核',
};
unset($row['order']);
}
unset($row);
return $rows;
}
public function create(array $params): array
{
$orderId = (int) ($params['order_id'] ?? 0);
$reason = trim((string) ($params['reason'] ?? ''));
if ($orderId <= 0 || $reason === '') {
$this->utils->errorThrow('请填写订单和原因');
}
$order = OrderModel::where('id', $orderId)->where('user_id', $this->userId)->where('deleted_at', 0)->first();
if (empty($order)) {
$this->utils->errorThrow('订单不存在');
}
$status = (int) $order['status'];
if (!in_array($status, [OrderModel::STATUS_PAID, OrderModel::STATUS_SHIPPED, OrderModel::STATUS_DONE], true)) {
$this->utils->errorThrow('当前订单状态不能申请售后');
}
$pending = AfterSaleModel::where('order_id', $orderId)
->where('deleted_at', 0)
->where('status', AfterSaleModel::STATUS_PENDING)
->exists();
if ($pending) {
$this->utils->errorThrow('已有待审核的售后申请');
}
$amount = (int) ($params['amount'] ?? 0);
if ($amount <= 0) {
$amount = (int) $order['paid_amount'];
}
if ($amount > (int) $order['paid_amount']) {
$this->utils->errorThrow('退款金额不能超过已付金额');
}
$now = time();
$id = (int) AfterSaleModel::insertGetId([
'order_id' => $orderId,
'user_id' => $this->userId,
'reason' => mb_substr($reason, 0, 255),
'amount' => $amount,
'images' => is_array($params['images'] ?? null)
? implode(',', $params['images'])
: (string) ($params['images'] ?? ''),
'status' => AfterSaleModel::STATUS_PENDING,
'created_at' => $now,
'updated_at' => $now,
]);
return AfterSaleModel::where('id', $id)->first()->toArray();
}
}