Cart.php 7.8 KB

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