feat : ide config

This commit is contained in:
2025-03-17 17:29:51 +08:00
parent 4758aa598d
commit 9e22529161
14 changed files with 259 additions and 52 deletions

View File

@@ -9,6 +9,7 @@ use App\Constants\Common\OrderCode;
use App\Lib\Log;
use App\Model\Order;
use App\Model\UserCoupon;
use App\Service\ServiceTrait\Api\CouponTrait;
use App\Service\ServiceTrait\Api\OrderTrait;
use Exception;
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
@@ -28,6 +29,7 @@ class CancelOrderConsumer extends ConsumerMessage
{
use ProducerDelayedMessageTrait,ConsumerDelayedMessageTrait;
use OrderTrait;
use CouponTrait;
/**
* @var Type|string 消息类型
@@ -103,27 +105,4 @@ class CancelOrderConsumer extends ConsumerMessage
return Result::ACK;
}
/**
* @param $orderType
* @param $orderInfo
* @return void
* @throws Exception
*/
private function rollbackCoupon($orderType,$orderInfo): void
{
if ($orderType != OrderCode::ORDER_TYPE_GOOD) return;
if ($orderInfo->coupon_id <= 0) return;
$couponInfo = $this->userCouponModel->where('coupon_id', $orderInfo->coupon_id)->where('user_id',$orderInfo->user_id)->first();
if (empty($couponInfo)) return;
$couponInfo->status = CouponCode::COUPON_STATUS_UNUSED;
if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) $couponInfo->status = CouponCode::COUPON_STATUS_EXPIRE;
if (!$couponInfo->save()) throw new Exception('CancelOrderConsumer:error:couponStatusUpdateError:'.json_encode($orderInfo->toArray()));
}
}

View File

@@ -294,7 +294,7 @@ class GoodCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function delCache(int $kitchenId,int $cycleId)
public function delCache(int $kitchenId,int $cycleId): void
{
$keyArr = [
ApiRedisKey::mealGoodListKey($cycleId,$kitchenId),

View File

@@ -79,6 +79,8 @@ class PlaceOrderService extends BaseOrderService
$this->sendStockMq($this->orderId,OrderCode::WAIT_PAY);
$this->orderRes['order_id'] = $this->orderId;
return $this->return->success('success',$this->orderRes);
}
@@ -195,7 +197,7 @@ class PlaceOrderService extends BaseOrderService
$orderInsertModel->copies = $this->orderRes['copies'];
$orderInsertModel->type = $this->orderType;
$orderInsertModel->total_price = $this->orderRes['total_price'];
$orderInsertModel->actual_price = $this->orderRes['actual_price'];
$orderInsertModel->actual_price = max($this->orderRes['actual_price'], 0);
$orderInsertModel->discount_price = $this->orderRes['favorable_sundry_price'] + $this->orderRes['favorable_good_price'];
$orderInsertModel->status = OrderCode::WAIT_PAY;
$orderInsertModel->is_refund_all = OrderCode::REFUND_NULL;

View File

@@ -15,11 +15,18 @@ use App\Cache\Redis\RedisCache;
use App\Constants\Common\OrderCode;
use App\Constants\Common\PayCode;
use App\Exception\ErrException;
use App\Extend\SystemUtil;
use App\Model\Order;
use App\Model\PayOrder;
use App\Model\UserThird;
use App\Service\Api\BaseService;
use App\Service\Common\Pay\Wx\WxJsRechargeOrderService;
use App\Service\ServiceTrait\Api\CateringTrait;
use App\Service\ServiceTrait\Api\CouponTrait;
use App\Service\ServiceTrait\Api\GoodOrderTrait;
use App\Service\ServiceTrait\Api\RefundOrderTrait;
use App\Service\ServiceTrait\Common\OrderChangeStatusTrait;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -44,17 +51,18 @@ class PlacePayService extends BaseService
/**
* @var Order
*/
private Order $orderModel;
#[Inject]
protected Order $orderModel;
/**
* @var mixed
*/
private mixed $orderInfo;
private Order $orderInfo;
/**
* @var mixed
*/
private mixed $payInfo;
private PayOrder $payInfo;
/**
* @var string
@@ -91,10 +99,15 @@ class PlacePayService extends BaseService
$this->orderType = (int)$this->request->input('order_type');
$this->payType = (int)$this->request->input('pay_type');
$this->orderModel = (new OrderTypeFactory)->getPayOrderModel($this->payType);
$this->orderModel = (new OrderTypeFactory)->getPayOrderModel($this->orderType);
$this->checkOrder();
//小于等于 0 直接支付
if ($this->orderInfo->actual_price <= 0) return $this->directPayment();
$this->checkPayOrder();
$this->setPayInfo();
$rechargeService = match ($this->payType)
@@ -106,17 +119,22 @@ class PlacePayService extends BaseService
$rechargeService->setConfig();
$rechargeService->setNotify();
// 测试环境 0.01
if (!SystemUtil::checkProEnv() && $this->orderInfo->actual_price > 0) $this->orderInfo->actual_price = '0.01';
$payData = $rechargeService->pay(
(float)$this->orderInfo->actual_price,
$this->request->input('body','订单支付'),
$this->orderInfo->order_no,
// $this->request->input('body','订单支付'),
$this->orderInfo->id,
$this->orderInfo->order_sno,
$this->userId
);
$res = [
'orderId' => $this->orderId,
'wechat_pay' => [],
'alipay' => []
'alipay' => [],
'status' => $this->orderInfo->status
];
//返回支付数组
@@ -130,6 +148,62 @@ class PlacePayService extends BaseService
return $this->return->success('success', $res);
}
use GoodOrderTrait;
use CateringTrait;
use OrderChangeStatusTrait;
private bool $isCatering;
private function directPayment(): array
{
switch ($this->orderType) {
case OrderCode::ORDER_TYPE_GOOD:
Db::transaction(function (){
$this->manageOrder();
$this->isCatering = $this->manageAddCateringLog();
});
//已经截单 自动退款
if (!$this->isCatering) $this->directGoodRefund();
break;
case OrderCode::ORDER_TYPE_BALANCE:
echo 1;
break;
default:
throw new ErrException('订单类型错误');
}
$res = [
'orderId' => $this->orderId,
'wechat_pay' => [],
'alipay' => [],
'status' => $this->orderInfo->status
];
$this->redisCache->delLock($this->lockKey);
return $this->return->success('success', $res);
}
use RefundOrderTrait;
use CouponTrait;
/**
* @return void
*/
private function directGoodRefund(): void
{
Db::transaction(function (){
// $this->manageRefundOrder();
$this->manageOrderByRefund();
$this->manageSubCateringLog();
if ($this->orderInfo->coupin_id > 0) $this->rollbackCoupon(OrderCode::ORDER_TYPE_GOOD,$this->orderInfo);
});
}
private function setAliPayOrder(): null
{
return null;
@@ -176,7 +250,7 @@ class PlacePayService extends BaseService
$this->payInfo->user_id = $this->userId;
$this->payInfo->order_id = $this->orderId;
$this->payInfo->order_type = $this->orderType;
$this->payInfo->order_no = $this->orderInfo->order_no;
$this->payInfo->order_no = $this->orderInfo->order_sno;
$this->payInfo->pay_money = $this->orderInfo->actual_price;
$this->payInfo->recharge_type = $this->payType;
$this->payInfo->status = PayCode::WAIT_PAY;
@@ -195,7 +269,13 @@ class PlacePayService extends BaseService
if (empty($this->orderInfo)) throw new ErrException('该订单为空');
if ($this->orderInfo->user_id != $this->userId) throw new ErrException('该订单不属于你');
if ($this->orderInfo->status != OrderCode::WAIT_PAY) throw new ErrException('该订单已支付或已取消,请确认后重试');
}
/**
* @return void
*/
private function checkPayOrder(): void
{
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndType($this->orderId,$this->orderType);
if (empty($this->payInfo)) return;
if ($this->payInfo->status == PayCode::FINISH_PAY) throw new ErrException('该订单已支付,请确认后重试');

View File

@@ -150,14 +150,13 @@ abstract class WxJsRechargeBaseService implements ThirdPayInterface
'description' => '测试订单',
'amount' => [
'total' => (int)bcmul((string)$money, "100"),
'currency' => 'CNY'
],
'payer' => [
'openid' => $this->openId,
]
'sub_openid' => $this->openId,
],
];
$result = $this->ysdPay->wechat($this->config)->mini($wxOrder);
$result = $this->ysdPay->wechat(array_merge($this->config, ['_force' => true]))->mp($wxOrder);
$this->log->callbackLog(__CLASS__.'微信支付调起数据|回调地址:'. json_encode($this->config['wechat']['default']).'|回调数据:'.json_encode($result).'|请求数据:'.json_encode($wxOrder));
return $result;

View File

@@ -3,13 +3,13 @@
namespace App\Service\Common\Pay\Wx;
use App\Constants\Common\OrderCode;
use App\Model\Order;
use App\Service\ServiceTrait\Api\CateringTrait;
use App\Service\ServiceTrait\Api\CheckOrderCallBackTrait;
use App\Service\ServiceTrait\Api\CouponTrait;
use App\Service\ServiceTrait\Api\GoodOrderTrait;
use App\Service\ServiceTrait\Api\OrderTrait;
use App\Service\ServiceTrait\Api\PayFinishTrait;
use App\Service\ServiceTrait\Api\RefundOrderTrait;
use App\Service\ServiceTrait\CateringTrait;
use App\Service\ServiceTrait\Common\OrderChangeStatusTrait;
use Hyperf\DbConnection\Db;
use Psr\Container\ContainerExceptionInterface;
@@ -26,7 +26,8 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
RefundOrderTrait,
OrderTrait,
OrderChangeStatusTrait,
CateringTrait;
CateringTrait,
CouponTrait;
/**
*
@@ -121,6 +122,8 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
$this->manageOrderByRefund();
$this->manageSubCateringLog();
if ($this->orderInfo->coupin_id > 0) $this->rollbackCoupon(OrderCode::ORDER_TYPE_GOOD,$this->orderInfo);
});
$this->sendStockMq($this->orderInfo->id,$this->orderInfo->status);

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Service\ServiceTrait;
namespace App\Service\ServiceTrait\Api;
use App\Cache\Redis\Api\SiteCache;
use App\Constants\Admin\CateringCode;

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Service\ServiceTrait\Api;
use App\Constants\Common\CouponCode;
use App\Constants\Common\OrderCode;
use App\Model\UserCoupon;
use Exception;
use Hyperf\Di\Annotation\Inject;
trait CouponTrait
{
/**
* @var UserCoupon
*/
#[Inject]
protected UserCoupon $userCouponModel;
/**
* @param $orderType
* @param $orderInfo
* @return void
* @throws Exception
*/
protected function rollbackCoupon($orderType,$orderInfo): void
{
if ($orderType != OrderCode::ORDER_TYPE_GOOD) return;
if ($orderInfo->coupon_id <= 0) return;
$couponInfo = $this->userCouponModel->where('coupon_id', $orderInfo->coupon_id)->where('user_id',$orderInfo->user_id)->first();
if (empty($couponInfo)) return;
$couponInfo->status = CouponCode::COUPON_STATUS_UNUSED;
if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) $couponInfo->status = CouponCode::COUPON_STATUS_EXPIRE;
if (!$couponInfo->save()) throw new Exception('CancelOrderConsumer:error:couponStatusUpdateError:'.json_encode($orderInfo->toArray()));
}
}

View File

@@ -102,11 +102,10 @@ trait OrderTrait
protected function checkGood(): void
{
foreach ($this->cartFirstData as $key => $one) {
if (!in_array($key, $this->goodIds)) throw new ErrException('商品不存在',ApiCode::ORDER_GOOD_IN_EXISTENCE);
if ($this->orderType == 0) $this->orderType = $this->skuArr[$key]['type'];
if ($this->skuArr[$key]['type'] != $this->orderType) throw new ErrException('自选菜品跟套餐菜品请分开订单下单');
if (in_array($key, $this->goodIds)) continue;
throw new ErrException('商品不存在',ApiCode::ORDER_GOOD_IN_EXISTENCE);
}
}
@@ -206,6 +205,7 @@ trait OrderTrait
$this->skuArr = array_column($skuArr,null,'id');
$this->skuImageArr = array_column($skuArr,null,'id');
$this->goodIds = array_column($skuArr,'id');
unset($skuArr);
}