Cart.php 7.9 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. Cache::set('returnuri','/index/cart/shoppingCart.html');
  37. return redirect('/index/passport/login');
  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. $userId = Session::get('user_id');
  132. if (empty($userId)) {
  133. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  134. }
  135. $key = $this->request->param('key', null);
  136. if (!empty($key)) {
  137. Cache::set(\app\index\model\User::SHARE_PREFIX . $userId, $key, 3600 * 24 * 30);
  138. }
  139. $model = new CartModel;
  140. if (!$model->add($goodsId, $goodsSkuId, $goodsNum)) {
  141. return $this->renderError($model->getError() ?: 'Added to shopping cart failed');
  142. }
  143. // 购物车商品总数量
  144. $cartTotal = $model->getCartTotal();
  145. return $this->renderSuccess(compact('cartTotal'), 'Added to shopping cart successfully');
  146. }
  147. /**
  148. * 更新购物车商品数量
  149. * @param int $goodsId 商品ID
  150. * @param string $goodsSkuId 商品sku索引
  151. * @param int $goodsNum 商品数量
  152. * @return Json
  153. * @throws BaseException
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function update(int $goodsId, string $goodsSkuId, int $goodsNum): Json
  159. {
  160. $userId = Session::get('user_id');
  161. if (empty($userId)) {
  162. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  163. }
  164. if ($goodsNum <= 0) {
  165. return $this->renderError('Action Not Supported!');
  166. }
  167. $model = new CartModel;
  168. if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
  169. return $this->renderError($model->getError() ?: '更新失败');
  170. }
  171. // 购物车商品总数量
  172. //$cartTotal = $model->getCartTotal();
  173. $cartMoney = $this->getCartMoney();
  174. return $this->renderSuccess(compact('cartMoney'), 'Successful operation');
  175. }
  176. /**
  177. * 删除购物车中指定记录
  178. * @param array $cartIds 购物车ID集, 如果为空删除所有
  179. * @return Json
  180. * @throws BaseException
  181. */
  182. public function clear(array $cartIds = []): Json
  183. {
  184. $userId = Session::get('user_id');
  185. if (empty($userId)) {
  186. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  187. }
  188. $model = new CartModel;
  189. if (!$model->clear($cartIds)) {
  190. return $this->renderError($model->getError() ?: 'Failed operation');
  191. }
  192. // 购物车商品总数量
  193. //$cartTotal = $model->getCartTotal();
  194. $cartMoney = $this->getCartMoney();
  195. return $this->renderSuccess(compact('cartMoney'), 'Successful operation');
  196. }
  197. private function getCartMoney()
  198. {
  199. $service = new CartService;
  200. $list = $service->getList();
  201. $sum = '0.00';
  202. foreach ($list as $item) {
  203. $temp = bcmul(strval($item['goods_num']), strval($item['goods']['goods_price_min']), 2);
  204. $sum = bcadd($sum, $temp, 2);
  205. }
  206. return $sum;
  207. }
  208. }