Cart.php 8.5 KB

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