Cart.php 7.7 KB

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