GoodsController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. return message(GoodsService::make()->getError(), false);
  35. }
  36. }
  37. /**
  38. * 分类
  39. * @return array
  40. */
  41. public function categorys()
  42. {
  43. $datas = GoodsService::make()->getCategoryList();
  44. return message(1010, true, $datas);
  45. }
  46. /**
  47. * 详情
  48. */
  49. public function info()
  50. {
  51. $params = request()->all();
  52. $id = isset($params['id'])? intval($params['id']) : 0;
  53. if(empty($id)){
  54. return message(1036, false);
  55. }
  56. if($info = GoodsService::make()->getInfo($id, $this->userId)){
  57. return message(1010, true, $info);
  58. }else{
  59. return message(1009, false);
  60. }
  61. }
  62. /**
  63. * 收藏
  64. */
  65. public function collect()
  66. {
  67. $params = request()->all();
  68. $id = isset($params['id']) ? intval($params['id']) : 0;
  69. if (empty($id)) {
  70. return message(1036, false);
  71. }
  72. try {
  73. if ($result = GoodsService::make()->collect($this->userId, $id)) {
  74. return message(GoodsService::make()->getError(), true, $result);
  75. } else {
  76. return message(GoodsService::make()->getError(), false);
  77. }
  78. } catch (\Exception $exception) {
  79. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  80. return showJson(1046, false, $error);
  81. }
  82. }
  83. }