diff --git a/beike/Models/Customer.php b/beike/Models/Customer.php index e08a0086..b0a9f67a 100644 --- a/beike/Models/Customer.php +++ b/beike/Models/Customer.php @@ -27,4 +27,9 @@ class Customer extends Authenticatable { return $this->belongsTo(CustomerGroup::class); } + + public function wishlists() : HasMany + { + return $this->hasMany(CustomerWishlist::class); + } } diff --git a/beike/Models/CustomerWishlist.php b/beike/Models/CustomerWishlist.php new file mode 100644 index 00000000..231e7491 --- /dev/null +++ b/beike/Models/CustomerWishlist.php @@ -0,0 +1,19 @@ +hasOne(Product::class); + } +} diff --git a/beike/Repositories/CustomerRepo.php b/beike/Repositories/CustomerRepo.php index 87a198ab..91394b61 100644 --- a/beike/Repositories/CustomerRepo.php +++ b/beike/Repositories/CustomerRepo.php @@ -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); + } } diff --git a/beike/Shop/Http/Controllers/Account/WishlistController.php b/beike/Shop/Http/Controllers/Account/WishlistController.php new file mode 100644 index 00000000..36987f5c --- /dev/null +++ b/beike/Shop/Http/Controllers/Account/WishlistController.php @@ -0,0 +1,53 @@ + + * @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); + } + +} diff --git a/beike/Shop/Http/Controllers/ProductController.php b/beike/Shop/Http/Controllers/ProductController.php index 555b5d1a..b728584f 100644 --- a/beike/Shop/Http/Controllers/ProductController.php +++ b/beike/Shop/Http/Controllers/ProductController.php @@ -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); diff --git a/beike/Shop/Http/Resources/ProductDetail.php b/beike/Shop/Http/Resources/ProductDetail.php new file mode 100644 index 00000000..2ba6336d --- /dev/null +++ b/beike/Shop/Http/Resources/ProductDetail.php @@ -0,0 +1,30 @@ + + * @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), + ]; + } +} diff --git a/beike/Shop/Http/Resources/SkuDetail.php b/beike/Shop/Http/Resources/SkuDetail.php new file mode 100644 index 00000000..635d32e3 --- /dev/null +++ b/beike/Shop/Http/Resources/SkuDetail.php @@ -0,0 +1,35 @@ + + * @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, + ]; + } +} diff --git a/database/migrations/2022_07_14_103842_wishlist.php b/database/migrations/2022_07_14_103842_wishlist.php new file mode 100644 index 00000000..07b0d10a --- /dev/null +++ b/database/migrations/2022_07_14_103842_wishlist.php @@ -0,0 +1,33 @@ +id(); + $table->unsignedInteger('customer_id'); + $table->unsignedInteger('product_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('customer_wishlists'); + } +}