GoodsController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Http\Validator\GoodsValidator;
  5. use App\Services\Api\GoodsService;
  6. use App\Services\Api\GoodsCategoryService;
  7. use App\Services\RedisService;
  8. /**
  9. * 商品/服务管理
  10. * Class CartController
  11. * @package App\Http\Controllers\Api
  12. */
  13. class GoodsController extends webApp
  14. {
  15. /**
  16. * 列表
  17. * @return array
  18. */
  19. public function index()
  20. {
  21. try {
  22. $params = request()->post();
  23. $pageSize = request()->post('pageSize', 0);
  24. $datas = GoodsService::make()->getDataList($params, $pageSize,'', $this->userId);
  25. return message(1010, true, $datas);
  26. } catch (\Exception $exception){
  27. RedisService::set("caches:request:error_goods_index", ['trace'=>$exception->getTrace()], 7200);
  28. return message(1018, false, ['error'=>env('APP_DEBUG')? $exception->getMessage() : '']);
  29. }
  30. }
  31. /**
  32. * 列表
  33. * @return array
  34. */
  35. public function list()
  36. {
  37. try {
  38. $params = request()->post();
  39. $pageSize = request()->post('pageSize', 0);
  40. $datas = GoodsService::make()->getDataList($params, $pageSize,'lev_a.*,lev_c.name as merch_name', $this->userId);
  41. return message(1010, true, $datas);
  42. } catch (\Exception $exception){
  43. RedisService::set("caches:request:error_goods_index", ['trace'=>$exception->getTrace()], 7200);
  44. return message(1018, false, ['error'=>env('APP_DEBUG')? $exception->getTrace() : '']);
  45. }
  46. }
  47. /**
  48. * 分类
  49. * @return array
  50. */
  51. public function category()
  52. {
  53. $params = request()->post();
  54. $pageSize = request()->post('pageSize', 99);
  55. $datas = GoodsCategoryService::make()->getDataList($params, $pageSize);
  56. return message(1010, true, $datas);
  57. }
  58. /**
  59. * 详情
  60. * @return array
  61. */
  62. public function info()
  63. {
  64. $id = request()->post('id', 0);
  65. $info = GoodsService::make()->getInfo($id, $this->userId);
  66. return message(1010, true, $info);
  67. }
  68. /**
  69. * 推荐
  70. * @return array
  71. */
  72. public function recommend()
  73. {
  74. try {
  75. $params = request()->post();
  76. $pageSize = request()->post('pageSize', 0);
  77. $lng = isset($params['lng'])? $params['lng'] : '';
  78. $lat = isset($params['lat'])? $params['lat'] : '';
  79. $params = [
  80. 'lat'=> $lat? $lat : (isset($this->userInfo['lat'])? $this->userInfo['lat'] : ''),
  81. 'lng'=> $lng? $lng : (isset($this->userInfo['lng'])? $this->userInfo['lng'] : ''),
  82. 'sort_type'=> 1,
  83. 'is_recommend'=> 1,
  84. 'type'=> isset($params['type'])? intval($params['type']) : 2,
  85. 'status'=> 2,
  86. ];
  87. $datas = GoodsService::make()->getDataList($params, $pageSize);
  88. return message(1010, true, $datas);
  89. } catch (\Exception $exception){
  90. RedisService::set("caches:request:error_goods_recommend", ['trace'=>$exception->getTrace()], 7200);
  91. return message(1018, false, ['error'=>env('APP_DEBUG')? $exception->getMessage() : '']);
  92. }
  93. }
  94. /**
  95. * 发布
  96. * @param GoodsValidator $validator
  97. * @return array
  98. */
  99. public function submit(GoodsValidator $validator)
  100. {
  101. $params = request()->all();
  102. $params = $validator->check($params, 'submit');
  103. if (!is_array($params)) {
  104. return message($params, false);
  105. }
  106. if(!$result = GoodsService::make()->submit($this->userId, $params)){
  107. return message(GoodsService::make()->getError(), false);
  108. }else{
  109. return message(GoodsService::make()->getError(), true, $result);
  110. }
  111. }
  112. /**
  113. * 状态/上下架
  114. * @return array|mixed
  115. */
  116. public function status()
  117. {
  118. if(!$result = GoodsService::make()->status()){
  119. return message(GoodsService::make()->getError(), false);
  120. }else{
  121. return message(GoodsService::make()->getError(), true, $result);
  122. }
  123. }
  124. /**
  125. * 删除
  126. * @return array|mixed
  127. */
  128. public function delete()
  129. {
  130. return GoodsService::make()->delete();
  131. }
  132. }