From 482821f933a93bf0915e345a4c800c9796bb72cb Mon Sep 17 00:00:00 2001 From: root Date: Tue, 16 Aug 2016 20:52:28 +0800 Subject: [PATCH] fecshop admin product image upload sections --- .../Catalog/block/productinfo/Imageupload.php | 59 +++++++ .../Catalog/block/productinfo/Manageredit.php | 71 +++++++- .../controllers/ProductinfoController.php | 5 + .../Catalog/views/productinfo/manageredit.php | 93 ++++++++++- config/services/Product.php | 7 +- services/Image.php | 4 +- services/product/Image.php | 157 ++++++------------ 7 files changed, 280 insertions(+), 116 deletions(-) create mode 100644 app/appadmin/modules/Catalog/block/productinfo/Imageupload.php diff --git a/app/appadmin/modules/Catalog/block/productinfo/Imageupload.php b/app/appadmin/modules/Catalog/block/productinfo/Imageupload.php new file mode 100644 index 00000000..0ad2b328 --- /dev/null +++ b/app/appadmin/modules/Catalog/block/productinfo/Imageupload.php @@ -0,0 +1,59 @@ + + * @since 1.0 + */ +class Imageupload +{ + + public function upload(){ + + if(empty($_FILES)) { + throw new InvalidValueException('$_FILES is empty.'); + } + $index = 0; //$_FILES 以文件name为数组下标,不适用foreach($_FILES as $index=>$file) + $post = Yii::$app->request->post(); + if($post['thisindex']){ + $rel_index = $post['thisindex']; + }else{ + $rel_index = $index ; + } + $img_str = ""; + foreach($_FILES as $FILE){ + $saveImgInfo = Yii::$service->product->image->saveProductUploadImg($FILE); + $rel_index++; + if(is_array($saveImgInfo) && !empty($saveImgInfo) ){ + list($imgSavedRelativePath,$imgUrl) = $saveImgInfo; + $img_str .=' + + + + + 删除 + '; + } + $index++; + } + echo json_encode(array( + "return_status" => "success", + "img_str"=>$img_str, + )); + } +} + + + diff --git a/app/appadmin/modules/Catalog/block/productinfo/Manageredit.php b/app/appadmin/modules/Catalog/block/productinfo/Manageredit.php index 60006d2d..a233208e 100644 --- a/app/appadmin/modules/Catalog/block/productinfo/Manageredit.php +++ b/app/appadmin/modules/Catalog/block/productinfo/Manageredit.php @@ -47,12 +47,13 @@ class Manageredit extends AppadminbaseBlockEdit implements AppadminbaseBlockEdi public function getLastData(){ return [ - 'baseInfo' => $this->getBaseInfo(), - 'metaInfo' => $this->getMetaInfo(), - 'groupAttr'=> $this->getGroupAttr(), + 'baseInfo' => $this->getBaseInfo(), + 'metaInfo' => $this->getMetaInfo(), + 'groupAttr' => $this->getGroupAttr(), 'descriptionInfo' => $this->getDescriptionInfo(), - 'attrGroup' => $this->_attr->getProductAttrGroupSelect(), - 'primaryInfo' => $this->getCurrentProductPrimay(), + 'attrGroup' => $this->_attr->getProductAttrGroupSelect(), + 'primaryInfo' => $this->getCurrentProductPrimay(), + 'img_html' => $this->getImgHtml(), //'editBar' => $this->getEditBar(), //'textareas' => $this->_textareas, //'lang_attr' => $this->_lang_attr, @@ -103,6 +104,66 @@ class Manageredit extends AppadminbaseBlockEdit implements AppadminbaseBlockEdi } + public function getImgHtml(){ + $main_image = [ + 'image' => '/2/14/21471340924755.jpg', + 'label' => 'xxxx', + 'sort_order'=> 1, + ]; + $gallery_image = [ + [ + 'image' => '/1/6/16.jpg', + 'label' => 'xxxx', + 'sort_order'=> 5, + ], + [ + 'image' => '/3/14/31471340924495.jpg', + 'label' => 'yyy', + 'sort_order'=> 6, + ] + ]; + $str = + '
+ + + + + + + + + + + + + + + + + + + '; + if(!empty($gallery_image) && is_array($gallery_image)){ + $i=2; + foreach($gallery_image as $gallery){ + $str .=' + + + + + + '; + $i++; + } + + } + + $str .= ' +
图片labelsort_order主图删除
删除
删除
+
'; + return $str; + } + public function getEditArr(){ diff --git a/app/appadmin/modules/Catalog/controllers/ProductinfoController.php b/app/appadmin/modules/Catalog/controllers/ProductinfoController.php index 7f6365b6..d7bf62d8 100644 --- a/app/appadmin/modules/Catalog/controllers/ProductinfoController.php +++ b/app/appadmin/modules/Catalog/controllers/ProductinfoController.php @@ -35,6 +35,11 @@ class ProductinfoController extends CatalogController $data = $this->getBlock('manageredit')->save(); return $this->render($this->action->id,$data); } + + # catalog + public function actionImageupload(){ + $this->getBlock()->upload(); + } public function actionRemove() diff --git a/app/appadmin/modules/Catalog/views/productinfo/manageredit.php b/app/appadmin/modules/Catalog/views/productinfo/manageredit.php index 9c14ad6a..abaac9f3 100644 --- a/app/appadmin/modules/Catalog/views/productinfo/manageredit.php +++ b/app/appadmin/modules/Catalog/views/productinfo/manageredit.php @@ -45,10 +45,20 @@ $(document).ready(function(){ }); + +function thissubmit(thiss){ + + main_image_image = $('.productimg input:checked').val(); + main_image_label = $('.productimg input:checked').parent().parent().find(".image_label").val(); + main_image_sort_order = $('.productimg input:checked').parent().parent().find(".sort_order").val(); + alert(main_image_image+main_image_label+main_image_sort_order); + return validateCallback(thiss, dialogAjaxDoneCloseAndReflush); + +}
-
+
@@ -88,12 +98,91 @@ $(document).ready(function(){
+ +
+ + + +
+ + + + + + + + +
+
-
diff --git a/config/services/Product.php b/config/services/Product.php index e00ed444..8d647e41 100644 --- a/config/services/Product.php +++ b/config/services/Product.php @@ -71,8 +71,11 @@ return [ # 子服务 'childService' => [ 'image' => [ - 'class' => 'fecshop\services\product\Image', - 'imageFloder' => 'media/catalog/product', + 'class' => 'fecshop\services\product\Image', + 'imageFloder' => 'media/catalog/product', + //'allowImgType' => ['image/jpeg','image/gif','image/png'], + 'maxUploadMSize'=> 5, #MB + ], 'price' => [ 'class' => 'fecshop\services\product\Price', diff --git a/services/Image.php b/services/Image.php index 26881f06..3467c553 100644 --- a/services/Image.php +++ b/services/Image.php @@ -27,9 +27,9 @@ class Image extends Service if($appbase = $this->appbase){ if(isset($appbase[$app]['basedir'])){ if($str){ - return $appbase[$app]['basedir'].'/'.$str; + return Yii::getAlias($appbase[$app]['basedir'].'/'.$str); } - return $appbase[$app]['basedir']; + return Yii::getAlias($appbase[$app]['basedir']); } } } diff --git a/services/product/Image.php b/services/product/Image.php index 312e63dd..fb8530e9 100644 --- a/services/product/Image.php +++ b/services/product/Image.php @@ -21,7 +21,7 @@ class Image extends Service /** * absolute image save floder */ - public $imageFloder; + public $imageFloder = 'media/catalog/product'; /** * upload image max size */ @@ -29,38 +29,50 @@ class Image extends Service /** * allow image type */ - public $allowImgType; + public $allowImgType = [ + 'image/jpeg', + 'image/gif', + 'image/png', + 'image/jpg', + 'image/pjpeg', + ]; + protected $_maxUploadSize; /** - * curent max upload size + * 得到上传图片的最大的size */ - private $_maxUploadSize; - /** - * image absolute floder that can save product image, - * example:/www/web/fecshop/appadmin/web/media/catalog/product - */ - private $_imageBaseFloder; - + protected function actionGetMaxUploadSize(){ + if(!$this->_maxUploadSize){ + if($this->maxUploadMSize){ + $this->_maxUploadSize = $this->maxUploadMSize * 1024 * 1024; + } + } + return $this->_maxUploadSize; + } /** - * image absolute url that product image saved, - * example:http://www.fecshop.com/media/catalog/product + * 得到保存产品图片所在相对根目录的url路径 */ - private $_imageBaseUrl; - + protected function actionGetBaseUrl(){ + return Yii::$service->image->GetImgUrl($this->imageFloder,'common'); + } /** - * default allowed image type ,if not set allowImgType in config file ,this value will be effective. + * 得到保存产品图片所在相对根目录的文件夹路径 */ - private $_defaultAllowImgType = ['image/jpeg','image/gif','image/png']; + protected function actionGetBaseFloder(){ + return Yii::$service->image->GetImgDir($this->imageFloder,'common'); + } /** - * default allow image upload size (MB) ,if not set maxUploadMSize in config file ,this value will be effective. + * 通过产品图片的相对路径得到产品图片的url */ - private $_defaultMaxUploadMSize = 2; #mb + protected function actionGetUrl($str){ + return Yii::$service->image->GetImgUrl($this->imageFloder.$str,'common'); + } /** - * default relative image save floder ,if not set imageFloder in config file ,this value will be effective. + * 通过产品图片的相对路径得到产品图片的绝对路径 */ - private $_defaultImageFloder = 'media/catalog/product'; - //private $_image - + protected function actionGetFilePath(){ + return Yii::$service->image->GetImgDir($this->imageFloder.$str,'common'); + } /** @@ -70,99 +82,34 @@ class Image extends Service * return , if success ,return image saved relative file path , like '/b/i/big.jpg' * if fail, reutrn false; */ - protected function actionSaveProductUploadImg($param_img_file){ - $this->initUploadImage(); - $size = $param_img_file['size']; - $file = $param_img_file['tmp_name']; - $name = $param_img_file['name']; - if($size > $this->_maxUploadSize){ - throw new InvalidValueException('upload image is to max than '.($this->maxUploadSize/(1024*1024))); + protected function actionSaveProductUploadImg($FILE){ + + $size = $FILE['size']; + $file = $FILE['tmp_name']; + $name = $FILE['name']; + if($size > $this->getMaxUploadSize()){ + throw new InvalidValueException('upload image is to max than'. $this->maxUploadMSize.' MB'); + }else if(!($img = getimagesize($file))){ + throw new InvalidValueException('file type is empty.'); + }else if($img = getimagesize($file)){ $imgType = $img['mime']; - if(in_array($imgType,$this->allowImgType)){ - - }else{ + + if(!in_array($imgType,$this->allowImgType)){ throw new InvalidValueException('image type is not allow for '.$imgType); } } // process image name. $imgSavedRelativePath = $this->getImgSavedRelativePath($name); - $isMoved = @move_uploaded_file ( $file, $this->getImageBaseFloder().$imgSavedRelativePath); + $isMoved = @move_uploaded_file ( $file, $this->getBaseFloder().$imgSavedRelativePath); if($isMoved){ - return $imgSavedRelativePath; + $imgUrl = $this->getUrl($imgSavedRelativePath); + $imgPath = $this->getFilePath($imgSavedRelativePath); + return [$imgSavedRelativePath,$imgUrl,$imgPath]; } return false; } - - protected function resize($imgPath,$width='',$height=''){ - - - } - - - - /** - * init Object property. - */ - - protected function initUploadImage(){ - if(!$this->allowImgType){ - $this->allowImgType = $this->_defaultAllowImgType; - } - if(!$this->_maxUploadSize){ - if($this->maxUploadMSize){ - $this->_maxUploadSize = $this->maxUploadMSize * 1024 * 1024; - }else{ - $this->_maxUploadSize = $this->_defaultMaxUploadMSize * 1024 * 1024; - } - } - $this->getImageFloder(); - } - - /** - * Get relative Floder that product image saved. - */ - protected function getImageFloder(){ - if(!$this->imageFloder){ - $this->imageFloder = $this->_defaultImageFloder; - } - } - /** - * Get absolute Floder that product image saved. - */ - protected function getImageBaseFloder(){ - if(!$this->_imageBaseFloder){ - if(!$this->imageFloder) - $this->getImageFloder(); - $this->_imageBaseFloder = Yii::getAlias("@webroot").'/'. $this->imageFloder; - } - return $this->_imageBaseFloder; - } - - - /** - * Get Image base url string that product image saved floder. - */ - protected function getImageBaseUrl(){ - if(!$this->_imageBaseUrl){ - if(!$this->imageFloder) - $this->getImageFloder(); - $this->_imageBaseUrl = Yii::$app->homeUrl.'/'. $this->imageFloder; - } - return $this->_imageBaseUrl; - } - - - - - - - - - - - /** * get Image save file path, if floder is not exist, this function will create floder. * if image file is exsit , image file name will be change to a not existed file name( by add radom string to file name ). @@ -179,7 +126,7 @@ class Image extends Service $first_str = substr($imgName,0,1); $two_str = substr($imgName,1,2); - $imgSaveFloder = CDir::createFloder($this->getImageBaseFloder(),[$first_str,$two_str]); + $imgSaveFloder = CDir::createFloder($this->getBaseFloder(),[$first_str,$two_str]); if($imgSaveFloder){ $imgName = $this->getUniqueImgNameInPath($imgSaveFloder,$imgName,$imgType); $relative_floder = '/'.$first_str.'/'.$two_str.'/'; @@ -201,7 +148,7 @@ class Image extends Service if(!file_exists($imagePath)){ return $name.$randStr.'.'.$imageType;; }else{ - $randStr = time().rand(100,999); + $randStr = time().rand(10000,99999); return $this->getUniqueImgNameInPath($imgSaveFloder,$name,$imageType,$randStr); } } -- GitLab