From 5f37fc0343a9b53808c665159f89468b535a603f Mon Sep 17 00:00:00 2001 From: mattoid Date: Thu, 4 Jul 2024 14:41:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=89=A3=E6=AC=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locale/en.yml | 4 +++ locale/zh-Hans.yml | 4 +++ src/Console/Command/GoodsInvalidCommand.php | 32 ++++++++++--------- src/Extend/StoreExtend.php | 34 ++++++++++----------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/locale/en.yml b/locale/en.yml index f60fb4d..5b51413 100644 --- a/locale/en.yml +++ b/locale/en.yml @@ -61,6 +61,10 @@ mattoid-store: buy-goods-fail: Purchase failed [{title}] file-exist: file already exist automatic-renewal: Automatic renewal not enabled + invalid-product: Invalid product + automatic-renewal-fail: Automatic renewal failed [{message}] + goods-invalid-fail: Product failure logic execution failed [{message}] + goods-invalid-event-fail: Product failure event push failed [{message}] lib: item-status-all: All product statuses diff --git a/locale/zh-Hans.yml b/locale/zh-Hans.yml index b3ad03a..2fe827a 100644 --- a/locale/zh-Hans.yml +++ b/locale/zh-Hans.yml @@ -56,6 +56,10 @@ mattoid-store: buy-goods-fail: 购买【{title}】失败 file-exist: 文件已存在 automatic-renewal: 未开启自动续费 + invalid-product: 无效商品 + automatic-renewal-fail: 自动续费失败【{message}】 + goods-invalid-fail: 商品失效逻辑执行失败【{message}】 + goods-invalid-event-fail: 商品失效事件推送失败【{message}】 lib: item-status-all: 全部商品状态 diff --git a/src/Console/Command/GoodsInvalidCommand.php b/src/Console/Command/GoodsInvalidCommand.php index ae1a91a..738a82d 100644 --- a/src/Console/Command/GoodsInvalidCommand.php +++ b/src/Console/Command/GoodsInvalidCommand.php @@ -5,7 +5,7 @@ use Carbon\Carbon; use Flarum\Console\AbstractCommand; use Flarum\Foundation\ValidationException; -use Flarum\Locale\Translator; +use Symfony\Contracts\Translation\TranslatorInterface; use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\User; use Illuminate\Contracts\Events\Dispatcher; @@ -20,31 +20,30 @@ class GoodsInvalidCommand extends AbstractCommand protected $events; protected $settings; - public function __construct() { + public function __construct(SettingsRepositoryInterface $settings, TranslatorInterface $translator, Repository $cache, Dispatcher $events) { parent::__construct(); - $this->cache = resolve(Repository::class); - $this->events = resolve(Dispatcher::class); - $this->translator = resolve(Translator::class); - $this->settings = resolve(SettingsRepositoryInterface::class); + $this->cache = $cache; + $this->events = $events; + $this->settings = $settings; + $this->translator = $translator; } protected function configure() { - $this->setName('mattoid:store:check:date')->setDescription('Check the expiration time of the goods'); + $this->setName('mattoid:store:check:date')->setDescription('Check the expiration time of the product'); } protected function fire() { $storeMap = []; - $dateTime = Carbon::now()->tz($this->settings->get($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai'))); + $dateTime = Carbon::now()->tz($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai')); $invalidList = StoreCartModel::query()->where('outtime', '<=', $dateTime)->where('type', 'limit')->where('status', 1)->get(); - if (!$invalidList) { // 未发现失效商品,跳过处理 return; } - $storeIdList = array_column(json_decode($invalidList, true), 'id'); + $storeIdList = array_column(json_decode($invalidList, true), 'store_id'); $storeList = StoreModel::query()->whereIn('id', $storeIdList)->get(); foreach ($storeList as $store) { $storeMap[$store->id] = $store; @@ -59,7 +58,7 @@ protected function fire() $this->autoDeduction($store, $cart); } catch (\Exception $e) { $buyStatus = false; - $this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: Automatic fee deduction failed【{$e->getMessage()}】"); + $this->error("[{$cart->code}] {$cart->store_id}:{$cart->id}: {$this->translator->trans('mattoid-store.forum.error.automatic-renewal-fail', ['message' => $e->getMessage()])}"); try { // 超期商品自动失效 $invalid = StoreExtend::getInvalid($cart->code); @@ -70,14 +69,14 @@ protected function fire() $cart->status = 2; $cart->save(); } catch (\Exception $e) { - $this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: Failed to execute product invalidation logic【{$e->getMessage()}】"); + $this->error("[{$cart->code}] {$cart->store_id}:{$cart->id}: {$this->translator->trans('mattoid-store.forum.error.goods-invalid-fail', ['message' => $e->getMessage()])}"); } } try { $this->events->dispatch(new StoreInvalidEvent($store, $cart, $buyStatus)); } catch (\Exception $e) { - $this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: Failed to notify product expiration event【{$e->getMessage()}】"); + $this->error("[{$cart->code}] {$cart->store_id}:{$cart->id}: {$this->translator->trans('mattoid-store.forum.error.goods-invalid-event-fail', ['message' => $e->getMessage()])}"); } } } @@ -85,7 +84,7 @@ protected function fire() private function autoDeduction(StoreModel $store, StoreCartModel $cart) { // 商品未开启自动扣费 - if ($cart->auto_deduction) { + if (!$cart->auto_deduction) { throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.automatic-renewal')]); } @@ -94,7 +93,7 @@ private function autoDeduction(StoreModel $store, StoreCartModel $cart) throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.invalid-product')]); } - $key = md5("{$store->store_id}-{$cart->user_id}"); + $key = md5("{$cart->store_id}-{$cart->user_id}"); if (!$this->cache->add($key, time(), 5)) { throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.validate-fail')]); } @@ -114,6 +113,7 @@ private function autoDeduction(StoreModel $store, StoreCartModel $cart) } // 刷新过期时间 + $cart->pay_amt = $cart->price; $cart->outtime = Carbon::now()->tz($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai'))->addDays($store->outtime); $cart->created_at = Carbon::now()->tz($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai')); $cart->save(); @@ -122,5 +122,7 @@ private function autoDeduction(StoreModel $store, StoreCartModel $cart) if (class_exists('Mattoid\MoneyHistory\Event\MoneyHistoryEvent')) { $this->events->dispatch(new \Mattoid\MoneyHistory\Event\MoneyHistoryEvent($user, -$cart->price, 'AUTODEDUCTION', $this->translator->trans("mattoid-store.forum.auto-deduction", ['title' => $store->title]), '')); } + + $this->cache->delete($key); } } diff --git a/src/Extend/StoreExtend.php b/src/Extend/StoreExtend.php index 9d3fb7a..448a0ca 100644 --- a/src/Extend/StoreExtend.php +++ b/src/Extend/StoreExtend.php @@ -19,7 +19,7 @@ class StoreExtend implements ExtenderInterface, LifecycleInterface private static $goodList = []; private static $afterList = []; private static $validateList = []; - private static $invalidList = []; + public static $invalidList = []; public function __construct(string $key = '') @@ -55,38 +55,38 @@ public function addInvalid($callback): self public static function getStoreGoods(String $key) { - $class = StoreExtend::$goodList[$key]; - if (!class_exists($class)) { - return null; + if (array_key_exists($key, StoreExtend::$goodList)) { + $class = StoreExtend::$goodList[$key]; + return new $class; } - return new $class; + return null; } public static function getValidate(String $key) { - $class = StoreExtend::$validateList[$key]; - if (!class_exists($class)) { - return null; + if (array_key_exists($key, StoreExtend::$validateList)) { + $class = StoreExtend::$validateList[$key]; + return new $class; } - return new $class; + return null; } public static function getAfter(String $key) { - $class = StoreExtend::$afterList[$key]; - if (!class_exists($class)) { - return null; + if (array_key_exists($key, StoreExtend::$afterList)) { + $class = StoreExtend::$afterList[$key]; + return new $class; } - return new $class; + return null; } public static function getInvalid(string $key) { - $class = StoreExtend::$invalidList[$key]; - if (!class_exists($class)) { - return null; + if (array_key_exists($key, StoreExtend::$invalidList)) { + $class = StoreExtend::$invalidList[$key]; + return new $class; } - return new $class; + return null; } public function extend(Container $container, Extension $extension = null)