商品收藏
This commit is contained in:
parent
2d0de819ea
commit
e240b8363a
|
|
@ -13,6 +13,6 @@ class CustomerWishlist extends Base
|
|||
|
||||
public function product(): HasOne
|
||||
{
|
||||
return $this->hasOne(Product::class);
|
||||
return $this->hasOne(Product::class, 'id', 'product_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,10 @@ namespace Beike\Repositories;
|
|||
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Models\CustomerWishlist;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class CustomerRepo
|
||||
|
|
@ -20,7 +24,7 @@ class CustomerRepo
|
|||
/**
|
||||
* 创建一个customer记录
|
||||
* @param $customerData
|
||||
* @return int
|
||||
* @return Builder|Model
|
||||
*/
|
||||
public static function create($customerData)
|
||||
{
|
||||
|
|
@ -51,7 +55,7 @@ class CustomerRepo
|
|||
|
||||
/**
|
||||
* @param $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
|
||||
* @return Builder|Builder[]|Collection|Model|null
|
||||
*/
|
||||
public static function find($id)
|
||||
{
|
||||
|
|
@ -69,9 +73,9 @@ class CustomerRepo
|
|||
|
||||
/**
|
||||
* @param $data
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
* @return LengthAwarePaginator
|
||||
*/
|
||||
public static function list($data)
|
||||
public static function list($data): LengthAwarePaginator
|
||||
{
|
||||
$builder = Customer::query()->with("customerGroup.description");
|
||||
|
||||
|
|
@ -102,7 +106,7 @@ class CustomerRepo
|
|||
/**
|
||||
* @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
|
||||
* @return Customer|Builder|Builder[]|Collection|Model|mixed|null
|
||||
*/
|
||||
public static function addToWishlist($customer, $productId)
|
||||
{
|
||||
|
|
@ -129,7 +133,7 @@ class CustomerRepo
|
|||
return $customer;
|
||||
}
|
||||
|
||||
public static function wishlists($customer)
|
||||
public static function wishlists($customer): LengthAwarePaginator
|
||||
{
|
||||
if (!$customer instanceof Customer) {
|
||||
$customer = Customer::query()->findOrFail($customer);
|
||||
|
|
|
|||
|
|
@ -13,39 +13,37 @@ 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 Beike\Shop\Http\Resources\Account\WishlistDetail;
|
||||
use Illuminate\Http\Request;
|
||||
use Beike\Repositories\CountryRepo;
|
||||
|
||||
class WishlistController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
public function index()
|
||||
{
|
||||
$wishlists = CustomerRepo::wishlists(current_customer(), $request->get('product_id'));
|
||||
$wishlists = CustomerRepo::wishlists(current_customer());
|
||||
$data = [
|
||||
'wishlist' => $wishlists,
|
||||
'wishlist' => WishlistDetail::collection($wishlists)->jsonSerialize(),
|
||||
];
|
||||
|
||||
return view('account/wishlist', $data);
|
||||
}
|
||||
|
||||
public function add(Request $request, $productId)
|
||||
public function add(Request $request): array
|
||||
{
|
||||
$productId = $request->get('product_id');
|
||||
CustomerRepo::addToWishlist(current_customer(), $productId);
|
||||
|
||||
$wishlists = CustomerRepo::wishlists(current_customer(), $request->get('product_id'));
|
||||
$wishlists = CustomerRepo::wishlists(current_customer());
|
||||
|
||||
return json_success('加入收藏成功', $wishlists);
|
||||
}
|
||||
|
||||
public function remove(Request $request, $productId)
|
||||
public function remove(Request $request): array
|
||||
{
|
||||
$productId = $request->get('product_id');
|
||||
CustomerRepo::removeFromWishlist(current_customer(), $productId);
|
||||
|
||||
$wishlists = CustomerRepo::wishlists(current_customer(), $request->get('product_id'));
|
||||
$wishlists = CustomerRepo::wishlists(current_customer());
|
||||
|
||||
return json_success('移除收藏成功', $wishlists);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
/**
|
||||
* WishlistDetail.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-07-25 20:39:55
|
||||
* @modified 2022-07-25 20:39:55
|
||||
*/
|
||||
|
||||
namespace Beike\Shop\Http\Resources\Account;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class WishlistDetail extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
$data = [
|
||||
'id' => $this->id,
|
||||
'image' => image_resize($this->product->image, 100, 100),
|
||||
'product_name' => $this->product->description->name,
|
||||
'price' => currency_format($this->product->price)
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue