CartController.php 3.4 KB

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