Merge pull request 'feat: 修改商品与库存逻辑,优化库存检查与错误处理,增强代码安全性' (#131) from dev into main

Reviewed-on: #131
This commit is contained in:
mkm 2024-08-13 19:58:40 +08:00
commit a427cd50fd
4 changed files with 54 additions and 22 deletions

View File

@ -8,7 +8,7 @@ use app\api\controller\BaseApiController;
use app\api\lists\order\CartList;
use app\common\model\order\Cart;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product_unit\StoreProductUnit;
class CartController extends BaseApiController
{
@ -27,14 +27,26 @@ class CartController extends BaseApiController
$result = Cart::where(['uid' => $params['uid'], 'store_id' => $params['store_id'], 'product_id' => $params['product_id'], 'is_fail' => 0, 'is_pay' => 0, 'delete_time' => null])->find();
//判断起批发价
$batch = StoreBranchProduct::where(
['product_id'=>$params['product_id'],
$branchProduct = StoreBranchProduct::where(
[
'product_id' => $params['product_id'],
'store_id' => $params['store_id']
]
)->value('batch');
if($params['cart_num'] < $batch){
return $this->fail('起批发量低于最低值'.$batch);
)->find();
if (!$branchProduct) {
return $this->fail('商品不存在');
}
if ($params['cart_num'] < $branchProduct['batch']) {
return $this->fail('起批发量低于最低值' . $branchProduct['batch']);
}
if ($params['cart_num']<1) {
$is_bulk = StoreProductUnit::where('id', $branchProduct['unit'])->value('is_bulk');
if ($is_bulk == 0) {
return $this->fail('非计量商品,不能有小数');
}
}
//数量下单判断
$count = Cart::where(['uid' => $params['uid'], 'delete_time' => null, 'is_pay' => 0])->count();
if ($count > 100) {
return $this->fail('购物车商品不能大于100个请先结算');
@ -92,6 +104,4 @@ class CartController extends BaseApiController
return $this->fail(CartLogic::getError());
}
}
}

View File

@ -61,6 +61,7 @@ class OrderList extends BaseAdminDataLists implements ListsSearchInterface
$v['image'] = '';
$v['price'] = '';
$v['unit_name']='';
$v['cart_num']=floatval($v['cart_num']);
if(isset($v['cart_info'])){
// foreach( $v['cart_info'] as $k=>$vv){
$v['store_name'] =$v['cart_info']['name'];

View File

@ -90,6 +90,13 @@ class OrderLogic extends BaseLogic
self::setError('商品不存在');
return false;
}
if (convertNumber($v['cart_num'])==false) {
$is_bulk = StoreProductUnit::where('id', $find['unit'])->value('is_bulk');
if ($is_bulk == 0) {
self::setError('非计量商品,不能有小数,请编辑购物车');
return false;
}
}
$StoreCategory=StoreCategory::where('id',$find['cate_id'])->find();
$find['top_cate_id']=$find['cate_id'];
if($StoreCategory && $StoreCategory['pid']>0){
@ -527,6 +534,7 @@ class OrderLogic extends BaseLogic
$find = StoreProduct::where('id', $item['product_id'])->withTrashed()->find();
}
$item['store_name'] = $find['store_name'];
$item['nums'] = floatval($item['nums']);
$item['image'] = $find['image'];
$item['price'] = $find['price'];
$item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->value('name') ?? '';

View File

@ -480,10 +480,23 @@ if (!function_exists('countRate')) {
if (!function_exists('payPassword')) {
//支付密码
function payPassword($password){
function payPassword($password)
{
return password_hash($password, PASSWORD_BCRYPT);
}
}
if (!function_exists('convertNumber')) {
function convertNumber($str)
{
// 将字符串转换为浮点数
$number = (float)$str;
// 将字符串转换为整数
$int = (int)$str;
if ($number == $int) {
return true;
} else {
return false;
}
}
}