ソースを参照

条形码下载

林琦海 4 年 前
コミット
24c20d5449

+ 10 - 0
app-ghs/controllers/ProductController.php

@@ -713,4 +713,14 @@ class ProductController extends BaseController
         util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
     }
 
+    //下载条形码 linqh 20211227
+    public function actionGenerateBarcodeTable()
+    {
+        $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId);
+        $file = $respond['file'] ?? '';
+        $shortFile = $respond['shortFile'] ?? '';
+        $fileUrl = Yii::$app->params['ghsHost'] . $file;
+        util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
+    }
+
 }

+ 110 - 0
biz-ghs/product/services/ProductService.php

@@ -7,6 +7,8 @@ use bizGhs\base\services\BaseService;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\item\classes\ItemClassClass;
 use bizGhs\product\classes\ProductClass;
+use common\components\barcodeUtil;
+use common\components\dirUtil;
 use common\services\xhItemService;
 use Yii;
 
@@ -235,4 +237,112 @@ class ProductService extends BaseService
         return ['file' => '/priceTable/' . $sjId . '/' . $file, 'shortFile' => $file];
     }
 
+    //生成条形码 linqh 20211227
+    public static function generateBarcodeTable($sjId, $shopId)
+    {
+        $classIds = ItemClassClass::getGhsItemClassAll($sjId);
+        $where['sjId'] = $sjId;
+        $where['shopId'] = $shopId;
+        $where['delStatus'] = 0;
+        $product = ProductClass::getAllByCondition($where,null,"*");
+        $data = [];
+        foreach ($product as $v){
+            $tmp = [];
+            $tmp['id'] = $v['id'];
+            $tmp['name'] = $v['name'];
+            $data[$v['classId']][] = $tmp;
+        }
+        $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
+        require_once($phpExcelFile . 'Classes/PHPExcel.php');
+
+        $objPHPExcel = new \PHPExcel();
+        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
+            ->setLastModifiedBy("Maarten Balliauw")
+            ->setTitle("Office 2007 XLSX Test Document")
+            ->setSubject("Office 2007 XLSX Test Document")
+            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
+            ->setKeywords("office 2007 openxml php")
+            ->setCategory("Test result file");
+
+        //一行五列
+        $column_start = "A";
+        $column_end = "F";
+        $title_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
+            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH'];
+        $i = 1;
+        $page = 0;
+        $objPHPExcel->setActiveSheetIndex($page);
+        foreach ($classIds as $k=>$v){
+            //if(($i%30==1|| $i%30==2 || $i%30==3 || $i%30==0  ) && $i >=30){
+            if($i >=30){
+                $i = 1;
+                $page ++;
+                $objPHPExcel->createSheet();
+                $objPHPExcel->setActiveSheetIndex($page);
+            }
+            $productData = $data[$v['id']]??[];
+            if(empty($productData)){
+                continue;
+            }
+            $cate_name = $v['name']??'';
+            $objPHPExcel->getActiveSheet()->mergeCells("{$column_start}{$i}:{$column_end}{$i}")->setCellValue("A".$i,$cate_name);
+            //居中
+            $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
+            //加粗
+            $objPHPExcel->getActiveSheet()->getStyle("{$column_start}{$i}:{$column_end}{$i}")->getFont()->setSize(18)->setBold(true);
+            $i ++;
+
+            if($productData){
+                $newChunk = array_chunk($productData,6); //6个一组
+                foreach ($newChunk as $c){
+
+                    foreach ($c as $ck=>$cv){
+                        $objPHPExcel->getActiveSheet()->setCellValue($title_array[$ck] . $i, $cv['name']);
+                        //居中
+                        $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
+                    }
+                    $i++;
+                    foreach ($c as $ck=>$cv){
+                        $objDrawing = new \PHPExcel_Worksheet_Drawing;
+                        $p =  dirUtil::getBarcodeDir().$cv['id'].".jpg";
+                        if(!file_exists($p)){
+                            //生成条形码
+                            barcodeUtil::generatorProductBarcode($cv['id']);
+                        }
+                        $objDrawing->setPath($p);
+                        //$objDrawing->setHeight(130);//设置图片高度
+                        $objDrawing->setOffsetX(12);//设置图片水平偏移
+                        //$objDrawing->setOffsetY(1);//设置图片垂直偏移
+                        $objPHPExcel->getActiveSheet()->getColumnDimension($title_array[$ck])->setWidth(15);
+                        $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(30);
+                        $objPHPExcel->getActiveSheet()->getStyle("{$title_array[$ck] }{$i}:{$title_array[$ck] }{$i}")->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
+                        $objDrawing->setCoordinates($title_array[$ck] . ($i));
+                        $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
+                        //居中
+                        $objPHPExcel->getActiveSheet()->getStyle("$title_array[$ck]$i")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
+
+                    }
+                    $i++;
+
+                }
+            }
+        }
+        $dir = './barcodeTable/' . $shopId;
+        if (file_exists($dir) == false) {
+            mkdir($dir, 0777, true);
+        }
+        //$date = date("m-d") . "-" . $levelName . "-编号" . $level;
+        $date = date("m-d");
+
+        $file = $date . '.xlsx';
+        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
+
+        if (file_exists($dir . '/' . $file)) {
+            unlink($dir . '/' . $file);
+        }
+        $objWriter->save($dir . '/' . $file);
+
+        return ['file' => '/barcodeTable/' . $shopId . '/' . $file, 'shortFile' => $file];
+    }
+
 }

+ 25 - 2
common/components/barcodeUtil.php

@@ -9,6 +9,18 @@ namespace common\components;
 
 class barcodeUtil
 {
+
+    public static function generatorProductBarcode($productId)
+    {
+        $barcode = barcodeUtil::barcodeGeneratorJPG($productId);
+        $path =  dirUtil::getBarcodeDir();
+        if (file_exists($path) == false) {
+            mkdir($path, 0777, true);
+        }
+        $file =  $path.$productId.".jpg";
+        file_put_contents($file,$barcode);
+    }
+
     // html eg:  echo barcodeUtil::barcodeGeneratorHTML("123");
     public static function barcodeGeneratorHTML($code)
     {
@@ -17,10 +29,21 @@ class barcodeUtil
     }
 
     //png
-    public static function  barcodeGeneratorPNG($code, $widthFactor = 2,$height = 30)
+    public static function  barcodeGeneratorPNG($code, $widthFactor = 1.5,$height = 38)
     {
         $generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
-        $barcode = $generator->getBarcode($code, $generator::TYPE_CODE_128,$widthFactor,$height);
+        $barcode = $generator->getBarcodeDiy($code, $generator::TYPE_CODE_128,$widthFactor,$height);
         return $barcode;
     }
+
+    //jpg
+    public static function  barcodeGeneratorJPG($code, $widthFactor = 1.5,$height = 38)
+    {
+        $generator = new \Picqer\Barcode\BarcodeGeneratorJPG();
+        $barcode = $generator->getBarcodeDiy($code, $generator::TYPE_CODE_128,$widthFactor,$height);
+        return $barcode;
+    }
+
+
+
 }

+ 7 - 1
common/components/dirUtil.php

@@ -24,4 +24,10 @@ class dirUtil
         return Yii::getAlias('@vendor') . '/../../resource/images/';
     }
 
-}
+    //条形码存储地方
+    public static function getBarcodeDir()
+    {
+        return Yii::getAlias('@vendor') . '/../../resource/images/barcode/';
+    }
+
+}

+ 44 - 0
vendor/picqer/php-barcode-generator/src/BarcodeGeneratorPNG.php

@@ -93,6 +93,50 @@ class BarcodeGeneratorPNG extends BarcodeGenerator
         return ob_get_clean();
     }
 
+    public function getBarcodeDiy($barcode, $type,  $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0])
+    {
+        $barcodeData = $this->getBarcodeData($barcode, $type);
+        $width = round($barcodeData->getWidth() * $widthFactor);
+
+        if ($this->useImagick) {
+            $imagickBarsShape = new imagickdraw();
+            $imagickBarsShape->setFillColor(new imagickpixel('rgb(' . implode(',', $foregroundColor) .')'));
+        } else {
+            $image = $this->createGdImageObject($width, $height);
+            $gdForegroundColor = imagecolorallocate($image, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]);
+        }
+
+        // print bars
+        $positionHorizontal = 0;
+        /** @var BarcodeBar $bar */
+        foreach ($barcodeData->getBars() as $bar) {
+            $barWidth = round(($bar->getWidth() * $widthFactor), 3);
+
+            if ($bar->isBar() && $barWidth > 0) {
+                $y = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3);
+                $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3);
+
+                // draw a vertical bar
+                if ($this->useImagick && isset($imagickBarsShape)) {
+                    $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight));
+                } else {
+                    imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor);
+                }
+            }
+            $positionHorizontal += $barWidth;
+        }
+
+        if ($this->useImagick && isset($imagickBarsShape)) {
+            $image = $this->createImagickImageObject($width, $height);
+            $image->drawImage($imagickBarsShape);
+            return $image->getImageBlob();
+        }
+
+        ob_start();
+        $this->generateGdImage($image);
+        return ob_get_clean();
+    }
+
     protected function createGdImageObject(int $width, int $height)
     {
         $image = imagecreate($width, $height);