diff --git a/beike/Helpers.php b/beike/Helpers.php index 0583adee..18ceae24 100644 --- a/beike/Helpers.php +++ b/beike/Helpers.php @@ -40,3 +40,30 @@ function locale(): string return 'zh_cn'; } +/** + * 货币格式化 + * + * @param $price + * @return string + */ +function currency_format($price): string +{ + return '$' . $price; +} + +/** + * 图片缩放 + * + * @param $image + * @param int $width + * @param int $height + * @return mixed|void + */ +function image_resize($image, int $width = 100, int $height = 100) +{ + if (\Illuminate\Support\Str::startsWith($image, 'http')) { + return $image; + } + return asset($image); +} + diff --git a/beike/Shop/Http/Resources/ProductList.php b/beike/Shop/Http/Resources/ProductList.php index 9033df94..d1eef3aa 100644 --- a/beike/Shop/Http/Resources/ProductList.php +++ b/beike/Shop/Http/Resources/ProductList.php @@ -20,6 +20,9 @@ class ProductList extends JsonResource return [ 'id' => $this->id, 'name' => $this->description->name ?? '', + 'price' => $this->price, + 'image' => image_resize($this->image), + 'price_format' => currency_format($this->price), 'category_id' => $this->category_id ?? null, ]; } diff --git a/beike/Shop/Repositories/ProductRepo.php b/beike/Shop/Repositories/ProductRepo.php index 68915d22..077aa7d7 100644 --- a/beike/Shop/Repositories/ProductRepo.php +++ b/beike/Shop/Repositories/ProductRepo.php @@ -13,7 +13,6 @@ namespace Beike\Shop\Repositories; use Beike\Models\Product; use Beike\Shop\Http\Resources\ProductList; -use Illuminate\Support\Collection; class ProductRepo { @@ -21,12 +20,12 @@ class ProductRepo * 通过多个产品分类获取产品列表 * * @param $categoryIds - * @return Collection + * @return array */ - public static function getProductsByCategories($categoryIds): Collection + public static function getProductsByCategories($categoryIds): array { $products = self::getProductsByCategory($categoryIds); - $items = collect($products)->groupBy('category_id'); + $items = collect($products)->groupBy('category_id')->jsonSerialize(); return $items; } @@ -49,6 +48,7 @@ class ProductRepo ->whereIn('c.id', $categoryId) ->get(); - return ProductList::collection($products)->jsonSerialize(); + $items = ProductList::collection($products)->jsonSerialize(); + return $items; } } diff --git a/database/migrations/2021_12_26_111435_create_tables.php b/database/migrations/2021_12_26_111435_create_tables.php index 41aaf9f6..6ee4fa0d 100644 --- a/database/migrations/2021_12_26_111435_create_tables.php +++ b/database/migrations/2021_12_26_111435_create_tables.php @@ -53,6 +53,7 @@ class CreateTables extends Migration Schema::create('products', function (Blueprint $table) { $table->id()->startingValue(100_000); $table->string('image')->default(''); + $table->decimal('price')->default(0); $table->string('video')->default(''); $table->integer('position')->default(0); $table->boolean('active')->default(0);