Cart.php 8.7 KB

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