Cart.php 9.0 KB

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