123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\controller;
- use app\api\model\ActivityDiscount;
- use app\api\model\Cart as CartModel;
- use app\api\model\GoodsSku as GoodsSkuModel;
- use app\api\model\Goods as GoodsModel;
- use app\api\model\UserCoupon as UserCouponModel;
- use \app\api\model\card\UserRiceCard as UserRiceCardModel;
- use app\api\service\Cart as CartService;
- use app\api\service\User as UserService;
- use app\common\exception\BaseException;
- use app\common\library\helper;
- use app\common\enum\goods\Status as GoodsStatusEnum;
- use think\facade\Cache;
- /**
- * 购物车管理
- * Class Cart
- * @package app\api\controller
- */
- class Cart extends Controller
- {
- /**
- * 购物车商品列表
- * @return array|\think\response\Json
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function list()
- {
- // 购物车商品列表
- $service = new CartService;
- $list = $service->getList();
- $sum_total_price = 0;
- $activity_discount_total_price = 0;
- // 整理购物车商品列表
- $orderGoodsIds = [];
- $goodList = [];
- foreach ($list as $item) {
- $item['err_status'] = 0;
- // if(empty($item['goods']['skuInfo'])){
- // $item['err_status'] = 3;
- // $item['selected'] = 0;
- // }else{
- if($item['goods_num']>$item['goods']['skuInfo']['stock_num']|| $item['goods_num']==0){ //库存不足
- $item['err_status'] = 1;
- $item['selected'] = 0;
- }
- // }
- if($item['goods']['status']==GoodsStatusEnum::OFF_SALE){ //下架商品
- $item['err_status'] = 2;
- $item['selected'] = 0;
- }
- if($item['selected'] && $item['err_status']==0){//用户选中的商品勾选后的总金额
- $item['total_price'] = $item['goods']['skuInfo']['goods_price']*$item['goods_num'];
- (new GoodsModel)->getActivityDiscountDetail($item);
- $goodList[] = $item;
- $sum_total_price += $item['total_price'];
- $activity_discount_total_price += $item['activity_discount_total_price'];
- $orderGoodsIds[] = $item['goods_id'];
- }
- }
- if(count($list)){
- $list = array_values($list->toArray());
- }
- // 购物车商品类型数量
- $cartTotal = (new CartModel)->getCartGoodsTotal();
- $sum_total_price = helper::bcadd($sum_total_price,0,2);
- $activity_discount_total_price = helper::bcadd($activity_discount_total_price,0,2);
- // 当前用户ID
- $user = UserService::getCurrentLoginUser(true);
- $user_coupon = [];
- $total_price = $sum_total_price-$activity_discount_total_price;
- // dd($orderGoodsIds);
- if($total_price>0){
- // 当前用户可用的优惠券列表(筛选了除外商品)
- $user_coupon = UserCouponModel::getUserCouponList($user['user_id'], $goodList, $orderGoodsIds);
- $user_coupon = array_values($user_coupon);
- }
- // //我的可用米卡列表
- // $user_rice_card = UserRiceCardModel::getUserRiceCardList($user['user_id']);
- return $this->renderSuccess(compact('cartTotal','sum_total_price', 'activity_discount_total_price', 'list', 'user_coupon'));
- }
- /**
- * 购物车商品总数量
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function total() {
- $model = new CartModel;
- $cartTotal = $model->getCartGoodsTotal();
- return $this->renderSuccess(compact('cartTotal'));
- }
- /**
- * 加入购物车
- * @param int $goodsId 商品id
- * @param string $goodsSkuId 商品sku索引
- * @param int $goodsNum 商品数量
- * @param int $staffUserId 链接分享者ID
- * @return array|\think\response\Json
- * @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, int $staffUserId = 0)
- {
- if(!is_numeric($goodsSkuId)){
- //自动选择库存足够的商品
- $goodsSkuId = GoodsSkuModel::where([['goods_id','=',$goodsId],['stock_num','>=',$goodsNum]])->order('id','asc')->value('goods_sku_id')??'';
- if(!is_numeric($goodsSkuId)){
- return $this->renderError( '商品不存在');
- }
- }
- $model = new CartModel;
- if (!$model->add($goodsId, $goodsSkuId, $goodsNum, $staffUserId)) {
- return $this->renderError($model->getError() ?: '加入购物车失败');
- }
- // 购物车商品总数量
- $cartTotal = $model->getCartGoodsTotal();
- return $this->renderSuccess(compact('cartTotal'), '加入购物车成功');
- }
- /**
- * 更新购物车商品数量
- * @param int $goodsId 商品id
- * @param string $goodsSkuId 商品sku索引
- * @param int $goodsNum 商品数量
- * @return array|\think\response\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)
- {
- $model = new CartModel;
- if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
- return $this->renderError($model->getError() ?: '更新失败');
- }
- // 购物车商品总数量
- $cartTotal = $model->getCartGoodsTotal();
- return $this->renderSuccess(compact('cartTotal'), '更新成功');
- }
- /**
- * 删除购物车中指定记录
- * @param array $cartIds 购物车ID集, 如果为空删除所有
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function delete(array $cartIds = [])
- {
- $model = new CartModel;
- if (!$model->clear($cartIds)) {
- return $this->renderError($model->getError() ?: '操作失败');
- }
- // 购物车商品总数量
- $cartTotal = $model->getCartGoodsTotal();
- return $this->renderSuccess(compact('cartTotal'), '操作成功');
- }
- /**
- * 勾选购物车中指定记录
- * @param array $cartIds 购物车ID集, 如果为空删除取消全部勾选
- * @param int $selected 选中状态 0未选中 1选中
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function goodsSelect(int $selected, array $cartIds = []){
- $model = new CartModel;
- if (!$model->updateSelected($cartIds,$selected)) {
- return $this->renderError($model->getError() ?: '操作失败');
- }
- return $this->renderSuccess();
- }
- /**
- * 更改购物车商品规格
- * @param int $cartId
- * @param int $goodsId
- * @param string $goodsSkuId
- * @return array
- * @throws BaseException
- * @author: zjwhust
- * @Time: 2021/9/26 17:50
- */
- public function updSku(int $cartId, int $goodsId, string $goodsSkuId){
- $model = new CartModel;
- if (!$model->updSku($cartId, $goodsId, $goodsSkuId)) {
- return $this->renderError($model->getError() ?: '操作失败');
- }
- return $this->renderSuccess();
- }
- }
|