Skip to content

Commit

Permalink
修改 商品失效逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
liufei-ereach committed Jul 4, 2024
1 parent 350bea4 commit 6695f1e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
71 changes: 65 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ php flarum cache:clear
```php
(new StoreExtend('商品的唯一KEY,不可与其他拓展重复'))
// 注册商品信息
->addStoreGoods(InviteGoods::class)
->addStoreGoods(Goods::class)
// 注册前置校验器
->addValidate(InviteValidate::class)
->addValidate(Validate::class)
// 注册商品的业务逻辑
->addAfter(InviteAfter::class)
->addAfter(After::class)
// 商品失效逻辑
->addInvalid(Invalid::class)
```

### 事件简介
Expand All @@ -41,12 +43,69 @@ php flarum cache:clear

- 购买后执行

用户点击购买按钮后,本插件会自动验证用户资金、商品购买权限等。确认无误后执行`Mattoid\Store\Goods\After`
用户点击购买按钮后,本插件会自动验证用户资金、商品购买权限等。确认无误后执行`Mattoid\Store\Goods\After`类。

操作完成后通知事件 `Mattoid\Store\Event\StoreBuyEvent`


- 商品失效(扣费失败)时执行

通过 `php flarum schedule:run` 命令定时查询失效商品,若商品开启自动扣费选项,则直接扣费(仅扣费不操作库存)。
若为开启自动扣费或扣费失败则执行 `Mattoid\Store\Goods\Invalid` 类。

操作完成后通知事件 `Mattoid\Store\Event\StoreInvalidEvent`


- 商品信息

注册产品信息(显示在管理端的添加产品页面上),用户需要重写并注册 `Mattoid\Store\Goods\Goods` 类。


## 事件介绍
所有事件均不受事务管理,仅为执行结束行为的通知,不做任何拦截等处理。若要想实现事务原子性,
请使用 `Mattoid\Store\Extend\StoreExtend` 类进行注册,该类注册的所有能力均视为本插件自有业务逻辑。
(例如:余额不足时,不执行 `Mattoid\Store\Event\StoreBuyEvent` 以及之后的逻辑;`Mattoid\Store\Goods\After` 执行失败时自动退款并增加库存)

事件通常不需要商品插件做特殊处理,本插件推荐使用 `Mattoid\Store\Extend\StoreExtend` 类的方式进行因为逻辑注册

### 购买失败事件
`Mattoid\Store\Event\StoreBuyFailEvent`

该事件由本插件监听,若商品插件执行自有业务逻辑失败导致需要回滚购买信息时可通知该事件,本插件会自动处理库存以及退款等操作
(若用用的是 `Mattoid\Store\Extend\StoreExtend` 注册的商品业务逻辑,则不需要考虑该事件。)

### 购买通知事件
`Mattoid\Store\Event\StoreBuyEvent`

该事件由商品插件自行监听,用于通知商品插件购买结束,商品插件可执行购买后的操作

### 添加购物车
`Mattoid\Store\Event\StoreCartAddEvent`

该事件由本插件监听,商品插件可通过通知该事件实现添加购物车的操作(该事件会自动减扣库存)

### 编辑购物车
`Mattoid\Store\Event\StoreCartEditEvent`

该事件由本插件监听,商品插件可通过通知该事件实现编辑购物车的操作(若 `status` 大于1,则该事件会自动增加库存)

### 回滚库存
`Mattoid\Store\Event\StoreStockAddEvent`

该事件由本插件监听,商品插件可通过通知该事件实现库存回滚操作

### 扣除库存
`Mattoid\Store\Event\StoreStockSubEvent`

该事件由本插件监听,商品插件可通过通知该事件实现库存减扣操作

### 商品失效事件
`Mattoid\Store\Event\StoreInvalidEvent`

该事件由商品插件监听,在商品失效时便会通知该事件(无论是否扣款成功)


- 商品信息`Mattoid\Store\Goods\Goods`

注册产品信息(显示在管理端的添加产品页面上)

### Goods plugin
- [邀请码(审核版)](https://github.com/invites-fun/flarum-ext-store-invite)
Expand Down
14 changes: 10 additions & 4 deletions src/Console/Command/GoodsInvalidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected function fire()
}

foreach ($invalidList as $cart) {
$buyStatus = true;
// 获取商品信息
$store = $storeMap[$cart->store_id];
try {
Expand All @@ -59,19 +60,24 @@ protected function fire()
$this->autoDeduction($store, $cart);
}
} catch (\Exception $e) {
$this->info("[{$cart->code}]-{$cart->id}-{$cart->store_id}: 自动扣费失败【{$e->getMessage()}");
$buyStatus = false;
$this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: 自动扣费失败【{$e->getMessage()}");
try {
// 超期商品自动失效
StoreExtend::getInvalid($cart->code);

$cart->status = 2;
$cart->save();

$this->events->dispatch(new StoreInvalidEvent($store, $cart));
} catch (\Exception $e) {
$this->info("[{$cart->code}]-{$cart->id}-{$cart->store_id}: 通知商品失效逻辑失败{$e->getMessage()}");
$this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: 执行商品失效逻辑失败{$e->getMessage()}");
}
}

try {
$this->events->dispatch(new StoreInvalidEvent($store, $cart, $buyStatus));
} catch (\Exception $e) {
$this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: 通知商品失效事件失败【{$e->getMessage()}");
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/Controller/BuyGoodsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Cache\Repository as CacheContract;
use Mattoid\Store\Serializer\GoodsSerializer;
use Mockery\Exception;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

Expand Down Expand Up @@ -124,7 +125,12 @@ protected function data(ServerRequestInterface $request, Document $document) {
if ($after) {
// 商品处理失败,通知购买失败事件进行回滚操作
// $this->translator, $this->settings, $this->events, $this->cache
if (!$after->after($actor, $store, $params)) {
try {
if (!$after->after($actor, $store, $params)) {
$this->events->dispatch(new StoreBuyFailEvent($user, $store, $cart, $params));
throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.buy-goods-fail', ['title' => $store->title])]);
}
} catch (Exception $e) {
$this->events->dispatch(new StoreBuyFailEvent($user, $store, $cart, $params));
throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.buy-goods-fail', ['title' => $store->title])]);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Event/StoreInvalidEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

namespace Mattoid\Store\Event;

use Flarum\User\User;
use Mattoid\Store\Model\StoreCartModel;
use Mattoid\Store\Model\StoreModel;

class StoreInvalidEvent
{
public $cart;
public $store;
public $status;

public function __construct(StoreModel $store, StoreCartModel $cart)
/**
* @param StoreModel $store
* @param StoreCartModel $cart
* @param $status // 购买状态 true-成功 false-失败
*/
public function __construct(StoreModel $store, StoreCartModel $cart, $status)
{
$this->cart = $cart;
$this->store = $store;
$this->status = $status;
}
}

0 comments on commit 6695f1e

Please sign in to comment.