xiansin il y a 2 ans
Parent
commit
d38fa0285d

+ 2 - 1
mini/components/Index/Logged.vue

xqd
@@ -43,7 +43,8 @@ export default {
   },
   methods: {
     handleClick(data) {
-      this.$u.route({ url: `/pages/product/detail?id=${data.product_id}` })
+      const ids = data.product_id.join(',')
+      this.$u.route({ url: `/pages/product/list?ids=${ids}` })
     }
   }
 }

+ 3 - 0
mini/pages/product/list.vue

xqd xqd xqd
@@ -38,6 +38,7 @@ export default {
       loading: false,
       keywords: '',
       cate_id: 0,
+      ids: '',
       limit: 6,
       page: 1,
       isMore: true,
@@ -61,6 +62,7 @@ export default {
         limit: this.limit,
         keywords: this.keywords,
         cate_id: this.cate_id,
+        ids: this.ids,
         page: this.page
       }).then(res => {
         this.loading = false
@@ -77,6 +79,7 @@ export default {
   onLoad(options) {
     this.keywords = options.keywords
     this.cate_id = options?.cate_id
+    this.ids = options?.ids
     this.loading = true
     this.getLists()
   },

+ 5 - 3
server/app/Admin/Controllers/ProductHotController.php

xqd xqd
@@ -33,13 +33,15 @@ class ProductHotController extends AdminController
      */
     protected function grid()
     {
-        return Grid::make(ProductHot::with(['product']), function (Grid $grid) {
+        return Grid::make(new ProductHot(), function (Grid $grid) {
             $grid->model()->orderByDesc('sort');
             $grid->model()->where('type', $this->type);
             $grid->column('id')->sortable();
             $grid->column('cover_img')->image('',80);
             $grid->column('product_id')->display(function (){
-                return $this->product->name;
+                $this->products = $this->product_id;
+                $names  = $this->products->pluck('name')->toArray();
+                return implode('、',$names);
             });
             $grid->column('is_opened')->switch();
             $grid->column('sort')->editable();
@@ -84,7 +86,7 @@ class ProductHotController extends AdminController
             $form->display('id');
             $form->hidden('type')->value($this->type);
             $cates = Product::select(['id','name'])->where('is_opened',1)->get()->toArray();
-            $form->select('product_id')
+            $form->multipleSelect('product_id')
                 ->options(array_column($cates,'name','id'))
                 ->required();
             $form->image('cover_img')->saveFullUrl()

+ 7 - 0
server/app/Http/Controllers/V1/ProductController.php

xqd xqd
@@ -46,8 +46,12 @@ class ProductController extends Controller
         $keywords = $request->input('keywords', '');
         $page = request()->input('page',1);
         $cateId = request()->input('cate_id',0);
+        $ids = request()->input('ids','');
         $offset = ($page - 1) * $limit;
 
+        $ids = explode(",",$ids);
+
+
         $lists = Product::withCount(['viewer'])->where('is_opened', 1)
             ->when($keywords, function ($query, $keywords) {
                 /* @var Builder  $query*/
@@ -56,6 +60,9 @@ class ProductController extends Controller
             ->when($cateId, function ($query, $cateId) {
                 /* @var Builder  $query*/
                 return $query->where('cate_id', $cateId);
+            })->when($ids, function ($query, $ids) {
+                /* @var Builder  $query*/
+                return $query->whereIn('id',$ids);
             })
             ->orderByDesc('viewer_count')
             ->orderByDesc('sort')

+ 3 - 1
server/app/Http/Controllers/V1/SettingController.php

xqd
@@ -37,11 +37,13 @@ class SettingController extends Controller
 
     public function indexBanner(): JsonResponse
     {
+        $type = \user()->account->type != 1 ? 2 : 1;
         $lists = ProductHot::select(['cover_img as image','product_id'])
-            ->where('type',\user()->account->type)
+            ->where('type',$type)
             ->where('is_opened' ,1)
             ->orderByDesc('sort')
             ->get();
+
         return $this->success($lists);
     }
 }

+ 7 - 4
server/app/Models/ProductHot.php

xqd xqd xqd
@@ -2,6 +2,7 @@
 
 namespace App\Models;
 
+use App\Casts\DelimiterCast;
 use App\Casts\HttpToHttps;
 use Dcat\Admin\Traits\HasDateTimeFormatter;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -35,7 +36,7 @@ use Illuminate\Database\Eloquent\Model;
  * @method static \Illuminate\Database\Query\Builder|ProductHot withoutTrashed()
  * @mixin \Eloquent
  * @property string $cover_img 爆款图片
- * @property-read \App\Models\Product|null $product
+ * @property-write mixed $products
  * @method static \Illuminate\Database\Eloquent\Builder|ProductHot whereCoverImg($value)
  */
 class ProductHot extends Model
@@ -46,12 +47,14 @@ class ProductHot extends Model
     protected $table = 'product_hots';
 
     protected $casts = [
-        'cover_img' =>HttpToHttps::class
+        'cover_img' =>HttpToHttps::class,
+        'product_id' => DelimiterCast::class
     ];
 
-    public function product(): BelongsTo
+    public function setProductsAttribute(array $product_ids)
     {
-        return $this->belongsTo(Product::class,'product_id','id');
+        $products = Product::whereIn('id', $product_ids);
+        $this->attributes['products'] = $products;
     }
 
 }