Cart.php 7.3 KB

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