Cart.php 7.3 KB

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