From ba00bd66c7ac99e96b7a9bf69e8f3b2e5ec5a4b4 Mon Sep 17 00:00:00 2001 From: gongfuxiang Date: Tue, 21 Mar 2023 22:39:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E5=95=86=E5=93=81=E8=A7=84?= =?UTF-8?q?=E6=A0=BC=E5=92=8C=E7=94=A8=E6=88=B7=E8=B4=AD=E7=89=A9=E8=BD=A6?= =?UTF-8?q?=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Common.php | 8 +-- app/lang/en.php | 2 + app/lang/zh.php | 2 + app/service/GoodsCartService.php | 38 ++++----------- app/service/GoodsService.php | 83 ++++++++++++++++++++++++++++++-- app/service/IntegralService.php | 2 +- app/service/SearchService.php | 3 +- 7 files changed, 100 insertions(+), 38 deletions(-) diff --git a/app/api/controller/Common.php b/app/api/controller/Common.php index a07074114..1524bf6b7 100755 --- a/app/api/controller/Common.php +++ b/app/api/controller/Common.php @@ -89,9 +89,6 @@ class Common extends BaseController // 公共数据初始化 $this->CommonInit(); - - // token验证 - $this->TokenCheck(); } /** @@ -149,7 +146,10 @@ class Common extends BaseController if(empty($this->user)) { exit(json_encode(DataReturn(MyLang('login_failure_tips'), -400))); - } + } else { + // token验证 + $this->TokenCheck(); + } } /** diff --git a/app/lang/en.php b/app/lang/en.php index 94f47d467..09c8fb7ae 100644 --- a/app/lang/en.php +++ b/app/lang/en.php @@ -1320,6 +1320,8 @@ return [ 'goodscart' => [ 'save_stock_empty_tips' => 'Wrong purchase quantity', 'save_stock_update_data_empty_tips' => 'Please join the shopping cart first', + 'save_buy_max_error_tips' => 'Exceeding the product purchase limit', + 'save_inventory_not_enough_tips' => 'Insufficient inventory of goods', ], // 商品评论 'goodscomments' => [ diff --git a/app/lang/zh.php b/app/lang/zh.php index f84f3f0d5..fc71dd05a 100755 --- a/app/lang/zh.php +++ b/app/lang/zh.php @@ -1325,6 +1325,8 @@ return [ 'goodscart' => [ 'save_stock_empty_tips' => '购买数量有误', 'save_stock_update_data_empty_tips' => '请先加入购物车', + 'save_buy_max_error_tips' => '超过商品限购数量', + 'save_inventory_not_enough_tips' => '商品库存不足', ], // 商品评论 'goodscomments' => [ diff --git a/app/service/GoodsCartService.php b/app/service/GoodsCartService.php index 9f1278d1c..7ed6c7bbf 100644 --- a/app/service/GoodsCartService.php +++ b/app/service/GoodsCartService.php @@ -117,37 +117,11 @@ class GoodsCartService $v['buy_max_number'] = ($v['buy_max_number'] <= 0) ? $v['inventory']: $v['buy_max_number']; // 错误处理 - $v['is_error'] = 0; - $v['error_msg'] = ''; - if($v['is_delete_time'] != 0) - { - $v['is_error'] = 1; - $v['error_msg'] = MyLang('goods_already_nullify_title'); - } if(empty($v['error_msg']) && $v['is_invalid'] == 1) { $v['is_error'] = 1; $v['error_msg'] = MyLang('goods_already_invalid_title'); } - if(empty($v['error_msg']) && $v['is_shelves'] != 1) - { - $v['is_error'] = 1; - $v['error_msg'] = MyLang('goods_already_shelves_title'); - } - if(empty($v['error_msg']) && $v['inventory'] <= 0) - { - $v['is_error'] = 1; - $v['error_msg'] = MyLang('goods_no_inventory_title'); - } - if(empty($v['error_msg'])) - { - $ret = GoodsService::IsGoodsSiteTypeConsistent($v['goods_id'], $v['site_type']); - if($ret['code'] != 0) - { - $v['is_error'] = 1; - $v['error_msg'] = $ret['msg']; - } - } } } return DataReturn(MyLang('operate_success'), 0, $data); @@ -297,6 +271,8 @@ class GoodsCartService // 规格库存赋值 $goods['inventory'] = $goods_base['data']['spec_base']['inventory']; + // 规格最大限购 + $goods['buy_max_number'] = $goods_base['data']['spec_base']['buy_max_number']; } // 数量 @@ -305,7 +281,7 @@ class GoodsCartService // 库存 if($stock > $goods['inventory']) { - return DataReturn('库存不足', -1); + return DataReturn(MyLang('common_service.goodscart.save_inventory_not_enough_tips'), -1); } // 添加购物车 @@ -331,7 +307,12 @@ class GoodsCartService return DataReturn(MyLang('join_success'), 0, self::UserGoodsCartTotal($params)); } } else { - $data['upd_time'] = time(); + // 购物车数量是否已经到达最大库存数量、是否达到最大限购数量 + if($temp['stock'] >= $goods['inventory'] || ($goods['buy_max_number'] > 0 && $temp['stock'] >= $goods['buy_max_number'])) + { + return DataReturn(MyLang('common_service.goodscart.save_buy_max_error_tips'), -1); + } + // 加入数量、避免超过最大库存 $data['stock'] += $temp['stock']; if($data['stock'] > $goods['inventory']) { @@ -341,6 +322,7 @@ class GoodsCartService { $data['stock'] = $goods['buy_max_number']; } + $data['upd_time'] = time(); if(Db::name('Cart')->where($where)->update($data)) { return DataReturn(MyLang('join_success'), 0, self::UserGoodsCartTotal($params)); diff --git a/app/service/GoodsService.php b/app/service/GoodsService.php index 9620b8838..4b3d892dc 100755 --- a/app/service/GoodsService.php +++ b/app/service/GoodsService.php @@ -14,6 +14,7 @@ use think\facade\Db; use app\service\SystemService; use app\service\SystemBaseService; use app\service\ResourcesService; +use app\service\UserService; use app\service\BrandService; use app\service\RegionService; use app\service\WarehouseGoodsService; @@ -208,13 +209,15 @@ class GoodsService if(!empty($goods_ids)) { $res = self::GoodsList([ - 'where' => [ + 'where' => [ ['id', 'in', array_unique($goods_ids)], ['is_shelves', '=', 1], ], - 'm' => 0, - 'n' => 0, - 'field' => '*' + 'm' => 0, + 'n' => 0, + 'field' => '*', + 'is_spec' => true, + 'is_cart' => true, ]); $goods_list = empty($res['data']) ? [] : array_column($res['data'], null, 'id'); } @@ -334,6 +337,7 @@ class GoodsService $is_content_app = (isset($params['is_content_app']) && $params['is_content_app'] == true) ? true : false; $is_category = (isset($params['is_category']) && $params['is_category'] == true) ? true : false; $is_params = (isset($params['is_params']) && $params['is_params'] == true) ? true : false; + $is_cart = (isset($params['is_cart']) && $params['is_cart'] == true) ? true : false; $data_key_field = empty($params['data_key_field']) ? 'id' : $params['data_key_field']; $goods_ids = array_filter(array_column($data, $data_key_field)); @@ -364,6 +368,9 @@ class GoodsService // app数据 $app_group = $is_content_app ? self::GoodsAppData($goods_ids) : []; + // 获取商品购物车数量 + $user_cart = $is_cart ? self::UserCartGoodsCountData($goods_ids) : []; + // 开始处理数据 foreach($data as &$v) { @@ -512,6 +519,46 @@ class GoodsService $v['content_app'] = (!empty($app_group) && array_key_exists($data_id, $app_group)) ? $app_group[$data_id] : []; } + // 用户购物车总数 + if($is_cart && !empty($data_id)) + { + $v['user_cart_count'] = (!empty($user_cart) && array_key_exists($data_id, $user_cart)) ? $user_cart[$data_id] : 0; + } + + // 错误处理 + if(!isset($v['is_error']) || $v['is_error'] == 0) + { + $v['is_error'] = 0; + $v['error_msg'] = ''; + } + if($v['is_error'] == 0 && array_key_exists('is_delete_time', $v) && $v['is_delete_time'] != 0) + { + $v['is_error'] = 1; + $v['error_msg'] = MyLang('goods_already_nullify_title'); + } + // 是否上架 + if($v['is_error'] == 0 && array_key_exists('is_shelves', $v) && $v['is_shelves'] != 1) + { + $v['is_error'] = 1; + $v['error_msg'] = MyLang('goods_already_shelves_title'); + } + // 是否有库存 + if($v['is_error'] == 0 && array_key_exists('inventory', $v) && $v['inventory'] <= 0) + { + $v['is_error'] = 1; + $v['error_msg'] = MyLang('goods_no_inventory_title'); + } + // 没错误则判断类型是否一致 + if($v['is_error'] == 0 && array_key_exists('site_type', $v) && !empty($data_id)) + { + $ret = self::IsGoodsSiteTypeConsistent($data_id, $v['site_type']); + if($ret['code'] != 0) + { + $v['is_error'] = 1; + $v['error_msg'] = $ret['msg']; + } + } + // 价格字段 // 原价 // 价格 @@ -648,6 +695,34 @@ class GoodsService return Db::name('GoodsPhoto')->where(['goods_id'=>$goods_id, 'is_show'=>1])->order('sort asc')->select()->toArray(); } + /** + * 获取用户购物车商品总数 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2023-03-20 + * @desc description + * @param [array] $goods_ids [商品id] + */ + public static function UserCartGoodsCountData($goods_ids) + { + $result = []; + $user = UserService::LoginUserInfo(); + if(!empty($user)) + { + $where = [ + ['goods_id', 'in', $goods_ids], + ['user_id', '=', $user['id']], + ]; + $result = Db::name('Cart')->where($where)->field('SUM(stock) AS count, goods_id')->group('goods_id')->select()->toArray(); + if(!empty($result)) + { + $result = array_column($result, 'count', 'goods_id'); + } + } + return $result; + } + /** * 获取商品手机详情 * @author Devil diff --git a/app/service/IntegralService.php b/app/service/IntegralService.php index 073203410..5c91e33a2 100755 --- a/app/service/IntegralService.php +++ b/app/service/IntegralService.php @@ -52,7 +52,7 @@ class IntegralService $log_id = Db::name('UserIntegralLog')->insertGetId($data); if($log_id > 0) { - $lang = MyLang('common_config.integral.add_message_data'); + $lang = MyLang('common_service.integral.add_message_data'); $type_msg = MyLang('common_integral_log_type_list')[$type]['name']; $detail = $msg.$lang['title'].$type_msg.$operation_integral; MessageService::MessageAdd($user_id, $lang['desc'], $detail, $lang['title'], $log_id); diff --git a/app/service/SearchService.php b/app/service/SearchService.php index 590f53fa2..0f24e2409 100755 --- a/app/service/SearchService.php +++ b/app/service/SearchService.php @@ -183,7 +183,8 @@ class SearchService })->group('g.id')->order($order_by)->limit($result['page_start'], $result['page_size'])->select()->toArray(); // 数据处理 - $params['is_spec'] = 1; + $params['is_spec'] = true; + $params['is_cart'] = true; $goods = GoodsService::GoodsDataHandle($data, $params); // 返回数据