CartController.php 3.3 KB

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