AdminEquipmentController.php 9.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/20
 * Time: 16:33
 */

namespace app\portal\controller;


use app\portal\model\EquipmentModel;
use cmf\controller\AdminBaseController;
use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Style_Alignment;
use PHPExcel_Worksheet_Drawing;
use PHPExcel_Writer_Excel2007;
use QRcode;
use think\Db;

/**
 * Class AdminEquipmentController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'设备管理',
 *     'action' =>'index',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'设备管理'
 * )
 */
class AdminEquipmentController extends AdminBaseController{

    public function index(){
        $Equipment=new EquipmentModel();
        $data=$Equipment->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function add(){

        return $this->fetch();
    }

    public function addPost(){
        $data=$this->request->post();
        $data['create_time']=strtotime($data['create_time']);
        $data['qr_code'] = $this->createQrCode($data['mac_address']);
        $Equipment=new EquipmentModel();
        $res=$Equipment->save($data);
        if ($res){
            $this->apiResponse(200,'保存成功');
        }else{
            $this->apiResponse(301,'error');
        }
    }

    //批量导入设备信息
    public function batch_add(){

        return $this->fetch();
    }
    //批量导入设备信息
    public function batch_addPost(){
        //上传excel文件
        $file = $this->request->file('myfile');
        //移到/public/uploads/excel/下
        $info = $file->move(ROOT_PATH.'public'.DS.'upload'.DS.'excel');
        //上传文件成功
        if($info) {
            //引入PHPExcel类
            vendor('VENDOR_PATH/phpoffice/phpexcel/Classes/PHPExcel.php');
            vendor('VENDOR_PATH/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php');
            //获取上传后的文件名
            $fileName = $info->getSaveName();
            //文件路径
            $filePath = ROOT_PATH.'public/upload/excel/'.$fileName;
            //实例化PHPExcel类
            //使用 PHPExcel_IOFactory 来鉴别文件应该使用哪一个读取类
            $inputFileType = PHPExcel_IOFactory::identify($filePath);
            //实例化类
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            //$PHPReader = new \PHPExcel_Reader_Excel2007();
            //读取excel文件
            $objPHPExcel = $objReader->load($filePath);
            //读取excel文件中的第一个工作表
            $sheet = $objPHPExcel->getSheet(0);
            $allRow = $sheet->getHighestRow();  //取得总行数
//            $testInfo=new TestinfoModel();
            //从第二行开始插入,第一行是列名
            for ($j = 2; $j <= $allRow; $j++) {
                $data=[
                    'serial_number'=> $objPHPExcel->getActiveSheet()->getCell("a" . $j)->getValue(),
                    'mac_address' => $objPHPExcel->getActiveSheet()->getCell("b" . $j)->getValue(),
                    'name' => $objPHPExcel->getActiveSheet()->getCell("c" . $j)->getValue(),
                    'hospital' => $objPHPExcel->getActiveSheet()->getCell("d" . $j)->getValue(),
                    'note' => $objPHPExcel->getActiveSheet()->getCell("e" . $j)->getValue(),
                    'create_time' => time()
                ];
                $data['qr_code'] = $this->createQrCode($data['mac_address']);
                $Equipment=new EquipmentModel();
                try{
                    $Equipment->save($data);
                    echo "第" . $j . "行数据导入成功!<br/>";
                }catch (\Exception $exception){
                    echo "第" . $j . "行数据导入失败!错误信息:".$exception->getMessage()."<br/>";
                }
            }
        }else{
            echo "上传文件失败!";
        }
    }

    //批量导出
    public function batch_export(){
        $data= Db::name('equipment')->select();

        // 导出Exl
        vendor("VENDOR_PATH/phpoffice/phpexcel/Classes/PHPExcel");
        vendor("VENDOR_PATH/phpoffice/phpexcel/Classes/PHPExcel/Worksheet/Drawing");
        vendor("VENDOR_PATH/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel2007");
        $objPHPExcel = new PHPExcel();

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

        $objActSheet = $objPHPExcel->getActiveSheet();

        // 水平居中(位置很重要,建议在最初始位置)
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objActSheet->setCellValue('A1', '商品货号');
        $objActSheet->setCellValue('B1', '商品名称');
        $objActSheet->setCellValue('C1', '商品图');
        $objActSheet->setCellValue('D1', '商品条码');
        $objActSheet->setCellValue('E1', '商品属性');
        $objActSheet->setCellValue('F1', '报价(港币)');
        // 设置个表格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(80);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);

        // 垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k, $v['mac_address']);
            $objActSheet->setCellValue('B'.$k, $v['name']);

            $img = Db::name('equipment')->where('id',$v['id'])->find();
            // 图片生成
            $objDrawing[$k] = new PHPExcel_Worksheet_Drawing();
            $objDrawing[$k]->setPath('./upload/'.$img['qr_code']);
            // 设置宽度高度
            $objDrawing[$k]->setHeight(80);//照片高度
            $objDrawing[$k]->setWidth(80); //照片宽度
            /*设置图片要插入的单元格*/
            $objDrawing[$k]->setCoordinates('C'.$k);
            // 图片偏移距离
            $objDrawing[$k]->setOffsetX(12);
            $objDrawing[$k]->setOffsetY(12);
            $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());

            // 表格内容
            $objActSheet->setCellValue('D'.$k, $v['hospital']);
            $objActSheet->setCellValue('E'.$k, $v['key']);
            $objActSheet->setCellValue('F'.$k, $v['note']);

            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(80);

        }

        $fileName = '报价表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        // END
    }

    //生成二维码
    public function createQrCode($value){
        import('phpqrcode.qrlib',VENDOR_PATH,'.php');
        $name=date('YmdHis').rand(1000,9999);
        $filename='qrCode/'.$name.'.png';
        $filePath = 'upload/'.$filename; // 生成的文件名
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 6;//生成图片大小
        QRcode::png($value,$filePath, $errorCorrectionLevel, $matrixPointSize, 2);
        return $filename;
    }

}