Cart.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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;
  15. use app\index\model\Region as RegionModel;
  16. use app\index\model\UserAddress;
  17. use app\index\service\Cart as CartService;
  18. use app\common\exception\BaseException;
  19. use think\facade\Cache;
  20. use think\facade\Session;
  21. use think\response\Json;
  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. Session::set('returnuri', '/index/cart/shoppingCart.html');
  37. return redirect('/index/passport/login.html');
  38. //return view('passport/logIn');
  39. }
  40. // 购物车商品列表
  41. $service = new CartService;
  42. $list = $service->getList();
  43. // 购物车商品总数量
  44. $cartTotal = (new CartModel)->getCartTotal();
  45. $user = $this->getCurrentLoginUser();
  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. $payPoints = intval(bcmul($cartMoney, '100', 0));//订单所需积分
  52. $payByPoints = false;
  53. if ($payPoints > 0 && intval($user['points']) >= $payPoints) {
  54. $payByPoints = true;
  55. }
  56. $res = ['list' => $list, 'cartTotal' => $cartTotal, 'cartMoney' => $cartMoney, 'points' => $user['points'], 'payByPoints' => $payByPoints];
  57. $res['addressId'] = 0;
  58. $res['full'] = '';
  59. $res['zipCode'] = '';
  60. $res['name'] = '';
  61. $res['lastName'] = '';
  62. $res['fullName'] = '';
  63. $res['phone'] = '';
  64. $res['email'] = '';
  65. $res['regionId'] = 0;
  66. $res['city'] = '';
  67. $res['detail'] = '';
  68. $regList = RegionModel::getCacheAll();
  69. $states50 = array_slice($regList, 2);
  70. $res['states50'] = $states50;
  71. if (!empty($user['address_id'])) {
  72. $addr = UserAddress::detailInner($user['address_id']);
  73. if ($addr) {
  74. $res['addressId'] = $addr['address_id'];
  75. $res['full'] = $addr['detail'] . ',' . $addr['city'] . ',' . $addr['region']['region'] . '(' . $addr['zip_code'] . '),US';
  76. $res['zipCode'] = $addr['zip_code'];
  77. $res['name'] = $addr['name'];
  78. $res['lastName'] = $addr['last_name'];
  79. $res['fullName'] = $addr['name'] . ' ' . $addr['last_name'];
  80. $res['phone'] = $addr['phone'];
  81. $res['email'] = $addr['email'];
  82. $res['regionId'] = $addr['region_id'];
  83. $res['city'] = $addr['city'];
  84. $res['detail'] = $addr['detail'];
  85. }
  86. }
  87. return view('shoppingCart', $res);
  88. }
  89. /**
  90. * 购物车商品列表
  91. * @return Json
  92. * @throws BaseException
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \cores\exception\BaseException
  97. */
  98. public function list(): Json
  99. {
  100. // 购物车商品列表
  101. $service = new CartService;
  102. $list = $service->getList();
  103. // 购物车商品总数量
  104. $cartTotal = (new CartModel)->getCartTotal();
  105. return $this->renderSuccess(compact('cartTotal', 'list'));
  106. }
  107. /**
  108. * 购物车商品总数量
  109. * @return Json
  110. * @throws BaseException
  111. */
  112. public function total(): Json
  113. {
  114. $model = new CartModel;
  115. $cartTotal = $model->getCartTotal();
  116. return $this->renderSuccess(compact('cartTotal'));
  117. }
  118. /**
  119. * 加入购物车
  120. * @param int $goodsId 商品ID
  121. * @param string $goodsSkuId 商品sku索引
  122. * @param int $goodsNum 商品数量
  123. * @return Json |\think\response\View
  124. * @throws BaseException
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function add(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  130. {
  131. $key = $this->request->param('key', '');
  132. $userId = Session::get('user_id');
  133. if (empty($userId)) {
  134. Session::set('returnuri', '/index/index/productDetails.html?goodsId=' . $goodsId . '&key=' . $key, 300);
  135. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  136. }
  137. if (!empty($key)) {
  138. Cache::set(\app\index\model\User::SHARE_PREFIX . $userId, $key, 3600 * 24 * 30);
  139. }
  140. $model = new CartModel;
  141. if (!$model->add($goodsId, $goodsSkuId, $goodsNum)) {
  142. return $this->renderError($model->getError() ?: 'Added to shopping cart failed');
  143. }
  144. // 购物车商品总数量
  145. $cartTotal = $model->getCartTotal();
  146. return $this->renderSuccess(compact('cartTotal'), 'Added to shopping cart successfully');
  147. }
  148. /**
  149. * 更新购物车商品数量
  150. * @param int $goodsId 商品ID
  151. * @param string $goodsSkuId 商品sku索引
  152. * @param int $goodsNum 商品数量
  153. * @return Json
  154. * @throws BaseException
  155. * @throws \think\db\exception\DataNotFoundException
  156. * @throws \think\db\exception\DbException
  157. * @throws \think\db\exception\ModelNotFoundException
  158. */
  159. public function update(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  160. {
  161. $userId = Session::get('user_id');
  162. if (empty($userId)) {
  163. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  164. }
  165. if ($goodsNum <= 0) {
  166. return $this->renderError('Action Not Supported!');
  167. }
  168. $model = new CartModel;
  169. if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
  170. return $this->renderError($model->getError() ?: '更新失败');
  171. }
  172. // 购物车商品总数量
  173. //$cartTotal = $model->getCartTotal();
  174. $cartMoney = $this->getCartMoney();
  175. return $this->renderSuccess(compact('cartMoney'), 'Successful operation');
  176. }
  177. /**
  178. * 删除购物车中指定记录
  179. * @param array $cartIds 购物车ID集, 如果为空删除所有
  180. * @return Json
  181. * @throws BaseException
  182. */
  183. public function clear(array $cartIds = []): Json
  184. {
  185. $userId = Session::get('user_id');
  186. if (empty($userId)) {
  187. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  188. }
  189. $model = new CartModel;
  190. if (!$model->clear($cartIds)) {
  191. return $this->renderError($model->getError() ?: 'Failed operation');
  192. }
  193. // 购物车商品总数量
  194. //$cartTotal = $model->getCartTotal();
  195. $cartMoney = $this->getCartMoney();
  196. return $this->renderSuccess(compact('cartMoney'), 'Successful operation');
  197. }
  198. private function getCartMoney()
  199. {
  200. $service = new CartService;
  201. $list = $service->getList();
  202. $sum = '0.00';
  203. foreach ($list as $item) {
  204. $temp = bcmul(strval($item['goods_num']), strval($item['goods']['goods_price_min']), 2);
  205. $sum = bcadd($sum, $temp, 2);
  206. }
  207. return $sum;
  208. }
  209. }