304 lines
12 KiB
PHP
Executable File
304 lines
12 KiB
PHP
Executable File
<?php
|
||
namespace wstmart\app\model;
|
||
use wstmart\common\model\Carts as CCarts;
|
||
use think\Db;
|
||
/**
|
||
* 购物类
|
||
*/
|
||
class Carts extends CCarts{
|
||
/**
|
||
* 购买保存下参数
|
||
* @param integer $goodsId [description]
|
||
* @param integer $goodsSpecId [description]
|
||
* @param integer $buyNum [description]
|
||
* @param integer $uId [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function buy($goodsId=0, $goodsSpecId=0, $buyNum =0, $uId=0){
|
||
|
||
$userId = ($uId==0)?(int)session('WST_USER.userId'):$uId;
|
||
if(!$userId){
|
||
return WSTReturn('购买失败,请先登录',-999);
|
||
}
|
||
$goodsId = ($goodsId==0)?input('goodsId/d'):$goodsId;
|
||
if(!$goodsId){
|
||
exit(jsonReturn('请输入参数值!',-1));
|
||
}
|
||
$goodsSpecId = ($goodsSpecId==0)?input('goodsSpecId/d',0):$goodsSpecId;
|
||
$buyNum = ($buyNum==0)?input('buyNum/d',1):$buyNum;
|
||
$goodsType = (int)input('goodsType/d',1); //1逛商都2助微吧
|
||
$buyNum = ($buyNum>0)?$buyNum:1;
|
||
//验证传过来的商品是否合法
|
||
$chk = $this->checkGoodsSaleSpec($goodsId,$goodsSpecId);
|
||
if($chk['status']==-1)return $chk;
|
||
$goodsSpecId = $chk['data']['goodsSpecId'];
|
||
//检测库存是否足够
|
||
if($chk['data']['stock']<$buyNum){
|
||
return WSTReturn("购买失败,商品库存不足", -1);
|
||
}
|
||
$carts = [];
|
||
$carts['goodsId'] = $goodsId;
|
||
$carts['goodsSpecId'] = $goodsSpecId;
|
||
$carts['buyNum'] = $buyNum;
|
||
$carts['goodsType'] = $goodsType;
|
||
session('HYH_CARTS',$carts);
|
||
return WSTReturn("添加成功", 1);
|
||
}
|
||
/**
|
||
* 现在购买
|
||
* isSettlement 是结算页面
|
||
* uId 会员ID,默认为当前ID
|
||
* userAddress 会员地址
|
||
*/
|
||
public function buyNow($areaId2=0, $uId=0){
|
||
$hyhCarts = session('HYH_CARTS');
|
||
$goodsId = $hyhCarts['goodsId'];
|
||
$goodsSpecId =$hyhCarts['goodsSpecId'];
|
||
$buyNum = $hyhCarts['buyNum'];
|
||
$areaId2 = ($areaId2==0)?input('areaId2/d'):$areaId2;
|
||
$userId = ($uId==0)?(int)session('WST_USER.userId'):$uId;
|
||
if(!$goodsId){
|
||
exit(jsonReturn('请勿重新下单!',-2));
|
||
}
|
||
if(!$userId){
|
||
exit(jsonReturn('请重新登录下单!',-999));
|
||
}
|
||
$where = [];
|
||
$where['g.goodsId'] = $goodsId;
|
||
$where['g.dataFlag'] = 1;
|
||
$where['g.goodsStatus'] = 1;
|
||
$where['g.isSale'] = 1;
|
||
$where['s.dataFlag'] = 1;
|
||
$where['s.shopStatus'] = 1;
|
||
$where['s.status'] = 1;
|
||
if($goodsSpecId){
|
||
$where['gs.id'] = $goodsSpecId;
|
||
}
|
||
//获取商品和商店信息
|
||
$rs = Db::name('goods g')
|
||
->join('__SHOPS__ s','s.shopId=g.shopId','left')
|
||
->join('__GOODS_SPECS__ gs','g.goodsId=gs.goodsId','left')
|
||
->where($where)
|
||
->field('s.userId,s.shopId,s.shopName,s.userId shopUserId,s.shopLevel,g.goodsId,g.goodsName,g.shopPrice,g.goodsStock,g.isSpec,g.marketPrice,gs.specPrice,gs.specStock,g.goodsImg,gs.specIds,g.goodsCatId,g.freight,g.isFreeShipping,gs.initNum,gs.whslePrice')
|
||
->find();
|
||
if(!$rs){
|
||
exit(jsonReturn('商品读取失败',-1));
|
||
}
|
||
|
||
$cartsInfo[$rs['shopId']]['isFreeShipping'] = (bool)$rs['isFreeShipping'];
|
||
$cartsInfo[$rs['shopId']]['promotion'] = [];
|
||
$cartsInfo[$rs['shopId']]['promotionMoney'] = 0;
|
||
$cartsInfo[$rs['shopId']]['shopId'] = $rs['shopId'];
|
||
$cartsInfo[$rs['shopId']]['shopName'] = $rs['shopName'];
|
||
$cartsInfo[$rs['shopId']]['userId'] = $rs['userId'];
|
||
if($rs['isSpec']==1){
|
||
$rs['shopPrice'] = $rs['specPrice'];
|
||
$rs['goodsStock'] = $rs['specStock'];
|
||
}else{
|
||
$rs['initNum'] = 0;
|
||
$rs['whslePrice'] = 0;
|
||
}
|
||
$hyhCarts['goodsType'] = (int) $hyhCarts['goodsType'];
|
||
if(2 == $hyhCarts['goodsType']){//助微吧购物判断店主等级
|
||
$tm = Model('common/Table');
|
||
$tm->setTable('shops');
|
||
//换算为3级循环等级
|
||
if(0 == $rs['shopLevel']){
|
||
$maxSaleMoney = dataConf('helpLevel0MaxSaleMoney');
|
||
}else{
|
||
//$nowLevel = $rs['shopLevel'] % 3;
|
||
$tm->setTable('users');
|
||
//最大可销售金额
|
||
$maxSaleMoney = $tm->getField(['userId'=>$rs['shopUserId']],'helpMaxSaleMoney');
|
||
}
|
||
|
||
$tm->setTable('shops');
|
||
//当前销售值
|
||
$nowSaleMoney = $tm->getSum(['userId'=>$rs['shopUserId']],'helpSaleMoney');
|
||
if(($nowSaleMoney+$rs['shopPrice']) > $maxSaleMoney ){
|
||
exit(jsonReturn('当前助微吧商户可销售额不足!',-1));
|
||
}
|
||
|
||
}
|
||
$cartsInfo[$rs['shopId']]['goodsMoney'] = $rs['shopPrice'] * $buyNum;
|
||
//判断能否购买,预设allowBuy值为10,为将来的各种情况预留10个情况值,从0到9
|
||
$rs['allowBuy'] = 10;
|
||
if($rs['goodsStock']<=0){
|
||
//$rs['allowBuy'] = 0;//库存不足
|
||
exit(jsonReturn('库存不足',-1));
|
||
}else if($rs['goodsStock']<$buyNum){
|
||
//$rs['allowBuy'] = 1;//库存比购买数小
|
||
$buyNum = $rs['goodsStock'];
|
||
}
|
||
|
||
|
||
$rs['specNames'] = [];
|
||
//加载规格值
|
||
if($goodsSpecId>0){
|
||
$specs = DB::name('spec_items')->alias('s')->join('__SPEC_CATS__ sc','s.catId=sc.catId','left')
|
||
->where(['s.goodsId'=>$rs['goodsId'],'s.dataFlag'=>1])->field('catName,itemId,itemName')->select();
|
||
if(count($specs)>0){
|
||
$specMap = [];
|
||
foreach ($specs as $key =>$v){
|
||
$specMap[$v['itemId']] = $v;
|
||
}
|
||
$strName = [];
|
||
if($rs['specIds']!=''){
|
||
$str = explode(':',$rs['specIds']);
|
||
foreach ($str as $vv){
|
||
if(isset($specMap[$vv]))$strName[] = $specMap[$vv];
|
||
}
|
||
$rs['specNames'] = $strName;
|
||
}
|
||
|
||
}
|
||
}
|
||
$cartsInfo[$rs['shopId']]['list']['0'] = $rs;
|
||
$cartsInfo[$rs['shopId']]['list']['0']['promotion'] = [];
|
||
$cartsInfo[$rs['shopId']]['list']['0']['goodsSpecId'] = $goodsSpecId;
|
||
$cartsInfo[$rs['shopId']]['list']['0']['cartNum'] = $buyNum;
|
||
$cartsInfo[$rs['shopId']]['list']['0']['cartId'] = $goodsSpecId;
|
||
$goodsTotalMoney = $rs['shopPrice'] * $buyNum ;
|
||
$goodsTotalNum = 1;
|
||
$cartData = ['carts'=>$cartsInfo,'goodsTotalMoney'=>$goodsTotalMoney,'goodsTotalNum'=>$goodsTotalNum,'promotionMoney'=>0];
|
||
//秒杀活动监听
|
||
hook("beforeSettlement",["carts"=>&$cartData]);
|
||
$allShippingMoney = 0;
|
||
if(empty($cartData['carts']['is_seckilling'])){
|
||
//批发插件 mart hsf 20171116
|
||
hook("mobileControllerCartsSettlement",["carts"=>&$cartData]);
|
||
//店铺优惠活动监听
|
||
hook('afterQueryCarts',["carts"=>&$cartData,'isSettlement'=>true,'isVirtual'=>false,'uId'=>$userId]);
|
||
//ect整合相关优惠还有判断不能和在线支付商品一块
|
||
hook("ectIntegration",["carts"=>&$cartData,'isSettlement'=>true,'uId'=>$userId]);
|
||
//11.11会场商品结算钩子
|
||
hook("orderCatsDoubleEleven",["carts"=>&$cartData,'isSettlement'=>true,'uId'=>$userId]);
|
||
|
||
}
|
||
//添加运费计算
|
||
if($rs['isFreeShipping']){
|
||
$cartData['carts'][$rs['shopId']]["shippingMoney"] = 0;
|
||
}else{
|
||
$cartData['carts'][$rs['shopId']]["shippingMoney"] = $rs['freight'];
|
||
// if($areaId2){
|
||
// $cartData['carts'][$rs['shopId']]["shippingMoney"] = WSTOrderFreight($rs['shopId'],$areaId2);
|
||
// }else{
|
||
// $cartData['carts'][$rs['shopId']]["shippingMoney"] = WSTOrderFreight($rs['shopId'],-1);
|
||
// }
|
||
$allShippingMoney += $cartData['carts'][$rs['shopId']]["shippingMoney"];
|
||
}
|
||
if($allShippingMoney){
|
||
$cartData['goodsTotalMoney'] += $allShippingMoney;
|
||
}
|
||
$cartData['allShippingMoney'] = $allShippingMoney;//添加总运费
|
||
$cartData['goodsType'] = $hyhCarts['goodsType'];
|
||
return $cartData;
|
||
}
|
||
/**
|
||
* 计算订单金额
|
||
*/
|
||
public function getMoney($areaId2=0, $uId=0){
|
||
$data = ['shops'=>[],'totalMoney'=>0,'totalGoodsMoney'=>0];
|
||
$userId = $uId==0?(int)session('WST_USER.userId'):$uId;
|
||
$areaId = input('post.areaId2/d',-1);
|
||
//计算各店铺运费及金额
|
||
$deliverType = (int)input('deliverType');//0是快递,1是自提,自提的不要运费,先取消
|
||
$carts = $this->buyNow($areaId,$userId);
|
||
$shopFreight = 0;
|
||
$maxScoreMoney = 0;//初始化最大可用惠宝数 mark hsf 20171117
|
||
$total_promotion_money = 0;//初始化合计优惠 mark hsf 20170303
|
||
$data['maxScoreMoneySum'] = 0;
|
||
foreach ($carts['carts'] as &$v){//优化一下循环效率 mark hsf 20171118
|
||
|
||
// if($v['isFreeShipping']){
|
||
// $data['shops'][$v['shopId']]['freight'] = 0;
|
||
// }else{
|
||
// $shopFreight = ($deliverType==1)?0:WSTOrderFreight($v['shopId'],$areaId);
|
||
// $data['shops'][$v['shopId']]['freight'] = $shopFreight;
|
||
// }
|
||
if($v['isFreeShipping']){
|
||
$shopFreight = 0;
|
||
}else{
|
||
if($deliverType == 0){
|
||
$shopFreight = $v['shippingMoney'];
|
||
}
|
||
}
|
||
|
||
$data['shops'][$v['shopId']]['freight'] = $shopFreight;
|
||
$data['shops'][$v['shopId']]['oldGoodsMoney'] = $v['goodsMoney'];
|
||
$data['shops'][$v['shopId']]['goodsMoney'] = $v['goodsMoney']+$shopFreight-$v['promotionMoney'];
|
||
$data['totalGoodsMoney'] += $v['goodsMoney']-$v['promotionMoney'];
|
||
$data['totalMoney'] += $v['goodsMoney'] + $shopFreight-$v['promotionMoney'];
|
||
$total_promotion_money += $v['promotionMoney'];//合计优惠 mark hsf 20170303
|
||
/*
|
||
* 计算最大可用惠宝,添加验证批发价的惠宝可抵用 mark hsf 20171117
|
||
*/
|
||
// foreach ($v['list'] as &$val) {
|
||
// if(isset($val['isWhsle'])){//是批发价的
|
||
// $maxScoreMoney += (int)($val['shopPrice'] * $val['cartNum'] * HuiWhsleScale());//可用惠宝默认抵10%
|
||
// }else{
|
||
// $maxScoreMoney += (int)($val['shopPrice'] * $val['cartNum'] * HuiScale());
|
||
// }
|
||
// }
|
||
/*
|
||
*----------end-------------
|
||
*/
|
||
}
|
||
//批发插件 mart hsf 20171116
|
||
hook("mobileControllerCartsSettlement",["carts"=>&$carts]);
|
||
//放钩子计算11.11订单的分类商品使用优惠券后的金额
|
||
hook("orderCatsCouponEleven",['data'=>&$data,'carts'=>&$carts,'isSettlement'=>true,'uId'=>$userId]);
|
||
if(isset($data['couponMoney']) && $data['couponMoney'] > 0 ){
|
||
$total_promotion_money += $data['couponMoney'];
|
||
}
|
||
//此处放钩子计算商家使用优惠券后的金额-根据优惠券ID计算
|
||
hook("afterCalculateCartMoney",["data"=>&$data,'carts'=>$carts,'isVirtual'=>false,'uId'=>$userId]);
|
||
//ect整合相关优惠还有判断不能和在线支付商品一块
|
||
hook("ectIntegration",["carts"=>&$carts,'isSettlement'=>true,'uId'=>$userId]);
|
||
$maxScoreMoney = $data['maxScoreMoneySum']-($total_promotion_money * HuiScale());//减去优惠过的
|
||
$maxScoreMoney = $maxScoreMoney < 0 ? 0 : $maxScoreMoney;
|
||
$data['totalGoodsMoney'] = ($data['totalGoodsMoney']>$data['totalMoney'])?$data['totalMoney']:$data['totalGoodsMoney'];
|
||
//echo $data['totalGoodsMoney'];
|
||
$data['maxScore'] = 0;
|
||
$data['maxScoreMoney'] = 0;
|
||
$data['useScore'] = 0;
|
||
$data['scoreMoney'] = 0;
|
||
// //计算最大可用惠宝
|
||
// /**
|
||
// * 最多金额抵用20% mark 20170907
|
||
// */
|
||
// //$maxScoreMoney = (int)($data['totalGoodsMoney'] * HuiScale());//$data['totalGoodsMoney']; //惠宝最多可抵用20% mark 20170907
|
||
// $maxScore = WSTScoreToMoney($maxScoreMoney,true);//WSTScoreToMoney($data['totalGoodsMoney'],true);
|
||
// /*
|
||
// -------------------end----------------
|
||
// */
|
||
// //最大可用惠宝不能大于用户惠宝
|
||
// $user = model('users')->getFieldsById($userId,'userScore');
|
||
// if($maxScore>$user['userScore']){
|
||
// $maxScore = $user['userScore'];
|
||
// $maxScoreMoney = WSTScoreToMoney($maxScore);
|
||
// }
|
||
// $data['maxScore'] = $maxScore;
|
||
// $data['maxScoreMoney'] = $maxScoreMoney;
|
||
// //判断是否使用惠宝
|
||
// $isUseScore = (int)input('isUseScore');
|
||
// if($isUseScore==1){
|
||
// //不能比用户惠宝还多
|
||
// $useScore = (float)input('useScore');
|
||
// if($useScore>$maxScore)$useScore = $maxScore;
|
||
// $data['useScore'] = $useScore;
|
||
// $data['scoreMoney'] = WSTScoreToMoney($useScore);
|
||
// }
|
||
// if((isset($carts['is_seckilling']) && $carts['is_seckilling'] == 1) || (isset($carts['promotion_goods']) && $carts['promotion_goods'] == 1)){//不可抵用惠宝
|
||
// $data['useScore']=0;
|
||
// $data['scoreMoney']=0;
|
||
// }
|
||
//$carts['promotionMoney'] = isset($carts['promotionMoney']) ? $carts['promotionMoney'] : 0;
|
||
$data['couponMoney'] = isset($data['couponMoney']) ? $data['couponMoney'] : 0;
|
||
$data['realTotalMoney'] = WSTPositiveNum($data['totalMoney'] - $data['scoreMoney'] - $data['couponMoney']);
|
||
//dump($data);
|
||
return WSTReturn('',1,$data);
|
||
}
|
||
|
||
}
|