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.

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