初始化项目

This commit is contained in:
2026-09-01 22:15:50 +08:00
commit b5a26a8848
11217 changed files with 1710987 additions and 0 deletions
+194
View File
@@ -0,0 +1,194 @@
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\console\service;
use app\common\enum\order\ReceiptStatus;
use app\common\model\UserOauth as UserOauthModel;
use app\console\model\Order as OrderModel;
use app\common\service\BaseService;
use app\common\service\Order as OrderService;
use app\common\service\order\Complete as OrderCompleteService;
use app\common\enum\order\OrderStatus as OrderStatusEnum;
use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
use app\common\library\helper;
use app\console\library\Tools;
use app\common\library\wechat\WxPay;
use app\common\model\wxapp\Setting as WxappSettingModel;
/**
* 服务类:订单模块
* Class Order
* @package app\console\service
*/
class Order extends BaseService
{
/**
* 未支付订单自动关闭
* @param int $storeId
* @param int $closeHours 自定关闭订单有效期 (小时)
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function closeEvent(int $storeId, int $closeHours)
{
// 截止时间
$deadlineTime = time() - ((int)$closeHours * 60 * 60);
// 查询截止时间未支付的订单
$model = new OrderModel;
$list = $model->getListByClose($storeId, $deadlineTime);
// 订单ID集
$orderIds = helper::getArrayColumn($list, 'order_id');
if (!empty($orderIds)) {
// 取消订单事件
foreach ($list as $order) {
OrderService::cancelEvent($order);
}
// 批量更新订单状态为已取消
$model->onBatchUpdate($orderIds, ['order_status' => OrderStatusEnum::CANCELLED]);
}
// 记录日志
Tools::taskLogs('Order', 'closeEvent', [
'storeId' => $storeId,
'closeHours' => $closeHours,
'deadlineTime' => $deadlineTime,
'orderIds' => helper::jsonEncode($orderIds)
]);
}
/**
* 已发货订单自动确认收货
* @param int $storeId
* @param int $receiveDays 自动收货天数
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function receiveEvent(int $storeId, int $receiveDays)
{
// 截止时间
$deadlineTime = time() - ((int)$receiveDays * 86400);
// 查询截止时间未确认收货的订单ID集
$model = new OrderModel;
$orderIds = $model->getOrderIdsByReceive($storeId, $deadlineTime);
// 更新订单收货状态
if (!empty($orderIds)) {
// 批量更新订单状态为已收货
$model->onUpdateReceived($orderIds);
// 批量处理已完成的订单
$this->onReceiveCompleted($storeId, $orderIds);
}
// 记录日志
Tools::taskLogs('Order', 'receiveEvent', [
'storeId' => $storeId,
'receiveDays' => $receiveDays,
'deadlineTime' => $deadlineTime,
'orderIds' => helper::jsonEncode($orderIds)
]);
}
/**
* 已完成订单自动结算
* @param int $storeId
* @param int $refundDays 售后期限天数
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function settledEvent(int $storeId, int $refundDays)
{
// 截止时间
$deadlineTime = time() - ((int)$refundDays * 86400);
// 查询截止时间确认收货的订单ID集
$model = new OrderModel;
$list = $model->getOrderListBySettled($storeId, $deadlineTime);
// 订单ID集
$orderIds = helper::getArrayColumn($list, 'order_id');
// 订单结算服务
if (!empty($orderIds)) {
$OrderCompleteService = new OrderCompleteService();
$OrderCompleteService->settled($list);
}
// 记录日志
Tools::taskLogs('Order', 'settledEvent', [
'storeId' => $storeId,
'refundDays' => $refundDays,
'deadlineTime' => $deadlineTime,
'orderIds' => helper::jsonEncode($orderIds)
]);
}
/**
* 批量处理已完成的订单
* @param int $storeId 商城ID
* @param array $orderIds 订单ID集
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function onReceiveCompleted(int $storeId, array $orderIds)
{
// 获取已完成的订单列表
$model = new OrderModel;
$list = $model->getListByOrderIds($storeId, $orderIds);
// 执行订单完成后的操作
if (!$list->isEmpty()) {
$OrderCompleteService = new OrderCompleteService();
$OrderCompleteService->complete($list, $storeId);
}
return true;
}
/**
* 自动微信支付发货
* @param int $storeId
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function deliveryEvent(int $storeId)
{
// 查询截止时间未支付的订单
$model = new OrderModel;
$list = $model->getListByDelivery($storeId);
// 订单ID集
$orderIds = helper::getArrayColumn($list, 'order_id');
if (!empty($orderIds)) {
// 小程序配置信息
$wxConfig = WxappSettingModel::getWxappConfig($storeId);
$wxPay = new WxPay(['app_id' => $wxConfig['app_id'], 'app_secret'=> $wxConfig['app_secret']]);
// 取消订单事件
foreach ($list as $order) {
// 请求API数据
$name = $order['goods'][0]['goods_name'] ?? '中石油油券';
// 获取第三方用户信息(微信)
$oauth = UserOauthModel::getOauth($order['user_id'], 'MP-WEIXIN');
$wxPay->uploadShipping($order['transaction_id'], $name, $oauth['oauth_id']);
}
// 批量更新订单状态为已取消
$model->onBatchUpdate($orderIds, [
'delivery_status' => DeliveryStatusEnum::DELIVERED,
'delivery_time' => time(),
'receipt_status' => ReceiptStatus::RECEIVED,
'receipt_time' => time()
]);
}
// 记录日志
Tools::taskLogs('Order', 'deliveryEvent', [
'storeId' => $storeId,
'orderIds' => helper::jsonEncode($orderIds)
]);
}
}