6
0
Fork 0
release
李静 2021-12-21 13:46:17 +08:00
parent b09fc5e01e
commit 21ab5b4fc4
4 changed files with 26 additions and 11 deletions

View File

@ -5,7 +5,7 @@ namespace App\Endpoint\Api\Http\Controllers\Product;
use App\Endpoint\Api\Http\Controllers\Controller; use App\Endpoint\Api\Http\Controllers\Controller;
use App\Endpoint\Api\Http\Resources\ProduckSkuResource; use App\Endpoint\Api\Http\Resources\ProduckSkuResource;
use App\Endpoint\Api\Http\Resources\ProductSkuTinyResource; use App\Endpoint\Api\Http\Resources\ProductSkuTinyResource;
use App\Events\ProductSku\Viewed; use App\Events\ProductSkuViewed;
use App\Helpers\Paginator; use App\Helpers\Paginator;
use App\Models\ProductSku; use App\Models\ProductSku;
use App\Models\ProductSkuFavorite; use App\Models\ProductSkuFavorite;
@ -89,7 +89,7 @@ class ProductSkuController extends Controller
} }
} }
Viewed::dispatch($user, $sku, now()); ProductSkuViewed::dispatch($user, $sku, now());
// 是否收藏商品 // 是否收藏商品
$isCollected = (bool) $user?->skuFavorites()->where('sku_id', $sku->id)->exists(); $isCollected = (bool) $user?->skuFavorites()->where('sku_id', $sku->id)->exists();

View File

@ -1,14 +1,15 @@
<?php <?php
namespace App\Events\ProductSku; namespace App\Events;
use App\Models\ProductSku; use App\Models\ProductSku;
use App\Models\User; use App\Models\User;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
class Viewed class ProductSkuViewed
{ {
use Dispatchable; use Dispatchable;
use SerializesModels; use SerializesModels;
@ -24,4 +25,14 @@ class Viewed
public Carbon $viewedAt, public Carbon $viewedAt,
) { ) {
} }
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
} }

View File

@ -2,19 +2,23 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\ProductSku\Viewed; use App\Events\ProductSkuViewed;
use App\Models\ProductViewLog; use App\Models\ProductViewLog;
use Exception; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Throwable;
class CreateProductViewLog class CreateProductViewLog implements ShouldQueue
{ {
use InteractsWithQueue;
/** /**
* Handle the event. * Handle the event.
* *
* @param \App\Events\ProductSku\Viewed $event * @param \App\Events\ProductSkuViewed $event
* @return void * @return void
*/ */
public function handle(Viewed $event) public function handle(ProductSkuViewed $event)
{ {
if ($event->user === null) { if ($event->user === null) {
return; return;
@ -28,7 +32,7 @@ class CreateProductViewLog
], [ ], [
'sku_id' => $event->sku->id, 'sku_id' => $event->sku->id,
]); ]);
} catch (Exception $e) { } catch (Throwable $e) {
report($e); report($e);
} }
} }

View File

@ -13,7 +13,7 @@ class EventServiceProvider extends ServiceProvider
* @var array * @var array
*/ */
protected $listen = [ protected $listen = [
\App\Events\ProductSku\Viewed::class => [ \App\Events\ProductSkuViewed::class => [
\App\Listeners\CreateProductViewLog::class, \App\Listeners\CreateProductViewLog::class,
], ],
\App\Events\OrderPaid::class => [ \App\Events\OrderPaid::class => [