StoreCartController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v2\order;
  12. use app\Request;
  13. use app\services\order\StoreCartServices;
  14. class StoreCartController
  15. {
  16. protected $services;
  17. public function __construct(StoreCartServices $services)
  18. {
  19. $this->services = $services;
  20. }
  21. /**
  22. * 购物车重选
  23. * @param Request $request
  24. * @return mixed
  25. */
  26. public function resetCart(Request $request)
  27. {
  28. [$id, $unique, $num, $product_id] = $request->postMore([
  29. ['id', 0],
  30. ['unique', ''],
  31. ['num', 1],
  32. ['product_id', 0]
  33. ], true);
  34. $this->services->resetCart($id, $request->uid(), $product_id, $unique, $num);
  35. return app('json')->successful('修改成功');
  36. }
  37. /**
  38. * 获取用户购物车
  39. * @param Request $request
  40. * @return mixed
  41. */
  42. public function getCartList(Request $request)
  43. {
  44. $uid = (int)$request->uid();
  45. [$store_id] = $request->postMore([
  46. ['store_id',0]
  47. ],true);
  48. $data = $this->services->getCartList(['uid' => $uid, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0, 'type' => 0, 'store_id' => 0], 0, 0, ['productInfo', 'attrInfo']);
  49. if($store_id) $this->services->setItem('store_id', (int)$store_id);
  50. [$data, $valid, $invalid] = $this->services->handleCartList($uid, $data, [], -1);
  51. $this->services->reset();
  52. return app('json')->successful($valid);
  53. }
  54. /**
  55. * 首页加入购物车
  56. * @param Request $request
  57. * @return mixed
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function setCartNum(Request $request)
  63. {
  64. [$product_id, $num, $unique, $type] = $request->postMore([
  65. ['product_id', 0],
  66. ['num', 0],
  67. ['unique', ''],
  68. ['type', -1]
  69. ], true);
  70. /** @var StoreCartServices $cartService */
  71. $cartService = app()->make(StoreCartServices::class);
  72. if (!$product_id || !is_numeric($product_id)) return app('json')->fail('参数错误');
  73. if (!(int)$num) return app('json')->fail('请提交加入购物车商品数量');
  74. $res = $cartService->setCartNum((int)$request->uid(), (int)$product_id, (int)$num, $unique, (int)$type);
  75. if ($res) return app('json')->successful('修改成功');
  76. return app('json')->fail('修改失败');
  77. }
  78. }