You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Controller\BaseController;
  5. use App\Exception\ErrorCodeException;
  6. use App\Model\v3\Category;
  7. use App\Model\v3\GoodsCategory;
  8. use App\Request\v3\GoodsEditorRequest;
  9. use App\Service\v3\Interfaces\CategoryServiceInterface;
  10. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  11. use App\Service\v3\Implementations\GoodsActivityService;
  12. use App\Service\v3\Interfaces\CollectStoreServiceInterface;
  13. use Hyperf\Di\Annotation\Inject;
  14. use App\Service\v3\Interfaces\GoodsServiceInterface;
  15. use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
  16. use App\Constants\v3\Goods;
  17. use App\Request\v3\GoodsRequest;
  18. class GoodsController extends BaseController
  19. {
  20. /**
  21. * @Inject
  22. * @var GoodsServiceInterface
  23. */
  24. protected $goodsService;
  25. /**
  26. * @Inject
  27. * @var GoodsActivityServiceInterface
  28. */
  29. protected $goodsActivityService;
  30. /**
  31. * @Inject
  32. * @var CollectStoreServiceInterface
  33. */
  34. protected $collectService;
  35. /**
  36. * @Inject
  37. * @var ShopCartServiceInterface
  38. */
  39. protected $shopCartService;
  40. /**
  41. * @Inject
  42. * @var CategoryServiceInterface
  43. */
  44. protected $categoryService;
  45. public function detail(GoodsRequest $request)
  46. {
  47. $params = $this->request->all();
  48. $activity = $this->request->input('activity',1);
  49. $goodsId = $this->request->input('goods_id',0);
  50. $marketId = $this->request->input('market_id',0);
  51. //判断是普通商品还是特价商品
  52. if($activity == Goods::IS_ACTIVITY){
  53. $res['detail'] = $this->goodsActivityService->detail($goodsId);
  54. $res['banner'] = $this->goodsActivityService->getBanner($goodsId);
  55. }else{
  56. $res['detail'] = $this->goodsService->detail($goodsId);
  57. $res['banner'] = $this->goodsService->getBanner($goodsId);
  58. }
  59. //搜索不到商品
  60. if(is_null($res['detail']['id'])){
  61. throw new ErrorCodeException(ErrorCode::GOODS_ON_SALE_NO);
  62. }
  63. //如果没有banner数据,使用商品cover图
  64. if(count($res['banner']) == 0){
  65. // $res['banner'] = [$res['detail']['cover_img']];
  66. $res['banner'] = [[
  67. 'banner_url' => $res['detail']['cover_img'],
  68. 'goods_id' => $goodsId,
  69. 'type' => 1,
  70. 'path' => $res['detail']['cover_img'],
  71. 'created_at' => '',
  72. 'updated_at' => '',
  73. 'deleted_at' => '',
  74. 'sort' => '',
  75. ]];
  76. }
  77. //如果存在用户ID则请求 购物车和收藏店铺信息
  78. if(isset($params['user_id'])) {
  79. $res['shopcart']['count'] = $this->shopCartService->countGoods($params['user_id'],$marketId);
  80. $res['shopcart']['total'] = $this->shopCartService->getTotal($params['user_id'],$marketId);
  81. $res['detail']->store->is_collected = (bool)$this->collectService->check($params['user_id'], $res['detail']->store->id);
  82. }else{
  83. $res['shopcart'] = '';
  84. $res['detail']->store->is_collected = '';
  85. }
  86. return $this->success($res);
  87. }
  88. public function update(GoodsEditorRequest $request)
  89. {
  90. $data['id'] = $this->request->input('id',0);
  91. $data['market_id'] = $this->request->input('market_id',0);
  92. $data['store_id'] = $this->request->input('store_id',0);
  93. $data['name'] = $this->request->input('name','');
  94. $data['category_id'] = $this->request->input('category_id',0);
  95. $data['goods_category_id'] = $this->request->input('goods_category_id',0);
  96. $data['goods_unit'] = $this->request->input('goods_unit','');
  97. $data['price'] = $this->request->input('price',0);
  98. $data['original_price'] = $this->request->input('original_price',0);
  99. $data['inventory'] = $this->request->input('inventory',0);
  100. $data['restrict_num'] = $this->request->input('restrict_num',0);
  101. $data['start_num'] = $this->request->input('start_num',0);
  102. $data['spec'] = $this->request->input('spec');
  103. $data['tags'] = $this->request->input('tags');
  104. $data['remark'] = $this->request->input('remark','');
  105. $data['on_sale'] = $this->request->input('on_sale',0);
  106. $data['is_infinite'] = $this->request->input('is_infinite',0);
  107. $data['user_id'] = $this->request->input('user_id',0);
  108. $data['cover_img'] = $this->request->input('cover_img',0);
  109. $banners = $this->request->input('banner',0);
  110. $res = $this->goodsService->update($data);
  111. if(!empty($banners)){
  112. $banners = explode(',',$banners);
  113. foreach ($banners as $banner){
  114. $this->goodsService->bannerCreate($data['id'],$banner,1);
  115. }
  116. }
  117. return $this->success($res);
  118. }
  119. public function info()
  120. {
  121. $goodsId = $this->request->input('goods_id',0);
  122. $goods = $this->goodsService->info($goodsId);
  123. $goods['secend'] = Category::find($goods['category_id']);
  124. $goods['first'] = Category::find($goods['secend']['parent_id']);
  125. if(!empty($goods['goods_category_id'])){
  126. $goods['third'] = GoodsCategory::find($goods['goods_category_id']);
  127. }else{
  128. $goods['third'] = null;
  129. }
  130. $first = $this->categoryService->getByParentId(0);
  131. $secend = $this->categoryService->getByParentId($goods['first']['id']);
  132. $third = $this->categoryService->getThird($goods['category_id']);
  133. $tags = $this->goodsService->getTags();
  134. return $this->success([
  135. 'goods' => $goods,
  136. 'first' => $first,
  137. 'secend' => $secend,
  138. 'third' => $third,
  139. 'tags' => $tags
  140. ]);
  141. }
  142. public function create(GoodsEditorRequest $request)
  143. {
  144. $data['id'] = $this->request->input('id',0);
  145. $data['market_id'] = $this->request->input('market_id',0);
  146. $data['store_id'] = $this->request->input('store_id',0);
  147. $data['name'] = $this->request->input('name','');
  148. $data['category_id'] = $this->request->input('category_id',0);
  149. $data['goods_category_id'] = $this->request->input('goods_category_id',0);
  150. $data['goods_unit'] = $this->request->input('goods_unit','');
  151. $data['price'] = $this->request->input('price',0);
  152. $data['original_price'] = $this->request->input('original_price',0);
  153. $data['inventory'] = $this->request->input('inventory',0);
  154. $data['restrict_num'] = $this->request->input('restrict_num',0);
  155. $data['start_num'] = $this->request->input('start_num',0);
  156. $data['spec'] = $this->request->input('spec');
  157. $data['tags'] = $this->request->input('tags');
  158. $data['remark'] = $this->request->input('remark','');
  159. $data['on_sale'] = $this->request->input('on_sale',0);
  160. $data['is_infinite'] = $this->request->input('is_infinite',0);
  161. $data['user_id'] = $this->request->input('user_id',0);
  162. $data['cover_img'] = $this->request->input('cover_img',0);
  163. $banners = $this->request->input('banner',0);
  164. $res = $this->goodsService->create($data);
  165. if(!empty($banners)){
  166. $banners = explode(',',$banners);
  167. foreach ($banners as $banner){
  168. $this->goodsService->bannerCreate($res['id'],$banner,1);
  169. }
  170. }
  171. return $this->success($res);
  172. }
  173. public function getTags()
  174. {
  175. $tags = $this->goodsService->getTags();
  176. return $this->success(['tags' => $tags]);
  177. }
  178. public function updateOnSale()
  179. {
  180. $goodsId = $this->request->input('id',0);
  181. return $this->success($this->goodsService->updateOnSale($goodsId));
  182. }
  183. public function bannerDelete()
  184. {
  185. $bannerId = $this->request->input('banner_id',0);
  186. return $this->success($this->goodsService->bannerDelete($bannerId));
  187. }
  188. }