parent
6bcec9cf37
commit
0a70373fc1
|
|
@ -27,4 +27,9 @@ class Customer extends Authenticatable
|
|||
{
|
||||
return $this->belongsTo(CustomerGroup::class);
|
||||
}
|
||||
|
||||
public function wishlists() : HasMany
|
||||
{
|
||||
return $this->hasMany(CustomerWishlist::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Beike\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class CustomerWishlist extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['customer_id', 'product_id'];
|
||||
|
||||
public function product(): HasOne
|
||||
{
|
||||
return $this->hasOne(Product::class);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@
|
|||
namespace Beike\Repositories;
|
||||
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Models\CustomerWishlist;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class CustomerRepo
|
||||
|
|
@ -97,5 +98,44 @@ class CustomerRepo
|
|||
{
|
||||
Customer::withTrashed()->find($id)->restore();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $customer, Customer对象或id
|
||||
* @param $productId
|
||||
* @return Customer|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|mixed|null
|
||||
*/
|
||||
public static function addToWishlist($customer, $productId)
|
||||
{
|
||||
if (!$customer instanceof Customer) {
|
||||
$customer = Customer::query()->findOrFail($customer);
|
||||
}
|
||||
$customer->wishlists()->save(new CustomerWishlist(['product_id' => $productId]));
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $customer, Customer对象或id
|
||||
* @param $productId
|
||||
* @return void
|
||||
*/
|
||||
public static function removeFromWishlist($customer, $productId)
|
||||
{
|
||||
if (!$customer instanceof Customer) {
|
||||
$customer = Customer::query()->findOrFail($customer);
|
||||
}
|
||||
$customer->wishlists()->where('product_id', $productId)->delete();
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
public static function wishlists($customer)
|
||||
{
|
||||
if (!$customer instanceof Customer) {
|
||||
$customer = Customer::query()->findOrFail($customer);
|
||||
}
|
||||
|
||||
return $customer->wishlists()->with('product.description')->paginate(20);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* WishlistController.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-07-14 20:47:04
|
||||
* @modified 2022-07-14 20:47:04
|
||||
*/
|
||||
|
||||
namespace Beike\Shop\Http\Controllers\Account;
|
||||
|
||||
use Beike\Repositories\CustomerRepo;
|
||||
use Beike\Shop\Http\Controllers\Controller;
|
||||
use Beike\Shop\Http\Requests\AddressRequest;
|
||||
use Beike\Shop\Http\Resources\Account\AddressResource;
|
||||
use Beike\Repositories\AddressRepo;
|
||||
use Beike\Shop\Services\AddressService;
|
||||
use Illuminate\Http\Request;
|
||||
use Beike\Repositories\CountryRepo;
|
||||
|
||||
class WishlistController extends Controller
|
||||
{
|
||||
public function index(Request $request, int $customerId)
|
||||
{
|
||||
$wishlists = CustomerRepo::wishlists($customerId, $request->get('product_id'));
|
||||
$data = [
|
||||
'wishlist' => $wishlists,
|
||||
];
|
||||
|
||||
return view('account/wishlist', $data);
|
||||
}
|
||||
|
||||
public function add(Request $request, $customerId, $productId)
|
||||
{
|
||||
CustomerRepo::addToWishlist($customerId, $productId);
|
||||
|
||||
$wishlists = CustomerRepo::wishlists($customerId, $request->get('product_id'));
|
||||
|
||||
return json_success('加入收藏成功', $wishlists);
|
||||
}
|
||||
|
||||
public function remove(Request $request, $customerId, $productId)
|
||||
{
|
||||
CustomerRepo::removeFromWishlist($customerId, $productId);
|
||||
|
||||
$wishlists = CustomerRepo::wishlists($customerId, $request->get('product_id'));
|
||||
|
||||
return json_success('移除收藏成功', $wishlists);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ namespace Beike\Shop\Http\Controllers;
|
|||
|
||||
use Beike\Models\Product;
|
||||
use Beike\Repositories\ProductRepo;
|
||||
use Beike\Shop\Http\Resources\ProductDetail;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProductController extends Controller
|
||||
|
|
@ -13,7 +14,7 @@ class ProductController extends Controller
|
|||
$product = ProductRepo::getProductDetail($product);
|
||||
|
||||
$data = [
|
||||
'product' => $product,
|
||||
'product' => (new ProductDetail($product)),
|
||||
];
|
||||
|
||||
return view('product', $data);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* ProductDetail.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-06-23 11:33:06
|
||||
* @modified 2022-06-23 11:33:06
|
||||
*/
|
||||
|
||||
namespace Beike\Shop\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductDetail extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->description->name ?? '',
|
||||
'description' => $this->description->description ?? '',
|
||||
'image' => image_resize($this->image),
|
||||
'category_id' => $this->category_id ?? null,
|
||||
'variables' => json_decode($this->variables),
|
||||
'skus' => SkuDetail::collection($this->skus),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
/**
|
||||
* SkuDetail.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-07-20 11:33:06
|
||||
* @modified 2022-07-20 11:33:06
|
||||
*/
|
||||
|
||||
namespace Beike\Shop\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class SkuDetail extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'variants' => json_decode($this->varians),
|
||||
'position' => $this->position,
|
||||
'image' => image_resize($this->image),
|
||||
'model' => $this->model,
|
||||
'sku' => $this->sku,
|
||||
'price' => $this->price,
|
||||
'price_format' => currency_format($this->price),
|
||||
'origin_price' => $this->origin_price,
|
||||
'origin_price_format' => currency_format($this->origin_price),
|
||||
'quantity' => $this->quantity,
|
||||
'is_default' => $this->is_default,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class Wishlist extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('customer_wishlists', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedInteger('customer_id');
|
||||
$table->unsignedInteger('product_id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('customer_wishlists');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue