GoodsController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. */
  68. public function collect()
  69. {
  70. $params = request()->all();
  71. $id = isset($params['id']) ? intval($params['id']) : 0;
  72. if (empty($id)) {
  73. return message(1036, false);
  74. }
  75. try {
  76. if ($result = GoodsService::make()->collect($this->userId, $id)) {
  77. return message(GoodsService::make()->getError(), true, $result);
  78. } else {
  79. return message(GoodsService::make()->getError(), false);
  80. }
  81. } catch (\Exception $exception) {
  82. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  83. return showJson(1046, false, $error);
  84. }
  85. }
  86. }