GoodsController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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','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. $spec_type = Goods::where('id',$goods_id)->where('is_del',0)->value('spec_type');
  69. if ($spec_type == 2){
  70. $sku = GoodsSpecServer::creatServer()->getGoodsSku($goods_id);
  71. }else{
  72. $sku = [];
  73. }
  74. return $this->apiResponseSuccess('成功',$sku);
  75. }
  76. /**
  77. * 搜索商家商品
  78. */
  79. function getSearchGoodsList()
  80. {
  81. $m_id = $this->getMemberId();//用户id
  82. $commonServer = CommonServer::creatServer();
  83. $search = $commonServer->filtrationStr('search');//搜索
  84. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  85. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  86. if (empty($search)) return $this->apiResponseSuccess('请输入搜索内容');
  87. //记录搜索
  88. GoodsServer::creatServer()->setSearch($m_id,$shop_id, $search);
  89. //查询该关键词下的商品
  90. $list = Goods::where('is_del',0)
  91. ->where('status',1)
  92. ->where('shop_id',$shop_id)
  93. ->where('goods_name','like',"%$search%")
  94. ->select(['id as goods_id','category_id','goods_name','goods_desc','goods_image','price','spec_type','line_price','sales'])
  95. ->orderBy('sort','desc')
  96. ->paginate(10);
  97. return $this->apiResponseSuccess('获取成功', [
  98. 'items' => $list->items(),
  99. 'total' => $list->total(),
  100. 'limit' => 10,
  101. ]);
  102. }
  103. /**
  104. * 获取搜索内容
  105. */
  106. function getSearchList()
  107. {
  108. $m_id = $this->getMemberId();//用户id
  109. $commonServer = CommonServer::creatServer();
  110. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  111. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  112. $data = GoodsServer::creatServer()->getSearch($m_id,$shop_id);
  113. return $this->apiResponseSuccess('获取数据成功', $data);
  114. }
  115. /**
  116. * 清除搜索记录
  117. */
  118. function delSearch()
  119. {
  120. $m_id = $this->getMemberId();//用户id
  121. $commonServer = CommonServer::creatServer();
  122. $shop_id = $commonServer->filtrationStr('shop_id');//商品id
  123. if (empty($shop_id)) return $this->apiResponseError('请选择商家');
  124. $res = GoodsSearch::where('m_id', $m_id)->where('shop_id',$shop_id)->delete();
  125. if (!$res){
  126. return $this->apiResponseError('清除失败');
  127. }
  128. return $this->apiResponseSuccess('清除成功');
  129. }
  130. }