GoodsController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\ApiController;
  4. use App\Models\Goods;
  5. use App\Models\GoodsCategory;
  6. use App\Models\GoodsSearch;
  7. use App\Servers\CommonServer;
  8. use App\Servers\GoodsServer;
  9. use App\Servers\GoodsSpecServer;
  10. /**
  11. * 外卖商品
  12. */
  13. class GoodsController extends ApiController
  14. {
  15. /**
  16. * 获取商家分类
  17. */
  18. function getShopCategoryList()
  19. {
  20. $commonServer = CommonServer::creatServer();
  21. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  22. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  23. $list = GoodsCategory::where('is_del',0)
  24. ->where('shop_id',$shop_id)
  25. ->select(['id as category_id','name'])
  26. ->orderBy('sort','desc')
  27. ->get();
  28. $data = [];
  29. foreach ($list as $value){
  30. $goods = Goods::where('is_del',0)
  31. ->where('status',1)
  32. ->where('shop_id',$shop_id)
  33. ->where('category_id',$value['category_id'])->count();
  34. if ($goods > 0){
  35. $data[] = $value;
  36. }
  37. }
  38. return $this->apiResponseSuccess('成功',$data);
  39. }
  40. /**
  41. * 获取分类下商品
  42. */
  43. function getShopGoodsList()
  44. {
  45. $m_id = $this->getMemberId();//用户id
  46. $commonServer = CommonServer::creatServer();
  47. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  48. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  49. $category = GoodsCategory::where('is_del',0)
  50. ->where('shop_id',$shop_id)
  51. ->select(['id as type','name'])
  52. ->orderBy('sort','desc')
  53. ->get();
  54. $data = [];
  55. foreach ($category as $value){
  56. $list = Goods::where('is_del',0)
  57. ->where('status',1)
  58. ->where('shop_id',$shop_id)
  59. ->where('category_id',$value['type'])
  60. ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','pack_price','spec_type','line_price','sales'])
  61. ->orderBy('sort','desc')
  62. ->get();
  63. foreach ($list as $goods){
  64. $goods->{'num'} = 0;//前端需要
  65. }
  66. $value->{'list'} = $list;
  67. if (count($list) > 0){
  68. $data[] = $value;
  69. }
  70. }
  71. return $this->apiResponseSuccess('成功',$data);
  72. }
  73. /**
  74. * 获取商品sku
  75. */
  76. function getGoodsSpec()
  77. {
  78. $m_id = $this->getMemberId();//用户id
  79. $commonServer = CommonServer::creatServer();
  80. $goods_id = $commonServer->filtrationStr('goods_id');//商品id
  81. if (empty($goods_id)) return $this->apiResponseError('请选择商品');
  82. $goods = Goods::where('is_del',0)
  83. ->where('status',1)
  84. ->where('id',$goods_id)
  85. ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','pack_price','spec_type','line_price','sales'])
  86. ->first();
  87. if (empty($goods)){
  88. return $this->apiResponseError('商品不存在');
  89. }
  90. if ($goods->{'spec_type'} == 2){
  91. $sku = GoodsSpecServer::creatServer()->getGoodsSku($goods_id);
  92. }else{
  93. $sku = [];
  94. }
  95. $data['goods'] = $goods;
  96. $data['sku'] = $sku;
  97. return $this->apiResponseSuccess('成功',$data);
  98. }
  99. /**
  100. * 搜索商家商品
  101. */
  102. function getSearchGoodsList()
  103. {
  104. $m_id = $this->getMemberId();//用户id
  105. $commonServer = CommonServer::creatServer();
  106. $search = $commonServer->filtrationStr('search');//搜索
  107. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  108. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  109. if (empty($search)) return $this->apiResponseSuccess('请输入搜索内容');
  110. //记录搜索
  111. GoodsServer::creatServer()->setSearch($m_id,$shop_id, $search);
  112. //查询该关键词下的商品
  113. $list = Goods::where('is_del',0)
  114. ->where('status',1)
  115. ->where('shop_id',$shop_id)
  116. ->where('goods_name','like',"%$search%")
  117. ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','pack_price','spec_type','line_price','sales'])
  118. ->orderBy('sort','desc')
  119. ->paginate(10);
  120. return $this->apiResponseSuccess('获取成功', [
  121. 'items' => $list->items(),
  122. 'total' => $list->total(),
  123. 'limit' => 10,
  124. ]);
  125. }
  126. /**
  127. * 获取搜索内容
  128. */
  129. function getSearchList()
  130. {
  131. $m_id = $this->getMemberId();//用户id
  132. $commonServer = CommonServer::creatServer();
  133. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  134. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  135. $data = GoodsServer::creatServer()->getSearch($m_id,$shop_id);
  136. return $this->apiResponseSuccess('获取数据成功', $data);
  137. }
  138. /**
  139. * 清除搜索记录
  140. */
  141. function delSearch()
  142. {
  143. $m_id = $this->getMemberId();//用户id
  144. $commonServer = CommonServer::creatServer();
  145. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  146. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  147. $res = GoodsSearch::where('m_id', $m_id)->where('shop_id',$shop_id)->delete();
  148. if (!$res){
  149. return $this->apiResponseError('清除失败');
  150. }
  151. return $this->apiResponseSuccess('清除成功');
  152. }
  153. }