feat: order

This commit is contained in:
2025-01-22 17:58:36 +08:00
parent 911c4fcf70
commit b2f96de226
7 changed files with 287 additions and 1 deletions

View File

@@ -16,4 +16,14 @@ class ApiCode extends ReturnCode
* @Message("登录token已失效")
*/
public const int LOGIN_TOKEN_ERROR = 10002;
/**
* @Message("商品不存在")
*/
public const int ORDER_GOOD_INEXISTENCE = 20001;
/**
* @Message("商品库存不足")
*/
public const int ORDER_GOOD_INSUFFICIENT_STOCK = 20002;
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Service\Api\Order\CheckCartService;
use App\Service\Api\Order\PlaceOrderService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: 'api/order')]
//#[Middlewares([
// JwtAuthMiddleware::class,
//])]
class OrderController extends AbstractController
{
#[RequestMapping(path: 'check_cart',methods: 'post')]
#[Scene(scene: 'check_cart')]
public function checkCart()
{
return (new CheckCartService)->handle();
}
#[RequestMapping(path: 'place_order',methods: 'post')]
#[Scene(scene: 'place_order')]
public function placeOrder()
{
return (new PlaceOrderService)->handle();
}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
#[Controller(prefix: 'api/order')]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class PayController extends AbstractController
{
public function index()
{
}
}

View File

@@ -35,7 +35,7 @@ class ErrExceptionHandler extends ExceptionHandler
{
if ($throwable instanceof ErrException) {
$urlArr = explode('/',$this->request->path());
var_dump($throwable);
$result = match ($urlArr[0]) {
'api' => $this->apiReturn->error($throwable->getMessage(),$throwable->getCode()),
'admin', 'common' => $this->adminReturn->error($throwable->getMessage(),$throwable->getCode()),

View File

@@ -0,0 +1,125 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Order;
use App\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\Api\GoodCache;
use App\Cache\Redis\RedisCache;
use App\Constants\ApiCode;
use App\Exception\ErrException;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\OrderTrait;
use App\Service\ServiceTrait\Common\CycleTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Hyperf\Config\config;
class CheckCartService extends BaseService
{
use CycleTrait,OrderTrait;
/**
* @var int 周期id
*/
private int $cycleId;
/**
* @var array
*/
private array $goodIds = [];
/**
* @var string 库存key
*/
private string $stockKey;
/**
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @var array
*/
private array $cartSecondData;
/**
* @var array
*/
private array $cartFirstData;
/**
* @var int
*/
private int $copies;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct()
{
parent::__construct();
$this->cycleId = (int)$this->initTodayCycleId();
$this->cartFirstData = [];
$this->cartSecondData = [];
$this->copies = 0;
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
throw new ErrException(ApiCode::getMessage(ApiCode::LOGIN_ERROR),ApiCode::LOGIN_ERROR);
$kitchenId = (int)$this->request->input('kitchen_id');
$this->getGoodInfo($kitchenId);
$this->buildCartData(json_decode($this->request->input('cart'), true));
$this->checkGood($this->goodIds);
$this->checkStock();
$this->return->success();
}
/**
* 获取商品信息
* @param int $kitchenId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getGoodInfo(int $kitchenId): void
{
$this->stockKey = ApiRedisKey::goodStockKey($this->cycleId,$kitchenId);
$mealGoodKey = ApiRedisKey::mealGoodListKey($this->cycleId,$kitchenId);
$optionalGoodKey = ApiRedisKey::optionalGoodListKey($this->cycleId,$kitchenId);
$mealGood = $this->redis->get($mealGoodKey);
$optionalGood = $this->redis->get($optionalGoodKey);
if (empty($mealGood) || empty($optionalGood)) throw new ErrException('商品不存在');
if ($this->redis->exists($this->stockKey)) throw new ErrException('库存不足');
$mealGood = json_decode($mealGood, true);
$optionalGood = json_decode($optionalGood, true);
$this->goodIds = array_merge(array_column($mealGood, 'id'), array_column($optionalGood, 'id'));
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Order;
use App\Service\Api\BaseService;
class PlaceOrderService extends BaseService
{
/**
* @var int 1=微信支付 2=支付宝支付 3=余额支付
*/
private int $type;
public function handle()
{
}
}

View File

@@ -0,0 +1,68 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\ServiceTrait\Api;
use App\Constants\ApiCode;
use App\Exception\ErrException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait OrderTrait
{
/**
* @param array $data
* @return void
*/
protected function buildCartData(array $data): void
{
$copies = 0;
foreach ($data as $oneCopies) {
$one = [];
foreach ($oneCopies as $oneGood) {
$this->cartFirstData[$oneGood] = ($this->cartFirstData[$oneGood] ?? 0) + 1;
$one[$oneGood] = ($one[$oneGood] ?? 0) + 1;
}
$this->cartSecondData[] = $one;
$copies++;
}
$this->copies = $copies;
}
/**
* @param array $goodIds
* @return void
*/
protected function checkGood(array $goodIds): void
{
foreach ($this->cartFirstData as $key => $one) {
if (in_array($key, $goodIds)) continue;
throw new ErrException('商品不存在');
}
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function checkStock(): void
{
foreach ($this->cartFirstData as $key => $one) {
if (($this->redis->zScore($this->stockKey,$key) ?? 0) >= $one) continue;
throw new ErrException('商品库存不足');
}
}
}