GoodsController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\MpService;
  6. /**
  7. * 商品管理
  8. * @package App\Http\Controllers\Api
  9. */
  10. class GoodsController extends webApp
  11. {
  12. /**
  13. * 列表
  14. * @return array
  15. */
  16. public function index()
  17. {
  18. $params =request()->post();
  19. $pageSize = request()->post('pageSize', 12);
  20. $datas = GoodsService::make()->getDataList($params, $pageSize);
  21. return message(1010, true, $datas);
  22. }
  23. /**
  24. * 购买商品列表
  25. * @return array
  26. */
  27. public function buyGoods()
  28. {
  29. $params =request()->post();
  30. $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
  31. $couponId = isset($params['coupon_id']) && $params['coupon_id'] ? $params['coupon_id'] : 0;
  32. $ids = $goods ? array_column($goods, 'id') : [];
  33. if($datas = GoodsService::make()->getOrderGoods($ids, $goods, $this->userId,'',0,$couponId)){
  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. /*
  87. * 商品类型
  88. */
  89. public function types()
  90. {
  91. $types = config('platform.goodsTypes1');
  92. return showJson(1010, true, $types);
  93. }
  94. /**
  95. * 分享链接
  96. */
  97. public function getLink()
  98. {
  99. $params = request()->all();
  100. $id = isset($params['id'])? intval($params['id']) : 0;
  101. if(empty($id)){
  102. return showJson(1036, false);
  103. }
  104. if($link = MpService::make()->getMiniShareLink('pagesSub/pages/goods/detail?id='.$id,'给您分享了一个心动商品')){
  105. return showJson(1010, true, $link);
  106. }else{
  107. return showJson(MpService::make()->getError(), false);
  108. }
  109. }
  110. }