AdminEquipmentController.php
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?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_IOFactory;
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 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;
}
}