123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\index\controller;
- use app\index\model\Cart as CartModel;
- use app\index\model\Region;
- use app\index\model\Region as RegionModel;
- use app\index\model\UserAddress;
- use app\index\service\Cart as CartService;
- use app\common\exception\BaseException;
- use think\facade\Cache;
- use think\facade\Session;
- use think\response\Json;
- /**
- * 购物车管理
- * Class Cart
- * @package app\api\controller
- */
- class Cart extends Controller
- {
- /**
- * @return |\think\response\View
- */
- public function shoppingCart()
- {
- $userId = Session::get('user_id');
- if (empty($userId)) {
- return view('passport/logIn');
- }
- // 购物车商品列表
- $service = new CartService;
- $list = $service->getList();
- // 购物车商品总数量
- $cartTotal = (new CartModel)->getCartTotal();
- $user = $this->getCurrentLoginUser();
- $cartMoney = '0.00';
- foreach ($list as $item) {
- $temp = bcmul(strval($item['goods_num']), $item['goods']['goods_price_min'], 2);
- $cartMoney = bcadd($cartMoney, $temp, 2);
- }
- $payPoints = intval(bcmul($cartMoney, '100', 0));//订单所需积分
- $payByPoints = false;
- if ($payPoints > 0 && intval($user['points']) >= $payPoints) {
- $payByPoints = true;
- }
- $res = ['list' => $list, 'cartTotal' => $cartTotal, 'cartMoney' => $cartMoney, 'points' => $user['points'], 'payByPoints' => $payByPoints];
- $res['addressId'] = 0;
- $res['full'] = '';
- $res['zipCode'] = '';
- $res['name'] = '';
- $res['lastName'] = '';
- $res['fullName'] = '';
- $res['phone'] = '';
- $res['email'] = '';
- $res['regionId'] = 0;
- $res['city'] = '';
- $res['detail'] = '';
- $regList = RegionModel::getCacheAll();
- $states50 = array_slice($regList, 2);
- $res['states50'] = $states50;
- if (!empty($user['address_id'])) {
- $addr = UserAddress::detailInner($user['address_id']);
- if ($addr) {
- $res['addressId'] = $addr['address_id'];
- $res['full'] = $addr['detail'] . ',' . $addr['city'] . ',' . $addr['region']['region'] . '(' . $addr['zip_code'] . '),US';
- $res['zipCode'] = $addr['zip_code'];
- $res['name'] = $addr['name'];
- $res['lastName'] = $addr['last_name'];
- $res['fullName'] = $addr['name'] . ' ' . $addr['last_name'];
- $res['phone'] = $addr['phone'];
- $res['email'] = $addr['email'];
- $res['regionId'] = $addr['region_id'];
- $res['city'] = $addr['city'];
- $res['detail'] = $addr['detail'];
- }
- }
- return view('shoppingCart', $res);
- }
- /**
- * 购物车商品列表
- * @return Json
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \cores\exception\BaseException
- */
- public function list(): Json
- {
- // 购物车商品列表
- $service = new CartService;
- $list = $service->getList();
- // 购物车商品总数量
- $cartTotal = (new CartModel)->getCartTotal();
- return $this->renderSuccess(compact('cartTotal', 'list'));
- }
- /**
- * 购物车商品总数量
- * @return Json
- * @throws BaseException
- */
- public function total(): Json
- {
- $model = new CartModel;
- $cartTotal = $model->getCartTotal();
- return $this->renderSuccess(compact('cartTotal'));
- }
- /**
- * 加入购物车
- * @param int $goodsId 商品ID
- * @param string $goodsSkuId 商品sku索引
- * @param int $goodsNum 商品数量
- * @return Json |\think\response\View
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function add(int $goodsId, string $goodsSkuId, int $goodsNum): Json
- {
- $userId = Session::get('user_id');
- if (empty($userId)) {
- return $this->renderJson(config('status.not_logged'), 'Log in please!');
- }
- $key = $this->request->param('key', null);
- if (!empty($key)) {
- Cache::set(\app\index\model\User::SHARE_PREFIX . $userId, $key, 3600 * 24 * 30);
- }
- $model = new CartModel;
- if (!$model->add($goodsId, $goodsSkuId, $goodsNum)) {
- return $this->renderError($model->getError() ?: 'Added to shopping cart failed');
- }
- // 购物车商品总数量
- $cartTotal = $model->getCartTotal();
- return $this->renderSuccess(compact('cartTotal'), 'Added to shopping cart successfully');
- }
- /**
- * 更新购物车商品数量
- * @param int $goodsId 商品ID
- * @param string $goodsSkuId 商品sku索引
- * @param int $goodsNum 商品数量
- * @return Json
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function update(int $goodsId, string $goodsSkuId, int $goodsNum): Json
- {
- $userId = Session::get('user_id');
- if (empty($userId)) {
- return $this->renderJson(config('status.not_logged'), 'Log in please!');
- }
- if ($goodsNum <= 0) {
- return $this->renderError('Action Not Supported!');
- }
- $model = new CartModel;
- if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
- return $this->renderError($model->getError() ?: '更新失败');
- }
- // 购物车商品总数量
- //$cartTotal = $model->getCartTotal();
- $cartMoney = $this->getCartMoney();
- return $this->renderSuccess(compact('cartMoney'), 'Successful operation');
- }
- /**
- * 删除购物车中指定记录
- * @param array $cartIds 购物车ID集, 如果为空删除所有
- * @return Json
- * @throws BaseException
- */
- public function clear(array $cartIds = []): Json
- {
- $userId = Session::get('user_id');
- if (empty($userId)) {
- return $this->renderJson(config('status.not_logged'), 'Log in please!');
- }
- $model = new CartModel;
- if (!$model->clear($cartIds)) {
- return $this->renderError($model->getError() ?: 'Failed operation');
- }
- // 购物车商品总数量
- //$cartTotal = $model->getCartTotal();
- $cartMoney = $this->getCartMoney();
- return $this->renderSuccess(compact('cartMoney'), 'Successful operation');
- }
- private function getCartMoney()
- {
- $service = new CartService;
- $list = $service->getList();
- $sum = '0.00';
- foreach ($list as $item) {
- $temp = bcmul(strval($item['goods_num']), strval($item['goods']['goods_price_min']), 2);
- $sum = bcadd($sum, $temp, 2);
- }
- return $sum;
- }
- }
|