GoodsController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Services\Api\GoodsService;
  5. /**
  6. * 商品管理
  7. * @package App\Http\Controllers\Api
  8. */
  9. class GoodsController extends webApp
  10. {
  11. /**
  12. * 列表
  13. * @return array
  14. */
  15. public function index()
  16. {
  17. $params =request()->post();
  18. $pageSize = request()->post('pageSize', 12);
  19. $datas = GoodsService::make()->getDataList($params, $pageSize);
  20. return message(1010, true, $datas);
  21. }
  22. /**
  23. * 购买商品列表
  24. * @return array
  25. */
  26. public function buyGoods()
  27. {
  28. $params =request()->post();
  29. $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
  30. $couponId = isset($params['coupon_id']) && $params['coupon_id'] ? $params['coupon_id'] : 0;
  31. $ids = $goods ? array_column($goods, 'id') : [];
  32. if($datas = GoodsService::make()->getOrderGoods($ids, $goods, $this->userId,'',0,$couponId)){
  33. return message(1010, true, $datas);
  34. }else{
  35. $error = GoodsService::make()->getError();
  36. return message($error, false,'',$error==1042? 403 : -1);
  37. }
  38. }
  39. /**
  40. * 分类
  41. * @return array
  42. */
  43. public function categorys()
  44. {
  45. $datas = GoodsService::make()->getCategoryList();
  46. return message(1010, true, $datas);
  47. }
  48. /**
  49. * 详情
  50. */
  51. public function info()
  52. {
  53. $params = request()->all();
  54. $id = isset($params['id'])? intval($params['id']) : 0;
  55. if(empty($id)){
  56. return message(1036, false);
  57. }
  58. if($info = GoodsService::make()->getInfo($id, $this->userId)){
  59. return message(1010, true, $info);
  60. }else{
  61. return message('商品已下架', false);
  62. }
  63. }
  64. /**
  65. * 收藏
  66. */
  67. public function collect()
  68. {
  69. $params = request()->all();
  70. $id = isset($params['id']) ? intval($params['id']) : 0;
  71. if (empty($id)) {
  72. return message(1036, false);
  73. }
  74. try {
  75. if ($result = GoodsService::make()->collect($this->userId, $id)) {
  76. return message(GoodsService::make()->getError(), true, $result);
  77. } else {
  78. return message(GoodsService::make()->getError(), false);
  79. }
  80. } catch (\Exception $exception) {
  81. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  82. return showJson(1046, false, $error);
  83. }
  84. }
  85. /*
  86. * 商品类型
  87. */
  88. public function types()
  89. {
  90. $types = config('platform.goodsTypes1');
  91. return showJson(1010, true, $types);
  92. }
  93. }