1 maxSize = 1024*1024 ;// 设置附件上传大小 20 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 21 $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录 22 $upload->savePath = 'goods/'; // 设置附件上传(子)目录 23 // 上传文件 24 $info = $upload->upload(); 25 // var_dump($info);die; 26 if(!$info) { // 上传错误提示错误信息 27 $this->error=$upload->getError(); 28 return false; 29 }else{ // 上传成功 30 //原图片路径 31 $logo = $info['logo']['savepath'].$info['logo']['savename']; 32 //缩略图路径 33 $mbig_logo = $info['logo']['savepath'].'mbig_'.$info['logo']['savename']; 34 $big_logo = $info['logo']['savepath'].'big_'.$info['logo']['savename']; 35 $mid_logo = $info['logo']['savepath'].'mid_'.$info['logo']['savename']; 36 $sm_logo = $info['logo']['savepath'].'sm_'.$info['logo']['savename']; 37 $image = new \Think\Image(); 38 $image->open('./Public/Uploads/'.$logo); 39 // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg 40 $image->thumb(700,700)->save('./Public/Uploads/'.$mbig_logo); 41 $image->thumb(300,300)->save('./Public/Uploads/'.$big_logo); 42 $image->thumb(150,150)->save('./Public/Uploads/'.$mid_logo); 43 $image->thumb(50,50)->save('./Public/Uploads/'.$sm_logo); 44 $data['logo'] = $logo; 45 $data['mbig_logo'] = $mbig_logo; 46 $data['big_logo'] = $big_logo; 47 $data['mid_logo'] = $mid_logo; 48 $data['sm_logo'] = $sm_logo; 49 } 50 } 51 $data['addtime'] = date('Y-m-d H:i:s'); 52 $data['goods_desc'] = removeXSS($data['goods_desc']); 53 } 54 55 protected function _after_insert($data, $options){ 56 $id = $data['id']; 57 /******************商品属性*********************/ 58 $goodsattr = I('post.attr_value_box'); 59 $gaModel = D('goods_attr'); 60 if($goodsattr){ 61 foreach ($goodsattr as $k=>$v){ 62 $v = array_unique($v); 63 foreach ($v as $k1=>$v1){ 64 if($v1=='') 65 continue; 66 $gaModel->add(array( 67 'attr_id'=>$k, 68 'goods_id'=>$id, 69 'attr_value'=>$v1 70 )); 71 } 72 } 73 } 74 75 /******************扩展分类******************/ 76 $cbModle = D('category_box'); 77 $cib = I('post.cat_id_box'); 78 $cib = array_unique($cib); 79 foreach ($cib as $k=>$v){ 80 if($v==0) 81 continue; 82 $cbModle->add(array( 83 'cat_id'=>$v, 84 'goods_id'=>$id, 85 )); 86 } 87 88 /***************商品相册*********************/ 89 $gpModle = D('goods_pic'); 90 if($_FILES['pic']){ 91 $pic = array(); 92 foreach ($_FILES['pic']['error'] as $k=>$v){ 93 if($v != 0) 94 continue; 95 $pic[] = array( 96 'name'=>$_FILES['pic']['name'][$k], 97 'type'=>$_FILES['pic']['type'][$k], 98 'tmp_name'=>$_FILES['pic']['tmp_name'][$k], 99 'error'=>$v,100 'size'=>$_FILES['pic']['size'][$k],101 );102 }103 $_FILES = $pic;//$pic传得是文件;104 foreach ($pic as $k=>$v){105 $upload = new \Think\Upload();// 实例化上传类106 $upload->maxSize = 1024*1024 ;// 设置附件上传大小107 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型108 $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录109 $upload->savePath = 'goods_pic/'; // 设置附件上传(子)目录110 // 上传文件111 $info = $upload->upload(array($k=>$_FILES[$k]));112 if(!$info) { // 上传错误提示错误信息113 $this->error=$upload->getError();114 return false;115 }else{ // 上传成功116 //原图片路径117 $pic = $info[$k]['savepath'].$info[$k]['savename'];118 //缩略图路径119 $big_pic = $info[$k]['savepath'].'big_'.$info[$k]['savename'];120 $mid_pic = $info[$k]['savepath'].'mid_'.$info[$k]['savename'];121 $sm_pic = $info[$k]['savepath'].'sm_'.$info[$k]['savename'];122 $image = new \Think\Image();123 $image->open('./Public/Uploads/'.$pic);124 // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg125 $image->thumb(700,700)->save('./Public/Uploads/'.$big_pic);126 $image->thumb(300,300)->save('./Public/Uploads/'.$mid_pic);127 $image->thumb(50,50)->save('./Public/Uploads/'.$sm_pic);128 $gpModle->add(array(129 'goods_id'=>$id,130 'pic'=>$pic,131 'sm_pic'=>$sm_pic,132 'mid_pic'=>$mid_pic,133 'big_pic'=>$big_pic,134 ));135 }136 }137 }138 139 /***********会员价格*************/140 $plData = I('post.price_level');141 $mpModel = D('member_price');142 if($plData){143 foreach ($plData as $k=>$v){144 if($v<=0)145 continue;146 $mpModel->add(array(147 'price'=>$v,148 'level_id'=>$k,149 'goods_id'=>$id150 ));151 }152 }153 154 }155 156 protected function _before_update(&$data, $options){157 $id = $options['where']['id'];158 /******************商品属性******************/159 $goodsattr = I('post.attr_value_box');160 $goodsAttrId = I('post.goods_attr_id');161 $gaModel = D('goods_attr');162 $num = 0;163 if($goodsattr){164 foreach ($goodsattr as $k=>$v){165 foreach ($v as $k1=>$v1){166 if($goodsAttrId[$num]==""){167 $gaModel->add(array(168 'goods_id'=>$id,169 'attr_id'=>$k,170 'attr_value'=>$v1,171 ));172 }else{173 $gaModel->where(array(174 'id'=>$goodsAttrId[$num],175 ))->setField('attr_value',$v1);176 }177 $num++;178 }179 }180 }181 182 /******************扩展分类******************/183 $cbModle = D('category_box');184 $cib = I('post.cat_id_box');185 if($cib){186 $cbModle->where(array(187 'goods_id'=>$id,188 ))->delete();189 190 $cib = array_unique($cib);191 foreach ($cib as $k=>$v){192 if($v==0)193 continue;194 $cbModle->add(array(195 'cat_id'=>$v,196 'goods_id'=>$id,197 ));198 }199 }200 /***************相册**********************/201 $gpModle = D('goods_pic');202 if($_FILES['pic']){203 $pic = array();204 foreach ($_FILES['pic']['error'] as $k=>$v){205 if($v != 0)206 continue;207 $pic[] = array(208 'name'=>$_FILES['pic']['name'][$k],209 'type'=>$_FILES['pic']['type'][$k],210 'tmp_name'=>$_FILES['pic']['tmp_name'][$k],211 'error'=>$v,212 'size'=>$_FILES['pic']['size'][$k],213 );214 }215 $_FILES = $pic;//$pic传得是文件;216 foreach ($pic as $k=>$v){217 $upload = new \Think\Upload();// 实例化上传类218 $upload->maxSize = 1024*1024 ;// 设置附件上传大小219 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型220 $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录221 $upload->savePath = 'goods_pic/'; // 设置附件上传(子)目录222 // 上传文件223 $info = $upload->upload(array($k=>$_FILES[$k]));224 if(!$info) { // 上传错误提示错误信息225 $this->error=$upload->getError();226 return false;227 }else{ // 上传成功228 //原图片路径229 $pic = $info[$k]['savepath'].$info[$k]['savename'];230 //缩略图路径231 $big_pic = $info[$k]['savepath'].'big_'.$info[$k]['savename'];232 $mid_pic = $info[$k]['savepath'].'mid_'.$info[$k]['savename'];233 $sm_pic = $info[$k]['savepath'].'sm_'.$info[$k]['savename'];234 $image = new \Think\Image();235 $image->open('./Public/Uploads/'.$pic);236 // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg237 $image->thumb(700,700)->save('./Public/Uploads/'.$big_pic);238 $image->thumb(300,300)->save('./Public/Uploads/'.$mid_pic);239 $image->thumb(50,50)->save('./Public/Uploads/'.$sm_pic);240 $gpModle->add(array(241 'goods_id'=>$id,242 'pic'=>$pic,243 'sm_pic'=>$sm_pic,244 'mid_pic'=>$mid_pic,245 'big_pic'=>$big_pic,246 ));247 }248 }249 }250 251 252 253 /***************相片**********************/254 if($_FILES['logo']['error']==0){255 $upload = new \Think\Upload();// 实例化上传类256 $upload->maxSize = 1024*1024 ;// 设置附件上传大小257 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型258 $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录259 $upload->savePath = 'goods/'; // 设置附件上传(子)目录260 // 上传文件261 $info = $upload->upload();262 // var_dump($info);die;263 if(!$info) { // 上传错误提示错误信息264 $this->error=$upload->getError();265 return false;266 }else{ // 上传成功267 //原图片路径268 $logo = $info['logo']['savepath'].$info['logo']['savename'];269 //缩略图路径270 $mbig_logo = $info['logo']['savepath'].'mbig_'.$info['logo']['savename'];271 $big_logo = $info['logo']['savepath'].'big_'.$info['logo']['savename'];272 $mid_logo = $info['logo']['savepath'].'mid_'.$info['logo']['savename'];273 $sm_logo = $info['logo']['savepath'].'sm_'.$info['logo']['savename'];274 $image = new \Think\Image();275 $image->open('./Public/Uploads/'.$logo);276 // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg277 $image->thumb(700,700)->save('./Public/Uploads/'.$mbig_logo);278 $image->thumb(300,300)->save('./Public/Uploads/'.$big_logo);279 $image->thumb(150,150)->save('./Public/Uploads/'.$mid_logo);280 $image->thumb(50,50)->save('./Public/Uploads/'.$sm_logo);281 $oldLogo = $this->field('logo,sm_logo,mid_logo,big_logo,mbig_logo')->find($id);282 unlink('./Public/Uploads/'.$oldLogo['logo']);283 unlink('./Public/Uploads/'.$oldLogo['sm_logo']);284 unlink('./Public/Uploads/'.$oldLogo['mid_logo']);285 unlink('./Public/Uploads/'.$oldLogo['big_logo']);286 unlink('./Public/Uploads/'.$oldLogo['mbig_logo']);287 $data['logo'] = $logo;288 $data['mbig_logo'] = $mbig_logo;289 $data['big_logo'] = $big_logo;290 $data['mid_logo'] = $mid_logo;291 $data['sm_logo'] = $sm_logo;292 }293 }294 295 /***************会员价格**********************/296 $mpModel = D('member_price');297 $plData = I('post.price_level');298 if($plData){299 $mpModel->where(array(300 'goods_id'=>$id,301 ))->delete();302 foreach ($plData as $k=>$v){303 if($v<=0)304 continue;305 $mpModel->add(array(306 'price'=>$v,307 'level_id'=>$k,308 'goods_id'=>$id309 ));310 }311 }312 313 314 315 /***************时间和描述XSS**********************/316 $data['goods_desc'] = removeXSS($data['goods_desc']);317 }318 319 /************一个分类id下包含子分类扩展分类所有的商品id*******/320 public function getCatGOodsId($catId){321 $catModel = D('Admin/Category');322 $cats = $catModel->getchildCate($catId);//取出所有的子分类的id323 $cats[] = $catId;324 //取出商品id325 $gId = $this->field('id')->where(array(326 'cat_id'=>array('in',$cats),327 ))->select();328 //取出扩展分类329 $cbModel = D('category_box');330 $gIds = $cbModel->field('distinct goods_id id')->where(array(331 'cat_id'=>array('in',$cats),332 ))->select();333 if($gId && $gIds){334 $gId = array_merge($gId,$gIds);335 }elseif ($gIds){336 $gId = $gIds;337 }338 $arr = array();339 foreach ($gId as $k=>$v){340 if(!in_array($v['id'], $arr))341 $arr[] = $v['id'];342 }343 return $arr;344 }345 protected function _before_delete($options){346 $id = $options['where']['id'];347 /*****************库存量************************/348 $gnModel = D('goods_number');349 $gnModel->where(array(350 'goods_id'=>$id,351 ))->delete();352 /*****************商品属性************************/353 $gaModel = D('goods_attr');354 $gaModel->where(array(355 'goods_id'=>$id,356 ))->delete();357 358 /*****************扩展分类************************/359 $cbModle = D('category_box');360 $cbModle->where(array(361 'goods_id'=>$id,362 ))->delete();363 /***************商品相册*********************/364 $gpModle = D('goods_pic');365 $oldPic = $gpModle->field('pic,sm_pic,mid_pic,big_pic')366 ->where(array(367 'goods_id'=>$id,368 ))->select();369 foreach ($oldPic as $k=>$v){370 unlink('./Public/Uploads/'.$v['pic']);371 unlink('./Public/Uploads/'.$v['sm_pic']);372 unlink('./Public/Uploads/'.$v['mid_pic']);373 unlink('./Public/Uploads/'.$v['big_pic']);374 }375 $gpModle->where(array(376 'goods_id'=>$id,377 ))->delete();378 379 /***************会员价格**********************/380 $mpModel = D('member_price');381 $mpModel->where(array(382 'goods_id'=>$id,383 ))->delete();384 /***************相片**********************/385 $oldLogo = $this->field('logo,sm_logo,mid_logo,big_logo,mbig_logo')->find($id);386 unlink('./Public/Uploads/'.$oldLogo['logo']);387 unlink('./Public/Uploads/'.$oldLogo['sm_logo']);388 unlink('./Public/Uploads/'.$oldLogo['mid_logo']);389 unlink('./Public/Uploads/'.$oldLogo['big_logo']);390 unlink('./Public/Uploads/'.$oldLogo['mbig_logo']);391 }392 393 public function search($pagecount=5){394 $where = array();395 //商品名称396 $gn = I('get.gn');397 if($gn)398 $where['a.goods_name'] = array('like',"%$gn%");399 //价格400 $pt = I('get.pt');401 $pf = I('get.pf');402 if($pt && $pf)403 $where['a.shop_price'] = array('between',array($pt,$pf));404 elseif($pt)405 $where['a.shop_price'] = array('egt',$pt);406 elseif ($pf)407 $where['a.shop_price'] = array('elt',$pf);408 //时间409 $tt = I('get.tt');410 $tf = I('get.tf');411 if($tt && $tf)412 $where['a.addtime'] = array('between',array($tt,$tf));413 elseif ($tt)414 $where['a.addtime'] = array('eqt',$tt);415 elseif ($tf)416 $where['a.addtime'] = array('elt',$tf);417 //上下架418 $ios = I('get.ios');419 if($ios)420 $where['a.is_on_sale'] = array('eq',$ios);421 //品牌422 $bn = I('get.bn');423 if($bn)424 $where['a.brand_id'] = array('eq',$bn);425 //分类426 $catId = I('get.cat_id');427 // var_dump($catId);die;428 if($catId>0){429 $goodsId = $this->getCatGOodsId($catId);430 $where['a.id'] = array('in',$goodsId);431 }432 433 //排序434 $paixu = I('get.paixu');435 $name = 'id';436 $pai = 'desc';437 if($paixu){438 if($paixu=='paixuIdasc')439 $pai = 'asc';440 elseif ($paixu == 'paixuPricedesc')441 $name = 'shop_price';442 elseif ($paixu == 'paixuPriceasc'){443 $name = 'shop_price';444 $pai = 'asc';445 }446 }447 448 $count = $this->alias('a')->where($where)->count();// 查询满足要求的总记录数449 $Page = new \Think\Page($count,$pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)450 $Page->setConfig('next','下一页');451 $Page->setConfig('prev','上一页');452 $show = $Page->show();// 分页显示输出453 // 进行分页数据查询 注意limit方法的参数要使用Page类的属性454 $list = $this->where($where)->limit($Page->firstRow.','.$Page->listRows)455 ->alias('a')456 ->field('a.*,b.brand_name,c.cat_name,GROUP_CONCAT(e.cat_name) cat_name_box')457 ->join('LEFT JOIN __BRAND__ b on a.brand_id=b.id')458 ->join('LEFT JOIN __CATEGORY__ c on a.cat_id=c.id')459 ->join('LEFT JOIN __CATEGORY_BOX__ d on a.id=d.goods_id')460 ->join('LEFT JOIN __CATEGORY__ e on e.id=d.cat_id')461 ->order("$name $pai")462 ->group('a.id')463 ->select();464 return array(465 'data' =>$list,466 'page'=>$show);467 }468 469 470 //取得促销价格信息;471 public function zhuxiaoInfo(){472 $date = date('Y-m-d H:i:s');473 $data = $this->field('promote_price,goods_name,mid_logo,id')->where(array(474 'promote_start_date'=>array('elt',$date),475 'promote_end_date'=>array('egt',$date),476 'promote_price'=>array('gt',0),477 'is_on_sale'=>'是'478 ))->order('sord asc')->limit(5)->select();479 return $data;480 }481 482 483 484 //取得热销信息485 public function horBeseInfo($bnt){486 $data = $this->field('shop_price,goods_name,mid_logo,id')->where(array(487 'shop_price'=>array('gt',0),488 "$bnt"=>array('eq','是'),489 'is_on_sale'=>'是'490 ))->order('sord asc')->limit(5)->select();491 return $data;492 }493 494 495 //计算最终价格496 public function besePriceInfo($goods_id){497 $level = session('level');498 $date = date('Y-m-d H:i:s');499 $promote_price = $data = $this->field('promote_price')->where(array(500 'promote_start_date'=>array('elt',$date),501 'promote_end_date'=>array('egt',$date),502 'promote_price'=>array('gt',0),503 'goods_id'=>$goods_id,504 'is_on_sale'=>'是'505 ))->find();506 //有没有登录507 if($level){508 //会员价格 登录了才有会员价格509 $mpMdoel = D('member_price');510 $mpData = $mpMdoel->where(array(511 'goods_id'=>$goods_id,512 'level_id'=>$level,513 'price'=>array('gt',0)514 ))->find();515 if($mpData){516 return min($promote_price['promote_price'],$mpData['price']);517 }else{518 $gData = $this->field('shop_price')->where(array(519 'id'=>$goods_id,520 ))->find();521 return min($promote_price['promote_price'],$gData['shop_price']);522 }523 }else{524 $gData = $this->field('shop_price')->where(array(525 'id'=>$goods_id,526 ))->find();527 if($promote_price['promote_price'])528 return min($promote_price['promote_price'],$gData['shop_price']);529 else 530 return $gData['shop_price'];531 }532 }533 534 535 public function getSearchInfo($Carid,$pagecount=4){536 $GoodsId = $this->getCatGOodsId($Carid);//所有的商品id537 $where['a.id']=array('in',$GoodsId);538 /*************************品牌***************************/539 $brandId = I('get.brand_id');540 if($brandId){541 $bid = strstr($brandId, '-');542 $bId = str_replace($bid, '', $brandId);543 // $bId = (int)$brandId;544 $where['a.brand_id'] = array('eq',$bId);545 }546 /*************************价格***************************/547 $price = I('get.price');548 if($price){549 $pri = explode('-', $price);550 $where['a.shop_price'] = array('between',array($pri[0],$pri[1]));551 }552 /*************************属性***************************/553 $array = null;554 foreach ($_GET as $k=>$v){555 if(strpos($k,'attr_')===0){556 //属性id557 $attId = str_replace('attr_', '',$k);558 //属性值559 $attr_value = str_replace(strrchr($v,'-'),'',$v);560 561 $gaModel = D('goods_attr');562 $id = $gaModel->field('GROUP_CONCAT(goods_id) gids')->where(array(563 'attr_value'=>$attr_value,564 'attr_id'=>$attId,565 ))->find();566 //如果有商品id567 if($id['gids']){568 $id['gids'] = explode(',', $id['gids']);569 if($array == null)570 $array = $id['gids'];571 else{572 $array = array_intersect($array, $id['gids']);573 if(empty($array)){574 $where['a.id'] = array('eq',0);575 break;576 }577 }578 }else{579 $array = array();580 $where['a.id'] = array('eq',0);581 break;582 }583 }584 }585 if($array)586 $where['a.id'] = array('IN',$array);587 588 /***********************分页*****************************/589 $count = $this->alias('a')->where($where)->count();// 查询满足要求的总记录数590 $Page = new \Think\Page($count,$pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)591 $Page->setConfig('next','下一页');592 $Page->setConfig('prev','上一页');593 $data['pate'] = $Page->show();// 分页显示输出594 /***********************数据*****************************/595 $data['data'] = $this->alias('a')596 ->field('a.id,a.goods_name,a.shop_price,a.mid_logo,a.id,sum(b.goods_number) X1')597 ->join('LEFT JOIN __ORDER_GOODS__ b 598 on (a.id=b.goods_id 599 and 600 b.order_id in(select id from guo_order where pay_status="是"))')601 ->group('a.id')602 ->order('X1 desc')603 ->where($where)->limit($Page->firstRow.','.$Page->listRows)604 ->select();605 return $data;606 }607 608 }