GoodsController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $ids = $goods ? array_column($goods, 'id') : [];
  31. if($datas = GoodsService::make()->getOrderGoods($ids, $goods, $this->userId)){
  32. return message(1010, true, $datas);
  33. }else{
  34. $error = GoodsService::make()->getError();
  35. return message($error, false,'',$error==1042? 403 : -1);
  36. }
  37. }
  38. /**
  39. * 分类
  40. * @return array
  41. */
  42. public function categorys()
  43. {
  44. $datas = GoodsService::make()->getCategoryList();
  45. return message(1010, true, $datas);
  46. }
  47. /**
  48. * 详情
  49. */
  50. public function info()
  51. {
  52. $params = request()->all();
  53. $id = isset($params['id'])? intval($params['id']) : 0;
  54. if(empty($id)){
  55. return message(1036, false);
  56. }
  57. if($info = GoodsService::make()->getInfo($id, $this->userId)){
  58. return message(1010, true, $info);
  59. }else{
  60. return message('商品已下架', false);
  61. }
  62. }
  63. /**
  64. * 收藏
  65. */
  66. public function collect()
  67. {
  68. $params = request()->all();
  69. $id = isset($params['id']) ? intval($params['id']) : 0;
  70. if (empty($id)) {
  71. return message(1036, false);
  72. }
  73. try {
  74. if ($result = GoodsService::make()->collect($this->userId, $id)) {
  75. return message(GoodsService::make()->getError(), true, $result);
  76. } else {
  77. return message(GoodsService::make()->getError(), false);
  78. }
  79. } catch (\Exception $exception) {
  80. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  81. return showJson(1046, false, $error);
  82. }
  83. }
  84. /*
  85. * 商品类型
  86. */
  87. public function types()
  88. {
  89. $types = config('platform.goodsTypes1');
  90. return showJson(1010, true, $types);
  91. }
  92. }