Cart.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\index\controller;
  13. use app\index\model\Cart as CartModel;
  14. use app\index\model\Region as RegionModel;
  15. use app\index\model\UserAddress;
  16. use app\index\service\Cart as CartService;
  17. use app\common\exception\BaseException;
  18. use think\facade\Cache;
  19. use think\facade\Session;
  20. use think\response\Json;
  21. use think\View;
  22. /**
  23. * 购物车管理
  24. * Class Cart
  25. * @package app\api\controller
  26. */
  27. class Cart extends Controller
  28. {
  29. /**
  30. * @return |\think\response\View
  31. */
  32. public function shoppingCart()
  33. {
  34. $userId = Session::get('user_id');
  35. if (empty($userId)) {
  36. return view('passport/logIn');
  37. }
  38. // 购物车商品列表
  39. $service = new CartService;
  40. $list = $service->getList();
  41. // 购物车商品总数量
  42. $cartTotal = (new CartModel)->getCartTotal();
  43. //dd($list->toArray(),$cartTotal);
  44. $user = $this->getCurrentLoginUser();
  45. //dd($user->toArray());
  46. $cartMoney = '0.00';
  47. foreach ($list as $item) {
  48. $temp = bcmul(strval($item['goods_num']), $item['goods']['goods_price_min'], 2);
  49. $cartMoney = bcadd($cartMoney, $temp, 2);
  50. }
  51. $res = ['list' => $list, 'cartTotal' => $cartTotal, 'cartMoney' => $cartMoney, 'points' => $user['points']];
  52. $res['address_id'] = 0;
  53. $res['full'] = '';
  54. $res['zip_code'] = '';
  55. $res['name'] = '';
  56. $res['last_name'] = '';
  57. $res['phone'] = '';
  58. $res['email'] = '';
  59. $regList = RegionModel::getCacheAll();
  60. $states50 = array_slice($regList,2);
  61. $res['states50'] = $states50;
  62. if (!empty($user['address_id'])) {
  63. $addr = UserAddress::detail($user['address_id']);
  64. if ($addr){
  65. $res['addressId'] = $addr['address_id'];
  66. $res['full'] = $addr['detail'] . ',' . $addr['region']['region'] . '(' . $addr['zip_code'] . '),US';
  67. $res['zipCode'] = $addr['zip_code'];
  68. $res['name'] = $addr['name'];
  69. $res['lastName'] = $addr['last_name'];
  70. $res['fullName'] = $addr['name'].' '.$addr['last_name'];
  71. $res['phone'] = $addr['phone'];
  72. $res['email'] = $addr['email'];
  73. }
  74. }
  75. return view('shoppingCart', $res);
  76. }
  77. /**
  78. * 购物车商品列表
  79. * @return Json
  80. * @throws BaseException
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \cores\exception\BaseException
  85. */
  86. public function list(): Json
  87. {
  88. // 购物车商品列表
  89. $service = new CartService;
  90. $list = $service->getList();
  91. // 购物车商品总数量
  92. $cartTotal = (new CartModel)->getCartTotal();
  93. return $this->renderSuccess(compact('cartTotal', 'list'));
  94. }
  95. /**
  96. * 购物车商品总数量
  97. * @return Json
  98. * @throws BaseException
  99. */
  100. public function total(): Json
  101. {
  102. $model = new CartModel;
  103. $cartTotal = $model->getCartTotal();
  104. return $this->renderSuccess(compact('cartTotal'));
  105. }
  106. /**
  107. * 加入购物车
  108. * @param int $goodsId 商品ID
  109. * @param string $goodsSkuId 商品sku索引
  110. * @param int $goodsNum 商品数量
  111. * @return Json |\think\response\View
  112. * @throws BaseException
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function add(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  118. {
  119. $userId = Session::get('user_id');
  120. if (empty($userId)) {
  121. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  122. }
  123. $key = $this->request->param('key', null);
  124. if (!empty($key)) {
  125. Cache::set(\app\index\model\User::SHARE_PREFIX . $userId, $key, 3600 * 24 * 30);
  126. }
  127. $model = new CartModel;
  128. if (!$model->add($goodsId, $goodsSkuId, $goodsNum)) {
  129. return $this->renderError($model->getError() ?: '加入购物车失败');
  130. }
  131. // 购物车商品总数量
  132. $cartTotal = $model->getCartTotal();
  133. return $this->renderSuccess(compact('cartTotal'), '加入购物车成功');
  134. }
  135. /**
  136. * 更新购物车商品数量
  137. * @param int $goodsId 商品ID
  138. * @param string $goodsSkuId 商品sku索引
  139. * @param int $goodsNum 商品数量
  140. * @return Json
  141. * @throws BaseException
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. public function update(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  147. {
  148. $userId = Session::get('user_id');
  149. if (empty($userId)) {
  150. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  151. }
  152. if ($goodsNum <= 0) {
  153. return $this->renderError('Action Not Supported!');
  154. }
  155. $model = new CartModel;
  156. if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
  157. return $this->renderError($model->getError() ?: '更新失败');
  158. }
  159. // 购物车商品总数量
  160. //$cartTotal = $model->getCartTotal();
  161. $cartMoney = $this->getCartMoney();
  162. return $this->renderSuccess(compact('cartMoney'), '更新成功');
  163. }
  164. /**
  165. * 删除购物车中指定记录
  166. * @param array $cartIds 购物车ID集, 如果为空删除所有
  167. * @return Json
  168. * @throws BaseException
  169. */
  170. public function clear(array $cartIds = []): Json
  171. {
  172. $userId = Session::get('user_id');
  173. if (empty($userId)) {
  174. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  175. }
  176. $model = new CartModel;
  177. if (!$model->clear($cartIds)) {
  178. return $this->renderError($model->getError() ?: '操作失败');
  179. }
  180. // 购物车商品总数量
  181. //$cartTotal = $model->getCartTotal();
  182. $cartMoney = $this->getCartMoney();
  183. return $this->renderSuccess(compact('cartMoney'), '操作成功');
  184. }
  185. private function getCartMoney()
  186. {
  187. $service = new CartService;
  188. $list = $service->getList();
  189. $sum = '0.00';
  190. foreach ($list as $item) {
  191. $temp = bcmul(strval($item['goods_num']), strval($item['goods']['goods_price_min']), 2);
  192. $sum = bcadd($sum, $temp, 2);
  193. }
  194. return $sum;
  195. }
  196. }