GoodsController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. return $this->apiResponseSuccess('成功',$list);
  29. }
  30. /**
  31. * 获取分类下商品
  32. */
  33. function getShopGoodsList()
  34. {
  35. $m_id = $this->getMemberId();//用户id
  36. $commonServer = CommonServer::creatServer();
  37. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  38. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  39. $category = GoodsCategory::where('is_del',0)
  40. ->where('shop_id',$shop_id)
  41. ->select(['id as type','name'])
  42. ->orderBy('sort','desc')
  43. ->get();
  44. foreach ($category as $value){
  45. $list = Goods::where('is_del',0)
  46. ->where('status',1)
  47. ->where('shop_id',$shop_id)
  48. ->where('category_id',$value['type'])
  49. ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','pack_price','spec_type','line_price','sales'])
  50. ->orderBy('sort','desc')
  51. ->get();
  52. foreach ($list as $goods){
  53. $goods->{'num'} = 0;//前端需要
  54. }
  55. $value->{'list'} = $list;
  56. }
  57. return $this->apiResponseSuccess('成功',$category);
  58. }
  59. /**
  60. * 获取商品sku
  61. */
  62. function getGoodsSpec()
  63. {
  64. $m_id = $this->getMemberId();//用户id
  65. $commonServer = CommonServer::creatServer();
  66. $goods_id = $commonServer->filtrationStr('goods_id');//商品id
  67. if (empty($goods_id)) return $this->apiResponseError('请选择商品');
  68. $goods = Goods::where('is_del',0)
  69. ->where('status',1)
  70. ->where('id',$goods_id)
  71. ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','pack_price','spec_type','line_price','sales'])
  72. ->first();
  73. if (empty($goods)){
  74. return $this->apiResponseError('商品不存在');
  75. }
  76. if ($goods->{'spec_type'} == 2){
  77. $sku = GoodsSpecServer::creatServer()->getGoodsSku($goods_id);
  78. }else{
  79. $sku = [];
  80. }
  81. $data['goods'] = $goods;
  82. $data['sku'] = $sku;
  83. return $this->apiResponseSuccess('成功',$data);
  84. }
  85. /**
  86. * 搜索商家商品
  87. */
  88. function getSearchGoodsList()
  89. {
  90. $m_id = $this->getMemberId();//用户id
  91. $commonServer = CommonServer::creatServer();
  92. $search = $commonServer->filtrationStr('search');//搜索
  93. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  94. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  95. if (empty($search)) return $this->apiResponseSuccess('请输入搜索内容');
  96. //记录搜索
  97. GoodsServer::creatServer()->setSearch($m_id,$shop_id, $search);
  98. //查询该关键词下的商品
  99. $list = Goods::where('is_del',0)
  100. ->where('status',1)
  101. ->where('shop_id',$shop_id)
  102. ->where('goods_name','like',"%$search%")
  103. ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','pack_price','spec_type','line_price','sales'])
  104. ->orderBy('sort','desc')
  105. ->paginate(10);
  106. return $this->apiResponseSuccess('获取成功', [
  107. 'items' => $list->items(),
  108. 'total' => $list->total(),
  109. 'limit' => 10,
  110. ]);
  111. }
  112. /**
  113. * 获取搜索内容
  114. */
  115. function getSearchList()
  116. {
  117. $m_id = $this->getMemberId();//用户id
  118. $commonServer = CommonServer::creatServer();
  119. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  120. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  121. $data = GoodsServer::creatServer()->getSearch($m_id,$shop_id);
  122. return $this->apiResponseSuccess('获取数据成功', $data);
  123. }
  124. /**
  125. * 清除搜索记录
  126. */
  127. function delSearch()
  128. {
  129. $m_id = $this->getMemberId();//用户id
  130. $commonServer = CommonServer::creatServer();
  131. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  132. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  133. $res = GoodsSearch::where('m_id', $m_id)->where('shop_id',$shop_id)->delete();
  134. if (!$res){
  135. return $this->apiResponseError('清除失败');
  136. }
  137. return $this->apiResponseSuccess('清除成功');
  138. }
  139. }