From 6b49f0eadec60649ee972e693dac14261745e456 Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Fri, 25 Sep 2020 10:48:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/v3/CategoryController.php | 17 +++++++++ app/Controller/v3/GoodsController.php | 28 +++++++++++++- app/Model/v3/Goods.php | 20 +++++++++- app/Request/v3/GoodsEditorRequest.php | 17 ++++++++- .../v3/Implementations/CategoryService.php | 17 +++++++++ .../v3/Implementations/GoodsService.php | 37 ++++++++++++------- .../Interfaces/CategoryServiceInterface.php | 2 + config/routes.php | 3 ++ 8 files changed, 122 insertions(+), 19 deletions(-) diff --git a/app/Controller/v3/CategoryController.php b/app/Controller/v3/CategoryController.php index 4994287..c80dd34 100644 --- a/app/Controller/v3/CategoryController.php +++ b/app/Controller/v3/CategoryController.php @@ -60,4 +60,21 @@ class CategoryController extends BaseController return $this->success(['category' => $category]); } + + public function first() + { + return $this->success($this->categoryService->getByParentId(0)); + } + + public function secend() + { + $parentId = $this->request->input('parent_id'); + return $this->success($this->categoryService->getByParentId($parentId)); + } + + public function third() + { + $categoryId = $this->request->input('category_id'); + return $this->success($this->categoryService->getByParentId($categoryId)); + } } \ No newline at end of file diff --git a/app/Controller/v3/GoodsController.php b/app/Controller/v3/GoodsController.php index 2fb56f2..c4fdb99 100644 --- a/app/Controller/v3/GoodsController.php +++ b/app/Controller/v3/GoodsController.php @@ -3,7 +3,10 @@ namespace App\Controller\v3; use App\Controller\BaseController; +use App\Model\v3\Category; +use App\Model\v3\GoodsCategory; use App\Request\v3\GoodsEditorRequest; +use App\Service\v3\Interfaces\CategoryServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface; use App\Service\v3\Implementations\GoodsActivityService; use App\Service\v3\Interfaces\CollectStoreServiceInterface; @@ -38,6 +41,12 @@ class GoodsController extends BaseController */ protected $shopCartService; + /** + * @Inject + * @var CategoryServiceInterface + */ + protected $categoryService; + public function detail(GoodsRequest $request) { $params = $this->request->all(); @@ -88,7 +97,22 @@ class GoodsController extends BaseController public function info() { $goodsId = $this->request->input('goods_id',0); - $res = $this->goodsService->info($goodsId); - return $this->success($res); + $goods = $this->goodsService->info($goodsId); + $goods['secend'] = Category::find($goods['category_id']); + $goods['first'] = Category::find($goods['secend']['parent_id']); + if(!empty($goods['goods_category_id'])){ + $goods['third'] = GoodsCategory::find($goods['goods_category_id']); + }else{ + $goods['third'] = null; + } + $first = $this->categoryService->getByParentId(0); + $secend = $this->categoryService->getByParentId($goods['category_id']); + $third = $this->categoryService->getThird($goods['category_id']); + return $this->success([ + 'goods' => $goods, + 'first' => $first, + 'secend' => $secend, + 'third' => $third + ]); } } \ No newline at end of file diff --git a/app/Model/v3/Goods.php b/app/Model/v3/Goods.php index 6ed70f0..9ff23d0 100644 --- a/app/Model/v3/Goods.php +++ b/app/Model/v3/Goods.php @@ -45,7 +45,24 @@ class Goods extends Model * * @var array */ - protected $fillable = []; + protected $fillable = [ + 'id', + 'market_id', + 'store_id', + 'name', + 'category_id', + 'goods_unit', + 'price', + 'original_price', + 'inventory', + 'restrict_num', + 'start_num', + 'spec', + 'tags', + 'remark', + 'on_sale', + 'is_infinite' + ]; /** * The attributes that should be cast to native types. * @@ -145,5 +162,4 @@ class Goods extends Model { return $this->morphMany(ShoppingCart::class, 'goods'); } - } \ No newline at end of file diff --git a/app/Request/v3/GoodsEditorRequest.php b/app/Request/v3/GoodsEditorRequest.php index 40cabc4..09b30b6 100644 --- a/app/Request/v3/GoodsEditorRequest.php +++ b/app/Request/v3/GoodsEditorRequest.php @@ -15,7 +15,22 @@ class GoodsEditorRequest extends BaseFormRequest public function rules(): array { return [ - 'id' => 'required|nonempty|integer', + 'id' => 'required', + 'market_id' => 'required|nonempty|integer', + 'store_id' => 'required|nonempty|integer', + 'name' => 'required|nonempty', + 'category_id' => 'required|nonempty|integer', + 'goods_unit' => 'required|nonempty', + 'price' => 'required|nonempty', + 'original_price' => 'required|nonempty', + 'is_infinite' => 'required', + 'inventory' => 'required', + 'restrict_num' => 'required', + 'start_num' => 'required', + 'spec' => 'required|nonempty', + 'tags' => 'required|nonempty', + 'remark' => 'required|nonempty', + 'on_sale' => 'required', ]; } diff --git a/app/Service/v3/Implementations/CategoryService.php b/app/Service/v3/Implementations/CategoryService.php index 268c04a..608a465 100644 --- a/app/Service/v3/Implementations/CategoryService.php +++ b/app/Service/v3/Implementations/CategoryService.php @@ -6,6 +6,7 @@ use App\Constants\v3\OssThumbnail; use App\Constants\v3\SsdbKeys; use App\Model\v3\Category; use App\Model\v3\Goods; +use App\Model\v3\GoodsCategory; use App\Service\v3\Interfaces\AttachmentServiceInterface; use App\Service\v3\Interfaces\CategoryServiceInterface; use App\TaskWorker\SSDBTask; @@ -120,4 +121,20 @@ class CategoryService implements CategoryServiceInterface return $categories; } + + public function getByParentId($parentId) + { + $categories = Category::query() + ->where('parent_id',$parentId) + ->get(); + return $categories; + } + + public function getThird($categoryId) + { + $categories = GoodsCategory::query() + ->where('category_id',$categoryId) + ->get(); + return $categories; + } } \ No newline at end of file diff --git a/app/Service/v3/Implementations/GoodsService.php b/app/Service/v3/Implementations/GoodsService.php index 986f6e7..ff7804b 100644 --- a/app/Service/v3/Implementations/GoodsService.php +++ b/app/Service/v3/Implementations/GoodsService.php @@ -78,20 +78,29 @@ class GoodsService implements GoodsServiceInterface public function update($params) { - $goods = Goods::query()->withoutGlobalScope('normal')->where('id',$params['id'])->first(); - if (empty($goods)) { - throw new ErrorCodeException( ErrorCode::GOODS_NOT_EXISTS); - } - if(isset($params['name']) && !empty($params['name'])) { - $goods->name = $params['name']; - } - if(isset($params['price']) && !empty($params['price'])) { - $goods->price = $params['price']; - } - if(isset($params['on_sale'])) { - $goods->on_sale = $params['on_sale']; - } - return $goods->save(); + $goods = Goods::query()->where( + [ + 'id' => $params['id'], + 'market_id' => $params['market_id'], + 'store_id' => $params['store_id'] + ]) + ->update( + [ + 'name' => $params['name'], + 'category_id' => $params['market_id'], + 'goods_unit' => $params['goods_unit'], + 'price' => $params['price'], + 'original_price' => $params['original_price'], + 'inventory' => $params['inventory'], + 'restrict_num' => $params['restrict_num'], + 'start_num' => $params['start_num'], + //'spec' => $params['spec'], + //'tags' => $params['tags'], + 'remark' => $params['remark'], + 'on_sale' => $params['on_sale'], + 'is_infinite' => $params['is_infinite'], + ]); + return $goods; } public function info($goodsId) diff --git a/app/Service/v3/Interfaces/CategoryServiceInterface.php b/app/Service/v3/Interfaces/CategoryServiceInterface.php index d79acab..6c760eb 100644 --- a/app/Service/v3/Interfaces/CategoryServiceInterface.php +++ b/app/Service/v3/Interfaces/CategoryServiceInterface.php @@ -12,4 +12,6 @@ interface CategoryServiceInterface public function allForStore($storeId); public function allForAppletIndex(); public function allForStoreIncludeOff($storeId); + public function getByParentId($parentId); + public function getThird($categoryId); } \ No newline at end of file diff --git a/config/routes.php b/config/routes.php index f13e1bc..015a49a 100644 --- a/config/routes.php +++ b/config/routes.php @@ -157,6 +157,9 @@ Router::addGroup('/v3/', function () { Router::post('store/getList', 'App\Controller\v3\StoreController@getList'); Router::post('goods/info', 'App\Controller\v3\GoodsController@info'); Router::post('store/getCategory', 'App\Controller\v3\StoreController@getCategory'); + Router::post('category/first', 'App\Controller\v3\CategoryController@first'); + Router::post('category/secend', 'App\Controller\v3\CategoryController@secend'); + Router::post('category/third', 'App\Controller\v3\CategoryController@third'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]); // 微信支付回调