适配商品规格和用户购物车数量

feat/task1-c-wallet
gongfuxiang 2023-03-21 22:39:13 +08:00
parent 008181c9a3
commit ba00bd66c7
7 changed files with 100 additions and 38 deletions

View File

@ -89,9 +89,6 @@ class Common extends BaseController
// 公共数据初始化
$this->CommonInit();
// token验证
$this->TokenCheck();
}
/**
@ -149,6 +146,9 @@ class Common extends BaseController
if(empty($this->user))
{
exit(json_encode(DataReturn(MyLang('login_failure_tips'), -400)));
} else {
// token验证
$this->TokenCheck();
}
}

View File

@ -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' => [

View File

@ -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' => [

View File

@ -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));

View File

@ -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;
@ -214,7 +215,9 @@ class GoodsService
],
'm' => 0,
'n' => 0,
'field' => '*'
'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

View File

@ -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);

View File

@ -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);
// 返回数据