fixed home page

This commit is contained in:
Edward Yang 2022-06-23 15:25:11 +08:00
parent d3c0b617cb
commit e2cb93b6dc
4 changed files with 36 additions and 5 deletions

View File

@ -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);
}

View File

@ -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,
];
}

View File

@ -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;
}
}

View File

@ -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);