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.

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