113 lines
3.7 KiB
PHP
113 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace App\Service\wx;
|
||
|
||
use App\BaseApp\BaseWxService;
|
||
use App\Models\business\OrderModel;
|
||
use App\Models\business\WxUserModel;
|
||
use App\Service\business\OrderCoreService;
|
||
use App\Service\business\PriceService;
|
||
|
||
/**
|
||
* 小程序订单:用户自己从清单下单、看订单、传凭证或调起微信支付
|
||
*/
|
||
class WxOrderService extends BaseWxService
|
||
{
|
||
/**
|
||
* 我的订单,可按状态筛
|
||
*
|
||
* 金额 text 必须在这里补:OrderCoreService::detail 才格式化金额,list 只走 paginate,
|
||
* 而小程序订单列表卡片直接渲染 total_amount_text,不补就会出现 ¥0。
|
||
*/
|
||
public function list(): array
|
||
{
|
||
$status = request()->get('status');
|
||
$result = OrderModel::with([
|
||
'items' => fn ($query) => $query->where('deleted_at', 0),
|
||
])->where('user_id', $this->userId)
|
||
->where('deleted_at', 0)
|
||
->when($status !== null && $status !== '', fn ($query) => $query->where('status', (int) $status))
|
||
->orderBy('id', 'desc')
|
||
->paginate((int) request()->get('pageSize', 10))
|
||
->toArray();
|
||
// 复用后台同款格式化,保证两端金额展示一致
|
||
$price = PriceService::getInstance();
|
||
foreach ($result['data'] as &$item) {
|
||
$item['total_amount_text'] = $price->centsToYuan((int) ($item['total_amount'] ?? 0));
|
||
$item['paid_amount_text'] = $price->centsToYuan((int) ($item['paid_amount'] ?? 0));
|
||
}
|
||
unset($item);
|
||
return [
|
||
'page' => $result['current_page'],
|
||
'size' => $result['per_page'],
|
||
'page_count' => $result['last_page'],
|
||
'total' => $result['total'],
|
||
'items' => $result['data'],
|
||
];
|
||
}
|
||
|
||
public function detail(int $id): array
|
||
{
|
||
$order = OrderCoreService::getInstance()->detail($id);
|
||
if ((int) $order['user_id'] !== $this->userId) {
|
||
$this->utils->errorThrow('无权查看该订单');
|
||
}
|
||
return $order;
|
||
}
|
||
|
||
/**
|
||
* 清单转订单
|
||
*/
|
||
public function createFromList(array $params): array
|
||
{
|
||
$listId = (int) ($params['list_id'] ?? 0);
|
||
if ($listId <= 0) {
|
||
$this->utils->errorThrow('请选择清单');
|
||
}
|
||
return OrderCoreService::getInstance()->createFromList($listId, $this->userId, $params);
|
||
}
|
||
|
||
/**
|
||
* 上传转账凭证
|
||
*/
|
||
public function submitVoucher(array $params): array
|
||
{
|
||
$orderId = (int) ($params['order_id'] ?? 0);
|
||
return OrderCoreService::getInstance()->submitVoucher($orderId, $params, $this->userId);
|
||
}
|
||
|
||
/**
|
||
* 调起微信支付
|
||
*/
|
||
public function wechatPay(array $params): array
|
||
{
|
||
$orderId = (int) ($params['order_id'] ?? 0);
|
||
$order = $this->detail($orderId);
|
||
$user = WxUserModel::where('id', $this->userId)->first(['open_id']);
|
||
if (empty($user['open_id'])) {
|
||
$this->utils->errorThrow('缺少 openid,请重新登录');
|
||
}
|
||
OrderModel::where('id', $orderId)->update([
|
||
'pay_type' => OrderModel::PAY_TYPE_WECHAT,
|
||
'updated_at' => time(),
|
||
]);
|
||
return WxPayService::getInstance()->jsapiOrder($order, (string) $user['open_id']);
|
||
}
|
||
|
||
/**
|
||
* 取消订单(仅未付款可取消)
|
||
*/
|
||
public function cancel(int $id): array
|
||
{
|
||
return OrderCoreService::getInstance()->transition($id, OrderModel::STATUS_CANCELLED, $this->userId);
|
||
}
|
||
|
||
/**
|
||
* 确认收货
|
||
*/
|
||
public function complete(int $id): array
|
||
{
|
||
return OrderCoreService::getInstance()->transition($id, OrderModel::STATUS_DONE, $this->userId);
|
||
}
|
||
}
|