You've already forked qlg.tsgz.moe
							
							Init Repo
This commit is contained in:
		
							
								
								
									
										610
									
								
								hyhproject/common/model/Carts.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										610
									
								
								hyhproject/common/model/Carts.php
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,610 @@ | ||||
| <?php | ||||
| namespace wstmart\common\model; | ||||
| use think\Db; | ||||
| /** | ||||
|  * ============================================================================ | ||||
|  * 购物车业务处理类 | ||||
|  */ | ||||
|  | ||||
| class Carts extends Base{ | ||||
| 	 | ||||
| 	/** | ||||
| 	 * 加入购物车 | ||||
| 	 */ | ||||
| 	public function addCart(){ | ||||
| //return WSTReturn("系统调试中,暂不支持购买,请等待调试完成", -1); | ||||
| 		$userId = (int)session('WST_USER.userId'); | ||||
| 		$goodsId = (int)input('post.goodsId'); | ||||
| 		//goods_specs表的规格ID | ||||
| 		$goodsSpecId = (int)input('post.goodsSpecId'); | ||||
| 		$type = (int)input('post.type'); | ||||
| 		//mark 添加验证 | ||||
| 		//$m = new \addons\hyhsale\model\Hyhsale(); | ||||
| 		// if($type == 0 && model('\addons\hyhsale\model\Hyhsale')->getEffectiveGoods(['goodsId'=>$goodsId,'specsId'=>$goodsSpecId])){ | ||||
| 		// 	return WSTReturn("秒杀活动商品请点击立即购买!", -1); | ||||
| 		// } | ||||
|  | ||||
| 		$cartNum = (int)input('post.buyNum',1); | ||||
| 		$cartNum = ($cartNum>0)?$cartNum:1; | ||||
| 		 | ||||
| 		if($userId==0)return WSTReturn('加入购物车失败,请先登录'); | ||||
| 		//验证传过来的商品是否合法 | ||||
| 		$chk = $this->checkGoodsSaleSpec($goodsId,$goodsSpecId); | ||||
| 		if($chk['status']==-1)return $chk; | ||||
| 		//检测库存是否足够 | ||||
| 		if($chk['data']['stock']<$cartNum)return WSTReturn("加入购物车失败,商品库存不足", -1); | ||||
| 		//添加实物商品 | ||||
| 		if($chk['data']['goodsType']==0){ | ||||
| 			$goodsSpecId = $chk['data']['goodsSpecId']; | ||||
| 			$goods = $this->where(['userId'=>$userId,'goodsId'=>$goodsId,'goodsSpecId'=>$goodsSpecId])->select(); | ||||
|  | ||||
| 			if(empty($goods)){ | ||||
| 				$data = array(); | ||||
| 				$data['userId'] = $userId; | ||||
| 				$data['goodsId'] = $goodsId; | ||||
| 				$data['goodsSpecId'] = $goodsSpecId; | ||||
| 				$data['isCheck'] = 1; | ||||
| 				$data['cartNum'] = $cartNum; | ||||
| 				$rs = $this->save($data); | ||||
| 			}else{ | ||||
| 				$rs = $this->where(['userId'=>$userId,'goodsId'=>$goodsId,'goodsSpecId'=>$goodsSpecId])->setInc('cartNum',$cartNum); | ||||
| 			} | ||||
| 			if(false !==$rs){ | ||||
| 				if($type==1){ | ||||
| 					$cartId = $this->where(['userId'=>$userId,'goodsId'=>$goodsId,'goodsSpecId'=>$goodsSpecId])->value('cartId'); | ||||
| 					$this->where("cartId = ".$cartId." and userId=".$userId)->setField('isCheck',1); | ||||
| 					$this->where("cartId != ".$cartId." and userId=".$userId)->setField('isCheck',0); | ||||
| 					$this->where(['cartId' =>$cartId,'userId'=>$userId])->setField('cartNum',$cartNum); | ||||
| 				} | ||||
| 				return WSTReturn("添加成功", 1); | ||||
| 			} | ||||
| 		}else{ | ||||
| 			//非实物商品 | ||||
|             $carts = []; | ||||
|             $carts['goodsId'] = $goodsId; | ||||
|             $carts['cartNum'] = $cartNum; | ||||
|             session('TMP_CARTS',$carts); | ||||
|             return WSTReturn("添加成功", 1,['forward'=>'quickSettlement']); | ||||
| 		} | ||||
| 		return WSTReturn("加入购物车失败", -1); | ||||
| 	} | ||||
| 	/** | ||||
| 	 * 验证商品是否合法 | ||||
| 	 */ | ||||
| 	public function checkGoodsSaleSpec($goodsId,$goodsSpecId){ | ||||
| 		$goods = model('Goods')->where(['goodsStatus'=>1,'dataFlag'=>1,'isSale'=>1,'goodsId'=>$goodsId])->field('goodsId,isSpec,goodsStock,goodsType')->find(); | ||||
| 		if(empty($goods))return WSTReturn("添加失败,无效的商品信息", -1); | ||||
| 		$goodsStock = (int)$goods['goodsStock']; | ||||
| 		//有规格的话查询规格是否正确 | ||||
| 		if($goods['isSpec']==1){ | ||||
| 			//商品规格详细列表页 | ||||
| 			$specs = Db::name('goods_specs')->where(['goodsId'=>$goodsId,'dataFlag'=>1])->field('id,isDefault,specStock')->select(); | ||||
| 			if(count($specs)==0){ | ||||
| 				return WSTReturn("添加失败,无效的商品信息", -1); | ||||
| 			} | ||||
| 			//默认规格ID | ||||
| 			$defaultGoodsSpecId = 0; | ||||
| 			//默认规格库存 | ||||
| 			$defaultGoodsSpecStock = 0; | ||||
| 			//是否找到规格 | ||||
| 			$isFindSpecId = false; | ||||
| 			foreach ($specs as $key => $v){ | ||||
| 				//获取默认的规格 | ||||
| 				if($v['isDefault']==1){ | ||||
| 					$defaultGoodsSpecId = $v['id']; | ||||
| 					$defaultGoodsSpecStock = (int)$v['specStock']; | ||||
| 				} | ||||
| 				//找到相关的规格 | ||||
| 				if($v['id']==$goodsSpecId){ | ||||
| 					$goodsStock = (int)$v['specStock']; | ||||
| 					$isFindSpecId = true; | ||||
| 				} | ||||
| 			} | ||||
| 			 | ||||
| 			if($defaultGoodsSpecId==0)return WSTReturn("添加失败,无效的商品信息", -1);//有规格却找不到规格的话就报错 | ||||
| 			//if(!$isFindSpecId)return  WSTReturn("添加失败,未找到该规格信息", -1);//修改没有规格直接不能买 mark hsf 20180313 | ||||
| 			if(!$isFindSpecId)return WSTReturn("", 1,['goodsSpecId'=>$defaultGoodsSpecId,'stock'=>$defaultGoodsSpecStock,'goodsType'=>$goods['goodsType']]);//如果没有找到的话就取默认的规格 mark hsf 20180313 | ||||
| 			return WSTReturn("", 1,['goodsSpecId'=>$goodsSpecId,'stock'=>$goodsStock,'goodsType'=>$goods['goodsType']]); | ||||
| 		}else{ | ||||
| 			return WSTReturn("", 1,['goodsSpecId'=>0,'stock'=>$goodsStock,'goodsType'=>$goods['goodsType']]); | ||||
| 		} | ||||
| 	} | ||||
| 	/** | ||||
| 	 * 删除购物车里的商品 | ||||
| 	 */ | ||||
| 	public function delCart(){ | ||||
| 		$userId = (int)session('WST_USER.userId'); | ||||
| 		$id = input('post.id'); | ||||
| 		$id = explode(',',WSTFormatIn(",",$id)); | ||||
| 		$id = array_filter($id); | ||||
| 		$this->where("userId = ".$userId." and cartId in(".implode(',', $id).")")->delete(); | ||||
| 		return WSTReturn("删除成功", 1); | ||||
| 	} | ||||
| 	/** | ||||
| 	 * 取消购物车商品选中状态 | ||||
| 	 */ | ||||
| 	public function disChkGoods($goodsId,$goodsSpecId,$userId){ | ||||
| 		$this->save(['isCheck'=>0],['userId'=>$userId,'goodsId'=>$goodsId,'goodsSpecId'=>$goodsSpecId]); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * 获取session中购物车列表 | ||||
| 	 */ | ||||
| 	public function getQuickCarts($uId=0){ | ||||
| 		$userId = ($uId==0)?(int)session('WST_USER.userId'):$uId; | ||||
| 		$tmp_carts = session('TMP_CARTS'); | ||||
| 		$where = []; | ||||
| 		$where['goodsId'] = $tmp_carts['goodsId']; | ||||
| 		$rs = Db::name('goods')->alias('g') | ||||
| 		           ->join('__SHOPS__ s','s.shopId=g.shopId','left') | ||||
| 		           ->where($where) | ||||
| 		           ->field('s.userId,s.shopId,s.shopName,g.goodsId,s.shopQQ,shopWangWang,g.goodsName,g.shopPrice,g.goodsStock,g.goodsImg,g.goodsCatId,g.isFreeShipping') | ||||
| 		           ->find(); | ||||
| 		if(empty($rs))return ['carts'=>[],'goodsTotalMoney'=>0,'goodsTotalNum'=>0];  | ||||
| 		$rs['cartNum'] = $tmp_carts['cartNum']; | ||||
| 		$carts = []; | ||||
| 		$cartShop = []; | ||||
| 		$goodsTotalNum = 1; | ||||
| 		$goodsTotalMoney = 0; | ||||
| 		//勿删!为插件促销活动做准备接口 | ||||
| 		$rs['promotion'] = [];//商品要优惠的活动 | ||||
| 		$cartShop['promotion'] = [];//店铺要优惠的活动 | ||||
| 		$cartShop['promotionMoney'] = 0;//店铺要优惠的金额 | ||||
| 		//--------------------------- | ||||
| 		$cartShop['isFreeShipping'] = true; | ||||
| 		$cartShop['shopId'] = $rs['shopId']; | ||||
| 		$cartShop['shopName'] = $rs['shopName']; | ||||
| 		$cartShop['shopQQ'] = $rs['shopQQ']; | ||||
| 		$cartShop['userId'] = $rs['userId']; | ||||
| 		$cartShop['shopWangWang'] = $rs['shopWangWang']; | ||||
| 		//判断能否购买,预设allowBuy值为10,为将来的各种情况预留10个情况值,从0到9 | ||||
| 		$rs['allowBuy'] = 10; | ||||
| 		if($rs['goodsStock']<0){ | ||||
| 			$rs['allowBuy'] = 0;//库存不足 | ||||
| 		}else if($rs['goodsStock']<$tmp_carts['cartNum']){ | ||||
| 			//$rs['allowBuy'] = 1;//库存比购买数小 | ||||
|             $rs['cartNum'] = $rs['goodsStock']; | ||||
| 		} | ||||
| 		$cartShop['goodsMoney'] = $rs['shopPrice'] * $rs['cartNum']; | ||||
| 		$goodsTotalMoney = $goodsTotalMoney + $rs['shopPrice'] * $rs['cartNum']; | ||||
| 		$rs['specNames'] = []; | ||||
| 		$rs['cartId'] = $rs['goodsId']; | ||||
| 		unset($rs['shopName']); | ||||
| 		$cartShop['list'][] = $rs; | ||||
| 		$carts[$cartShop['shopId']] = $cartShop; | ||||
| 		$cartData = ['carts'=>$carts,'goodsTotalMoney'=>$goodsTotalMoney,'goodsTotalNum'=>$goodsTotalNum,'promotionMoney'=>0]; | ||||
| 		//店铺优惠活动监听 | ||||
| 		hook("afterQueryCarts",["carts"=>&$cartData,'isSettlement'=>true,'isVirtual'=>true,'uId'=>$userId]);  | ||||
| 		return $cartData;  | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| 	 * 获取购物车列表 | ||||
| 	 * isSettlement 是结算页面 | ||||
| 	 * uId 会员ID,默认为当前ID | ||||
| 	 * areaId2 市id | ||||
| 	 */ | ||||
| 	public function getCarts($isSettlement = false, $uId=0, $areaId2=0){//添加会员地址判断 mark hsf 20171116  | ||||
| 		$userId = ($uId==0)?(int)session('WST_USER.userId'):$uId; | ||||
| 		$where = []; | ||||
| 		$where['c.userId'] = $userId; | ||||
| 		$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($isSettlement)$where['c.isCheck'] = 1;//在结算页面,就获取选中的商品 | ||||
| 		//获取购物车产品,包括选中和未选中的 | ||||
| 		$rs = Db::name('carts')->alias('c')->join('__GOODS__ g','c.goodsId=g.goodsId','inner') | ||||
| 		           ->join('__SHOPS__ s','s.shopId=g.shopId','left') | ||||
| 		           ->join('__GOODS_SPECS__ gs','c.goodsSpecId=gs.id','left') | ||||
| 		           ->where($where) | ||||
| 		           ->field('c.goodsSpecId,c.cartId,s.userId,s.shopId,s.shopName,g.goodsId,s.shopQQ,shopWangWang,g.goodsName,g.shopPrice,g.goodsStock,g.isSpec,gs.specPrice,gs.specStock,g.goodsImg,c.isCheck,gs.specIds,c.cartNum,g.goodsCatId,g.freight,g.isFreeShipping,gs.initNum,gs.whslePrice,g.marketPrice') | ||||
| 		           ->select();//添加返回起批量和批发价 mark hsf 20171117 | ||||
| 		//dump($rs);die; | ||||
| 		$carts = []; | ||||
| 		$goodsIds = []; | ||||
| 		$goodsTotalNum = 0; | ||||
| 		$goodsTotalMoney = 0; | ||||
| 		//将同一个店铺的商品整合到一块 | ||||
| 		foreach ($rs as $key =>$v){ | ||||
| 			if(!isset($carts[$v['shopId']]['goodsMoney']))$carts[$v['shopId']]['goodsMoney'] = 0; | ||||
| 			if(!isset($carts[$v['shopId']]['isFreeShipping']))$carts[$v['shopId']]['isFreeShipping'] = true; | ||||
|             //勿删!为插件促销活动做准备接口 | ||||
| 			$v['promotion'] = [];//商品优惠活动 | ||||
| 			$carts[$v['shopId']]['promotion'] = [];//店铺优惠活动 | ||||
| 			$carts[$v['shopId']]['promotionMoney'] = 0;//店铺要优惠的金额 | ||||
| 			//---------------------------- | ||||
| 			$carts[$v['shopId']]['shopId'] = $v['shopId']; | ||||
| 			$carts[$v['shopId']]['shopName'] = $v['shopName']; | ||||
| 			$carts[$v['shopId']]['shopQQ'] = $v['shopQQ']; | ||||
| 			$carts[$v['shopId']]['userId'] = $v['userId']; | ||||
| 			//如果店铺一旦不包邮了,那么就不用去判断商品是否包邮了 | ||||
| 			if($v['isFreeShipping']==0 && $carts[$v['shopId']]['isFreeShipping'])$carts[$v['shopId']]['isFreeShipping'] = false; | ||||
| 			$carts[$v['shopId']]['shopWangWang'] = $v['shopWangWang']; | ||||
| 			if($v['isSpec']==1){ | ||||
| 				$v['shopPrice'] = $v['specPrice']; | ||||
| 				$v['goodsStock'] = $v['specStock']; | ||||
| 			}else{ | ||||
| 				$v['initNum'] = 0;//添加返回起批量和批发价 mark hsf 20171117 | ||||
| 				$v['whslePrice'] = 0;//添加返回起批量和批发价 mark hsf 20171117			 | ||||
| 			} | ||||
| 			//$v['initNum'] = $v['initNum'];//添加返回起批量和批发价 mark hsf 20171117 | ||||
| 			//$v['whslePrice'] = $v['whslePrice'];//添加返回起批量和批发价 mark hsf 20171117			 | ||||
| 			//判断能否购买,预设allowBuy值为10,为将来的各种情况预留10个情况值,从0到9 | ||||
| 			$v['allowBuy'] = 10; | ||||
| 			if($v['goodsStock']<0){ | ||||
| 				$v['allowBuy'] = 0;//库存不足 | ||||
| 			}else if($v['goodsStock']<$v['cartNum']){ | ||||
| 				//$v['allowBuy'] = 1;//库存比购买数小 | ||||
| 				$v['cartNum'] = $v['goodsStock']; | ||||
| 			} | ||||
| 			//如果是结算的话,则要过滤了不符合条件的商品 | ||||
| 			if($isSettlement && $v['allowBuy']!=10){ | ||||
| 				$this->disChkGoods($v['goodsId'],(int)$v['goodsSpecId'],(int)session('WST_USER.userId')); | ||||
| 				continue; | ||||
| 			} | ||||
| 			if($v['isCheck']==1){ | ||||
| 				$carts[$v['shopId']]['goodsMoney'] = $carts[$v['shopId']]['goodsMoney'] + $v['shopPrice'] * $v['cartNum']; | ||||
| 				$goodsTotalMoney = $goodsTotalMoney + $v['shopPrice'] * $v['cartNum']; | ||||
| 				$goodsTotalNum++; | ||||
| 			} | ||||
| 			$v['specNames'] = []; | ||||
| 			unset($v['shopName']); | ||||
| 			$carts[$v['shopId']]['list'][] = $v; | ||||
| 			if(!in_array($v['goodsId'],$goodsIds))$goodsIds[] = $v['goodsId']; | ||||
| 		} | ||||
|  | ||||
|  | ||||
| 		//dump($carts);die; | ||||
| 		//加载规格值 | ||||
| 		if(count($goodsIds)>0){ | ||||
| 		    $specs = DB::name('spec_items')->alias('s')->join('__SPEC_CATS__ sc','s.catId=sc.catId','left') | ||||
| 		        ->where(['s.goodsId'=>['in',$goodsIds],'s.dataFlag'=>1])->field('catName,itemId,itemName')->select(); | ||||
| 		    if(count($specs)>0){  | ||||
| 		    	$specMap = []; | ||||
| 		    	foreach ($specs as $key =>$v){ | ||||
| 		    		$specMap[$v['itemId']] = $v; | ||||
| 		    	} | ||||
| 			    foreach ($carts as $key =>$shop){ | ||||
| 			    	foreach ($shop['list'] as $skey =>$v){ | ||||
| 			    		$strName = []; | ||||
| 			    		if($v['specIds']!=''){ | ||||
| 			    			$str = explode(':',$v['specIds']); | ||||
| 			    			foreach ($str as $vv){ | ||||
| 			    				if(isset($specMap[$vv]))$strName[] = $specMap[$vv]; | ||||
| 			    			} | ||||
| 			    		} | ||||
| 			    		$carts[$key]['list'][$skey]['specNames'] = $strName; | ||||
| 			    	} | ||||
| 			    } | ||||
| 		    } | ||||
| 		} | ||||
|  | ||||
| 		$cartData = ['carts'=>$carts,'goodsTotalMoney'=>$goodsTotalMoney,'goodsTotalNum'=>$goodsTotalNum,'promotionMoney'=>0];  | ||||
|  | ||||
| 		//秒杀活动监听 | ||||
| 		hook("beforeSettlement",["carts"=>&$cartData]); | ||||
|  | ||||
| 		$allShippingMoney = 0; | ||||
| 		if(empty($cartData['carts']['is_seckilling'])){			 | ||||
| 			//dump($cartData);die; | ||||
| 			//店铺优惠活动监听 | ||||
| 			hook("afterQueryCarts",["carts"=>&$cartData,'isSettlement'=>$isSettlement,'isVirtual'=>false,'uId'=>$userId]);  | ||||
| 			//批发插件 mart hsf 20171116 | ||||
| 			hook("mobileControllerCartsSettlement",["carts"=>&$cartData]); | ||||
| 			//ect整合相关优惠还有判断不能和在线支付商品一块 | ||||
| 			hook("ectIntegration",["carts"=>&$cartData,'isSettlement'=>$isSettlement,'uId'=>$userId]); | ||||
| 			//11.11会场商品结算钩子 | ||||
| 			hook("orderCatsDoubleEleven",["carts"=>&$cartData,'isSettlement'=>$isSettlement,'uId'=>$userId]); | ||||
| 		} | ||||
| 		$freight=0; | ||||
| 		/*添加运费计算 ,要先算店铺活动,不然满送运费有问题 mark hsf 20171116 */ | ||||
| 		$carts = &$cartData['carts']; | ||||
| 		foreach ($carts as $val) { | ||||
| 			if(!isset($carts[$val['shopId']]['shippingMoney']))$carts[$val['shopId']]['shippingMoney'] = 0; | ||||
|  | ||||
| 			if($val['isFreeShipping']){ | ||||
| 	          	$freight = 0; | ||||
| 			}else{ | ||||
| 				foreach ($val['list'] as &$value) { | ||||
| 					$freight += $value['freight']; | ||||
| 				} | ||||
|                  | ||||
| //				if($areaId2){ | ||||
| //		            $freight = WSTOrderFreight($val['shopId'],$areaId2); | ||||
| //		        }else{ | ||||
| //		            $freight = WSTOrderFreight($val['shopId'],-1); | ||||
| //		        } | ||||
| 	        } | ||||
| 	        $carts[$val['shopId']]['shippingMoney'] += $freight;//累计运费 | ||||
| 	        $allShippingMoney += $freight; | ||||
| 		}		 | ||||
| 		//foreach ($carts as $v) { | ||||
| 			$cartData['goodsTotalMoney'] += $allShippingMoney;//$v['shippingMoney']; | ||||
| 		//} | ||||
|         /*-------end--------*/ | ||||
| 		 | ||||
| 		 | ||||
|  | ||||
| 		// $cartData['goodsTotalMoney']-=$cartData['promotionMoney']; | ||||
| 		// $cartData['goodsTotalMoney'] = $cartData['goodsTotalMoney'] > 0 ? $cartData['goodsTotalMoney'] : 0;  | ||||
| 		$cartData['all_carts_num'] = $this->where('userId='.$userId)->count();//添加会员的购物车产品数量 mark hsf 20180111 | ||||
| 		$cartData['allShippingMoney'] = $allShippingMoney;//添加总运费 | ||||
| 		return $cartData;    | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| 	 * 获取购物车商品列表 | ||||
| 	 */ | ||||
| 	public function getCartInfo($isSettlement = false){ | ||||
| 		$userId = (int)session('WST_USER.userId'); | ||||
| 		$where = []; | ||||
| 		$where['c.userId'] = $userId; | ||||
| 		if($isSettlement)$where['c.isCheck'] = 1; | ||||
| 		$rs = $this->alias('c')->join('__GOODS__ g','c.goodsId=g.goodsId','inner') | ||||
| 		           ->join('__GOODS_SPECS__ gs','c.goodsSpecId=gs.id','left') | ||||
| 		           ->where($where) | ||||
| 		           ->field('c.goodsSpecId,c.cartId,g.goodsId,g.goodsName,g.shopPrice,g.goodsStock,g.isSpec,gs.specPrice,gs.specStock,g.goodsImg,c.isCheck,gs.specIds,c.cartNum') | ||||
| 		           ->select(); | ||||
| 		$goodsIds = [];  | ||||
| 		$goodsTotalMoney = 0; | ||||
| 		$goodsTotalNum = 0; | ||||
| 		foreach ($rs as $key =>$v){ | ||||
| 			if(!in_array($v['goodsId'],$goodsIds))$goodsIds[] = $v['goodsId']; | ||||
| 			if($v['isSpec']==1){ | ||||
| 				$v['shopPrice'] = $v['specPrice']; | ||||
| 				$v['goodsStock'] = $v['specStock']; | ||||
| 			} | ||||
| 			if($v['goodsStock']<$v['cartNum']){ | ||||
| 				$v['cartNum'] = $v['goodsStock']; | ||||
| 			} | ||||
| 			$goodsTotalMoney = $goodsTotalMoney + $v['shopPrice'] * $v['cartNum']; | ||||
| 			$rs[$key]['goodsImg'] = WSTImg($v['goodsImg']); | ||||
| 		} | ||||
| 	    //加载规格值 | ||||
| 		if(count($goodsIds)>0){ | ||||
| 		    $specs = DB::name('spec_items')->alias('s')->join('__SPEC_CATS__ sc','s.catId=sc.catId','left') | ||||
| 		        ->where(['s.goodsId'=>['in',$goodsIds],'s.dataFlag'=>1])->field('itemId,itemName')->select(); | ||||
| 		    if(count($specs)>0){  | ||||
| 		    	$specMap = []; | ||||
| 		    	foreach ($specs as $key =>$v){ | ||||
| 		    		$specMap[$v['itemId']] = $v; | ||||
| 		    	} | ||||
| 			    foreach ($rs as $key =>$v){ | ||||
| 			    	$strName = []; | ||||
| 			    	if($v['specIds']!=''){ | ||||
| 			    		$str = explode(':',$v['specIds']); | ||||
| 			    		foreach ($str as $vv){ | ||||
| 			    			if(isset($specMap[$vv]))$strName[] = $specMap[$vv]['itemName']; | ||||
| 			    		} | ||||
| 			    	} | ||||
| 			    	$rs[$key]['specNames'] = $strName; | ||||
| 			    } | ||||
| 		    } | ||||
| 		} | ||||
| 		$goodsTotalNum = count($rs); | ||||
| 		return ['list'=>$rs,'goodsTotalMoney'=>sprintf("%.2f", $goodsTotalMoney),'goodsTotalNum'=>$goodsTotalNum]; | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| 	 * 修改购物车商品状态 | ||||
| 	 */ | ||||
| 	public function changeCartGoods(){ | ||||
| 		$isCheck = Input('post.isCheck/d',-1); | ||||
| 		$buyNum = Input('post.buyNum/d',1); | ||||
| 		if($buyNum<1)$buyNum = 1; | ||||
| 		$id = Input('post.id/d'); | ||||
| 		$userId = (int)session('WST_USER.userId'); | ||||
| 		$data = []; | ||||
| 		if($isCheck!=-1)$data['isCheck'] = $isCheck; | ||||
| 		$data['cartNum'] = $buyNum; | ||||
| 		$this->where(['userId'=>$userId,'cartId'=>$id])->update($data); | ||||
| 		return WSTReturn("操作成功", 1); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * 批量修改购物车商品状态 | ||||
| 	 */ | ||||
| 	public function batchChangeCartGoods(){ | ||||
| 		$ids = input('ids'); | ||||
| 		if($ids=='')return WSTReturn("操作失败"); | ||||
|         $ids = explode(',',WSTFormatIn(',',$ids)); | ||||
|         $userId = (int)session('WST_USER.userId'); | ||||
|         $isCheck = ((int)input('post.isCheck/d',-1)==1)?1:0; | ||||
|         $this->where(['userId'=>$userId,'cartId'=>['in',$ids]])->update(['isCheck'=>$isCheck]); | ||||
| 		return WSTReturn("操作成功", 1); | ||||
| 	} | ||||
| 	/** | ||||
| 	 * 计算订单金额 | ||||
| 	 */ | ||||
| 	public function getCartMoney($uId=0){ | ||||
| 		$data = ['shops'=>[],'totalMoney'=>0,'totalGoodsMoney'=>0];		 | ||||
|         $areaId = input('areaId2/d',-1); | ||||
|         $userId = $uId==0?(int)session('WST_USER.userId'):$uId; | ||||
| 		//计算各店铺运费及金额 | ||||
| 		$deliverType = (int)input('deliverType');//0是快递,1是自提,自提的不要运费,先取消 mark 20170907 | ||||
| 		$carts = $this->getCarts(true,$userId,$areaId);//计算价格添加城市运费 mark hsf 20171222  | ||||
| 		$data['couponMoney'] = 0;//定义一个变量,存储11.11优惠券价格 | ||||
| 		$data['maxScoreMoneySum'] = 0;//定义一个变量,存储惠宝数量 | ||||
| 		//批发插件 mart hsf 20171116 | ||||
| 		//hook("mobileControllerCartsSettlement",["carts"=>&$carts]); | ||||
| 		$shopFreight = 0; | ||||
| 		$maxScoreMoney = 0;//初始化最大可用惠宝数 mark hsf 20171117 | ||||
| 		$total_promotion_money = 0;//初始化合计优惠 mark hsf 20170303 | ||||
| 		foreach ($carts['carts'] as &$v){//优化一下循环效率 mark hsf 20171118 | ||||
| 			/** | ||||
| 			 * 修改运费计算 | ||||
| 			 * 因为在获取购物车产品信息时已经加上运费了 | ||||
| 			 * mark hsf 20171222 | ||||
| 			 */ | ||||
| 			// 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; | ||||
| 		 | ||||
| 			/****************end****************/ | ||||
| 			$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;//防止负数 mark 20180303  | ||||
|  | ||||
| 		// dump($data)	;die; | ||||
| 		$data['totalGoodsMoney'] = ($data['totalGoodsMoney']>$data['totalMoney'])?$data['totalMoney']:$data['totalGoodsMoney']; | ||||
| 		$data['maxScore'] = 0; | ||||
| 		$data['maxScoreMoney'] = 0; | ||||
| 		$data['useScore'] = 0; | ||||
| 		$data['scoreMoney'] = 0; | ||||
| 		//计算最大可用惠宝 | ||||
| 		/** | ||||
| 		 * 最多金额抵用20% mark 20170907 | ||||
| 		 */ | ||||
| 		$maxScore = round(WSTScoreToMoney($maxScoreMoney,true),2);//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 = 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); | ||||
| 	} | ||||
|  | ||||
| 	public function getQuickCartMoney($uId=0){ | ||||
| 		$data = ['shops'=>[],'totalMoney'=>0,'totalGoodsMoney'=>0]; | ||||
|         $areaId = input('post.areaId2/d',-1); | ||||
| 		//计算各店铺运费及金额 | ||||
| 		$carts = $this->getQuickCarts(true); | ||||
| 		$cart = current($carts['carts']); | ||||
| 		$data['shops'][$cart['shopId']]['freight'] = 0; | ||||
| 		$data['shops'][$cart['shopId']]['goodsMoney'] = $cart['goodsMoney']; | ||||
| 		$data['totalGoodsMoney'] = $cart['goodsMoney']; | ||||
| 		$data['totalMoney'] += $cart['goodsMoney']; | ||||
| 		//此处放钩子计算商家使用优惠券后的金额-根据优惠券ID计算 | ||||
| 		hook("afterCalculateCartMoney",["data"=>&$data,'carts'=>$carts,'isVirtual'=>true]); | ||||
| 		$data['totalGoodsMoney'] = ($data['totalGoodsMoney']>$data['totalMoney'])?$data['totalMoney']:$data['totalGoodsMoney']; | ||||
|         $data['maxScore'] = 0; | ||||
| 		$data['maxScoreMoney'] = 0; | ||||
| 		$data['useScore'] = 0; | ||||
| 		$data['scoreMoney'] = 0; | ||||
|  | ||||
| 		/*************计算最大可用惠宝************/ | ||||
| 		$maxScoreMoney = (int)($data['totalGoodsMoney'] * HuiScale()); | ||||
| 		$maxScore = WSTScoreToMoney($maxScoreMoney,true); | ||||
| 		//$maxScoreMoney = $data['totalGoodsMoney']; | ||||
| 		//$maxScore = WSTScoreToMoney($data['totalGoodsMoney'],true); | ||||
| 		/***********end*************/ | ||||
| 		//最大可用惠宝不能大于用户惠宝 | ||||
|         $userId = $uId==0?(int)session('WST_USER.userId'):$uId; | ||||
| 		$user = model('users')->getFieldsById($userId,'userScore'); | ||||
| 		if($maxScore>$user['userScore']){ | ||||
| 			$maxScore = $user['userScore']; | ||||
| 			$maxScoreMoney = WSTScoreToMoney($maxScore,true); | ||||
| 		} | ||||
| 		$data['maxScore'] = $maxScore; | ||||
| 		$data['maxScoreMoney'] = $maxScoreMoney; | ||||
|         //判断是否使用惠宝 | ||||
| 		$isUseScore = (int)input('isUseScore'); | ||||
| 		if($isUseScore==1){ | ||||
| 			//不能比用户惠宝还多 | ||||
| 			$useScore = (int)input('useScore'); | ||||
| 			if($useScore>$maxScore)$useScore = $maxScore; | ||||
| 			$data['useScore'] = $useScore; | ||||
|             $data['scoreMoney'] = WSTScoreToMoney($useScore); | ||||
| 		} | ||||
| 		$data['realTotalMoney'] = WSTPositiveNum($data['totalMoney'] - $data['scoreMoney']); | ||||
| 		return WSTReturn('',1,$data); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * 删除购物车商品 | ||||
| 	 */ | ||||
| 	public function delCartByUpdate($goodsId){ | ||||
| 		if(is_array($goodsId)){ | ||||
|             $this->where(['goodsId'=>['in',$goodsId]])->delete(); | ||||
| 		}else{ | ||||
| 			$this->where('goodsId',$goodsId)->delete(); | ||||
| 		} | ||||
| 		 | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * app购物车推荐商品 | ||||
| 	 * uId 会员ID,默认为当前ID | ||||
| 	 */ | ||||
| 	public function recommendGoods($uId=0){ | ||||
| 		$pageSize = (int)input('pageSize/d',10); | ||||
| 		$userId = ($uId==0)?(int)session('WST_USER.userId'):$uId; | ||||
| 		$where = []; | ||||
| 		$where['c.userId'] = $userId; | ||||
| 		//获取购物车产品,包括选中和未选中的 | ||||
| 		$catsGoods = Db::name('carts')->alias('c')->join('__GOODS__ g','c.goodsId=g.goodsId','inner') | ||||
| 		           ->where($where) | ||||
| 		           ->Distinct(true) | ||||
| 		           ->cache(true,60) | ||||
| 		           ->column('g.goodsCatId'); | ||||
| 		$recommendGoods = Db::name('goods')->where(['goodsCatId'=>['in',$catsGoods]])->field('goodsId,goodsName,goodsImg,shopPrice')->order('saleNum desc')->cache(true,60)->paginate($pageSize) | ||||
| 	           ->toArray(); | ||||
| 		return WSTReturn('',1,$recommendGoods); | ||||
| 	} | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user