GoodsController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. }