添加商品浏览历史
parent
6be635172b
commit
10211c3e54
|
|
@ -5,6 +5,7 @@ namespace App\Endpoint\Api\Http\Controllers\Product;
|
|||
use App\Endpoint\Api\Http\Controllers\Controller;
|
||||
use App\Endpoint\Api\Http\Resources\ProductSku\ProduckSkuResource;
|
||||
use App\Endpoint\Api\Http\Resources\ProductSku\ProductSkuSimpleResource;
|
||||
use App\Events\ProductSku\Viewed;
|
||||
use App\Helpers\Paginator as PaginatorHelper;
|
||||
use App\Models\ProductPart;
|
||||
use App\Models\ProductPartSku;
|
||||
|
|
@ -13,7 +14,7 @@ use App\Models\ProductSpu;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
|
||||
class ProductController extends Controller
|
||||
class ProductSkuController extends Controller
|
||||
{
|
||||
/**
|
||||
* 筛选商品
|
||||
|
|
@ -100,6 +101,8 @@ class ProductController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
Viewed::dispatch($request->user(), $sku, now());
|
||||
|
||||
return response()->json([
|
||||
'spu_specs' => $spuSpecs,
|
||||
'sku' => array_merge(ProduckSkuResource::make($sku)->resolve(), [
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Events\ProductSku;
|
||||
|
||||
use App\Models\ProductSku;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class Viewed
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* @param \App\Models\User|null $user 查看用户
|
||||
* @param \App\Models\ProductSku $sku 查看商品
|
||||
* @param \Illuminate\Support\Carbon $viewedAt 查看时间
|
||||
*/
|
||||
public function __construct(
|
||||
public ?User $user,
|
||||
public ProductSku $sku,
|
||||
public Carbon $viewedAt,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\ProductSku\Viewed;
|
||||
use App\Models\ProductViewLog;
|
||||
use Exception;
|
||||
|
||||
class CreateProductViewLog
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param \App\Events\ProductSku\Viewed $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Viewed $event)
|
||||
{
|
||||
if ($event->user === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
ProductViewLog::updateOrCreate([
|
||||
'user_id' => $event->user->id,
|
||||
'spu_id' => $event->sku->spu_id,
|
||||
'view_date' => $event->viewedAt->toDateString(),
|
||||
], [
|
||||
'sku_id' => $event->sku->id,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ProductViewLog extends Model
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'view_date' => 'date',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'spu_id',
|
||||
'sku_id',
|
||||
'view_date',
|
||||
];
|
||||
|
||||
/**
|
||||
* 此浏览记录所属的商品 sku
|
||||
*/
|
||||
public function sku()
|
||||
{
|
||||
return $this->belongsTo(ProductSku::class, 'sku_id');
|
||||
}
|
||||
}
|
||||
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
|
|
@ -15,8 +13,8 @@ class EventServiceProvider extends ServiceProvider
|
|||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
Registered::class => [
|
||||
SendEmailVerificationNotification::class,
|
||||
\App\Events\ProductSku\Viewed::class => [
|
||||
\App\Listeners\CreateProductViewLog::class,
|
||||
],
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateProductViewLogsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('product_view_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id')->comment('用户ID');
|
||||
$table->unsignedBigInteger('spu_id')->comment('SPU ID');
|
||||
$table->unsignedBigInteger('sku_id')->comment('SKU ID');
|
||||
$table->date('view_date')->comment('查看日期');
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'spu_id', 'view_date']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('product_view_logs');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue