panghu 8 місяців тому
батько
коміт
947579f57b

+ 91 - 0
app/Http/Controllers/Api/GoodsController.php

@@ -0,0 +1,91 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+
+use App\Http\Controllers\ApiController;
+use App\Models\Goods;
+use App\Models\GoodsCategory;
+use App\Servers\CommonServer;
+use App\Servers\GoodsSpecServer;
+
+/**
+ * 外卖商品
+ */
+class GoodsController extends ApiController
+{
+
+
+    /**
+     * 获取商家分类
+     */
+    function getShopCategoryList()
+    {
+        $commonServer = CommonServer::creatServer();
+        $shop_id = $commonServer->filtrationStr('shop_id');//商品id
+        if (empty($shop_id)) return $this->apiResponseError('请选择商家');
+
+        $list = GoodsCategory::where('is_del',0)
+            ->where('shop_id',$shop_id)
+            ->select(['id as category_id','name','type'])
+            ->orderBy('sort','desc')
+            ->get();
+
+
+        return $this->apiResponseSuccess('成功',$list);
+    }
+
+    /**
+     * 获取分类下商品
+     */
+    function getShopGoodsList()
+    {
+        $m_id = $this->getMemberId();//用户id
+        $commonServer = CommonServer::creatServer();
+        $shop_id = $commonServer->filtrationStr('shop_id');//商品id
+        if (empty($shop_id)) return $this->apiResponseError('请选择商家');
+
+
+        $category = GoodsCategory::where('is_del',0)
+            ->where('shop_id',$shop_id)
+            ->select(['id as type','name'])
+            ->orderBy('sort','desc')
+            ->get();
+        foreach ($category as $value){
+            $list = Goods::where('is_del',0)
+                ->where('status',1)
+                ->where('shop_id',$shop_id)
+                ->where('category_id',$value['type'])
+                ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','spec_type','line_price','sales','is_infinite','stock'])
+                ->orderBy('sort','desc')
+                ->get();
+            foreach ($list as $goods){
+                $goods->{'num'} = 0;//前端需要
+            }
+            $value->{'list'} = $list;
+        }
+
+        return $this->apiResponseSuccess('成功',$category);
+    }
+
+    /**
+     * 获取商品sku
+     */
+    function getGoodsSpec()
+    {
+        $m_id = $this->getMemberId();//用户id
+        $commonServer = CommonServer::creatServer();
+        $goods_id = $commonServer->filtrationStr('goods_id');//商品id
+        if (empty($goods_id)) return $this->apiResponseError('请选择商品');
+
+        $spec_type = Goods::where('id',$goods_id)->where('is_del',0)->value('spec_type');
+        if ($spec_type == 2){
+            $sku = GoodsSpecServer::creatServer()->getGoodsSku($goods_id);
+        }else{
+            $sku = [];
+        }
+
+        return $this->apiResponseSuccess('成功',$sku);
+    }
+
+}

+ 58 - 0
app/Servers/GoodsSpecServer.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Servers;
+
+
+
+use App\Models\GoodsSku;
+use App\Models\GoodsSpec;
+
+/**
+ * 商品规格服务
+ */
+class GoodsSpecServer
+{
+    static private $server = '';
+
+    private function __construct()
+    {
+
+    }
+
+    /**
+     * 创建对象
+     * @return GoodsSpecServer
+     */
+    static function creatServer()
+    {
+        if (empty(self::$server)) {
+            self::$server = new  GoodsSpecServer();
+        }
+        return self::$server;
+    }
+
+    /**
+     * 获取商品规格信息
+     * @param $goods_id
+     * @return array
+     */
+    function getGoodsSku($goods_id)
+    {
+        $sku = GoodsSku::where('goods_id',$goods_id)
+            ->where('is_del',0)
+            ->select(['id as sku_id','sku_name'])
+            ->orderBy('id','asc')
+            ->get();
+        foreach ($sku as $value){
+            $spec_list = GoodsSpec::where('goods_id', $goods_id)
+                ->where('is_del',0)
+                ->where('sku_id',$value->{'sku_id'})
+                ->select(['id as spec_id','spec_name','spec_price'])
+                ->orderBy('id','asc')
+                ->get();
+            $value->{'spec_list'} = $spec_list;
+        }
+
+        return $sku;
+    }
+}

+ 13 - 0
routes/api/goods.php

@@ -0,0 +1,13 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+
+
+
+Route::group(['domain' => env('APP_USER_API'), 'prefix' => 'goods'],function (){
+    Route::match(['get','post'],'category', 'GoodsController@getShopCategoryList')->name('goods.category');//获取商家分类
+    Route::match(['get','post'],'goods', 'GoodsController@getShopGoodsList')->name('goods.goods');//获取商家分类商品
+    Route::match(['get','post'],'goodsSpec', 'GoodsController@getGoodsSpec')->name('goods.goodsSpec');//获取商品sku
+
+
+});