GoodsController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Services\Api\GoodsService;
  5. use App\Services\Common\AdService;
  6. use App\Services\RedisService;
  7. /**
  8. * 商品管理
  9. * @package App\Http\Controllers\Api
  10. */
  11. class GoodsController extends webApp
  12. {
  13. /**
  14. * 列表
  15. * @return array
  16. */
  17. public function index()
  18. {
  19. $params =request()->post();
  20. $pageSize = request()->post('pageSize', 12);
  21. $datas = GoodsService::make()->getDataList($params, $pageSize);
  22. return message(1010, true, $datas);
  23. }
  24. /**
  25. * 购买商品列表
  26. * @return array
  27. */
  28. public function buyGoods()
  29. {
  30. $params =request()->post();
  31. $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
  32. $ids = $goods ? array_column($goods, 'id') : [];
  33. if($datas = GoodsService::make()->getOrderGoods($ids, $goods, $this->userId)){
  34. return message(1010, true, $datas);
  35. }else{
  36. $error = GoodsService::make()->getError();
  37. return message($error, false,'',$error==1042? 403 : -1);
  38. }
  39. }
  40. /**
  41. * 分类
  42. * @return array
  43. */
  44. public function categorys()
  45. {
  46. $datas = GoodsService::make()->getCategoryList();
  47. return message(1010, true, $datas);
  48. }
  49. /**
  50. * 详情
  51. */
  52. public function info()
  53. {
  54. $params = request()->all();
  55. $id = isset($params['id'])? intval($params['id']) : 0;
  56. if(empty($id)){
  57. return message(1036, false);
  58. }
  59. if($info = GoodsService::make()->getInfo($id, $this->userId)){
  60. return message(1010, true, $info);
  61. }else{
  62. return message('商品已下架', false);
  63. }
  64. }
  65. /**
  66. * 创业大礼包
  67. * @return array
  68. */
  69. public function data()
  70. {
  71. $areaId = $this->areaId;
  72. $buyType = $this->buyType;
  73. if($areaId<=0){
  74. $cacheInfo = RedisService::get("caches:index:area_".get_client_ip());
  75. $areaId = isset($cacheInfo['area_id'])?$cacheInfo['area_id'] : 1;
  76. $buyType = isset($cacheInfo['buy_type'])?$cacheInfo['buy_type'] : 1;
  77. }
  78. try {
  79. $data = [
  80. // 轮播
  81. 'banners' => AdService::make()->getListByPosition(1),
  82. 'goods' => GoodsService::make()->getListByType(2, $buyType, $this->userId),
  83. 'areaId'=> $areaId,
  84. 'buyType'=> $buyType,
  85. ];
  86. return showJson(1010, true, $data);
  87. } catch (\Exception $exception) {
  88. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  89. return showJson(1046, false, $error);
  90. }
  91. }
  92. /**
  93. * 收藏
  94. */
  95. public function collect()
  96. {
  97. $params = request()->all();
  98. $id = isset($params['id']) ? intval($params['id']) : 0;
  99. if (empty($id)) {
  100. return message(1036, false);
  101. }
  102. try {
  103. if ($result = GoodsService::make()->collect($this->userId, $id)) {
  104. return message(GoodsService::make()->getError(), true, $result);
  105. } else {
  106. return message(GoodsService::make()->getError(), false);
  107. }
  108. } catch (\Exception $exception) {
  109. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  110. return showJson(1046, false, $error);
  111. }
  112. }
  113. }