Cart.php 7.6 KB

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