CartController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Services\Api\CartService;
  5. use App\Services\ConfigService;
  6. /**
  7. * 购物车管理
  8. * @package App\Http\Controllers\Api
  9. */
  10. class CartController 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. $params['user_id'] = $this->userId;
  21. $datas = CartService::make()->getDataList($params, $pageSize);
  22. return message(1010, true, $datas);
  23. }
  24. /**
  25. * 添加购物车
  26. */
  27. public function add()
  28. {
  29. $params = request()->all();
  30. $id = isset($params['id']) ? intval($params['id']) : 0;
  31. $skuId = isset($params['sku_id']) ? intval($params['sku_id']) : 0;
  32. $num = isset($params['num']) && $params['num']? intval($params['num']) : 1;
  33. if (empty($id)) {
  34. return message(1036, false);
  35. }
  36. try {
  37. if ($result = CartService::make()->add($this->userId, $id, $skuId, $num)) {
  38. return message(CartService::make()->getError(), true, $result);
  39. } else {
  40. return message(CartService::make()->getError(), false);
  41. }
  42. } catch (\Exception $exception) {
  43. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  44. return showJson(1046, false, $error);
  45. }
  46. }
  47. /**
  48. * 更新购物车
  49. */
  50. public function update()
  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. try {
  58. if ($result = CartService::make()->update($this->userId, $id)) {
  59. return message(CartService::make()->getError(), true, $result);
  60. } else {
  61. return message(CartService::make()->getError(), false);
  62. }
  63. } catch (\Exception $exception) {
  64. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  65. return showJson(1046, false, $error);
  66. }
  67. }
  68. /**
  69. * 删除购物车
  70. */
  71. public function delete()
  72. {
  73. $params = request()->all();
  74. $ids = isset($params['ids']) && $params['ids']? $params['ids'] : [];
  75. try {
  76. if ($result = CartService::make()->deleteCart($this->userId, $ids)) {
  77. return message(CartService::make()->getError(), true, $result);
  78. } else {
  79. return message(CartService::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 count()
  90. {
  91. try {
  92. if ($result = CartService::make()->getCount($this->userId)) {
  93. return message(1010, true, [
  94. 'count'=>$result,
  95. 'social_open'=> ConfigService::make()->getConfigByCode('social_open', 0),
  96. ]);
  97. } else {
  98. return message(1009, false);
  99. }
  100. } catch (\Exception $exception) {
  101. $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
  102. return showJson(1046, false, $error);
  103. }
  104. }
  105. }