作者 董瑞恩
1 个管道 的构建 通过 耗费 1 秒

cost

@@ -11,6 +11,7 @@ namespace app\portal\controller; @@ -11,6 +11,7 @@ namespace app\portal\controller;
11 11
12 use app\portal\model\EquipmentModel; 12 use app\portal\model\EquipmentModel;
13 use cmf\controller\AdminBaseController; 13 use cmf\controller\AdminBaseController;
  14 +use PHPExcel_IOFactory;
14 use QRcode; 15 use QRcode;
15 use think\Db; 16 use think\Db;
16 17
@@ -61,7 +62,53 @@ class AdminEquipmentController extends AdminBaseController{ @@ -61,7 +62,53 @@ class AdminEquipmentController extends AdminBaseController{
61 } 62 }
62 63
63 public function batch_addPost(){ 64 public function batch_addPost(){
64 - 65 + //上传excel文件
  66 + $file = $this->request->file('myfile');
  67 + //移到/public/uploads/excel/下
  68 + $info = $file->move(ROOT_PATH.'public'.DS.'upload'.DS.'excel');
  69 + //上传文件成功
  70 + if($info) {
  71 + //引入PHPExcel类
  72 + vendor('VENDOR_PATH/phpoffice/phpexcel/Classes/PHPExcel.php');
  73 + vendor('VENDOR_PATH/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php');
  74 + //获取上传后的文件名
  75 + $fileName = $info->getSaveName();
  76 + //文件路径
  77 + $filePath = ROOT_PATH.'public/upload/excel/'.$fileName;
  78 + //实例化PHPExcel类
  79 + //使用 PHPExcel_IOFactory 来鉴别文件应该使用哪一个读取类
  80 + $inputFileType = PHPExcel_IOFactory::identify($filePath);
  81 + //实例化类
  82 + $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  83 + //$PHPReader = new \PHPExcel_Reader_Excel2007();
  84 + //读取excel文件
  85 + $objPHPExcel = $objReader->load($filePath);
  86 + //读取excel文件中的第一个工作表
  87 + $sheet = $objPHPExcel->getSheet(0);
  88 + $allRow = $sheet->getHighestRow(); //取得总行数
  89 +// $testInfo=new TestinfoModel();
  90 + //从第二行开始插入,第一行是列名
  91 + for ($j = 2; $j <= $allRow; $j++) {
  92 + $data=[
  93 + 'serial_number'=> $objPHPExcel->getActiveSheet()->getCell("a" . $j)->getValue(),
  94 + 'mac_address' => $objPHPExcel->getActiveSheet()->getCell("b" . $j)->getValue(),
  95 + 'name' => $objPHPExcel->getActiveSheet()->getCell("c" . $j)->getValue(),
  96 + 'hospital' => $objPHPExcel->getActiveSheet()->getCell("d" . $j)->getValue(),
  97 + 'note' => $objPHPExcel->getActiveSheet()->getCell("e" . $j)->getValue(),
  98 + 'create_time' => time()
  99 + ];
  100 + $data['qr_code'] = $this->createQrCode($data['mac_address']);
  101 + $Equipment=new EquipmentModel();
  102 + try{
  103 + $Equipment->save($data);
  104 + echo "第" . $j . "行数据导入成功!<br/>";
  105 + }catch (\Exception $exception){
  106 + echo "第" . $j . "行数据导入失败!错误信息:".$exception->getMessage()."<br/>";
  107 + }
  108 + }
  109 + }else{
  110 + echo "上传文件失败!";
  111 + }
65 } 112 }
66 113
67 114
  1 +<include file="public@header"/>
  2 +<style type="text/css">
  3 + .pic-list li {
  4 + margin-bottom: 5px;
  5 + }
  6 +</style>
  7 +<script type="text/html" id="photos-item-tpl">
  8 + <li id="saved-image{id}">
  9 + <input id="photo-{id}" type="hidden" name="photo_urls[]" value="{filepath}">
  10 + <input class="form-control" id="photo-{id}-name" type="text" name="photo_names[]" value="{name}"
  11 + style="width: 200px;" title="图片名称">
  12 + <img id="photo-{id}-preview" src="{url}" style="height:36px;width: 36px;"
  13 + onclick="imagePreviewDialog(this.src);">
  14 + <a href="javascript:uploadOneImage('图片上传','#photo-{id}');">替换</a>
  15 + <a href="javascript:(function(){$('#saved-image{id}').remove();})();">移除</a>
  16 + </li>
  17 +</script>
  18 +<script type="text/html" id="files-item-tpl">
  19 + <li id="saved-file{id}">
  20 + <input id="file-{id}" type="hidden" name="file_urls[]" value="{filepath}">
  21 + <input class="form-control" id="file-{id}-name" type="text" name="file_names[]" value="{name}"
  22 + style="width: 200px;" title="文件名称">
  23 + <a id="file-{id}-preview" href="{preview_url}" target="_blank">下载</a>
  24 + <a href="javascript:uploadOne('图片上传','#file-{id}','file');">替换</a>
  25 + <a href="javascript:(function(){$('#saved-file{id}').remove();})();">移除</a>
  26 + </li>
  27 +</script>
  28 +</head>
  29 +<body>
  30 +<div class="wrap js-check-wrap">
  31 + <ul class="nav nav-tabs">
  32 + <li><a href="{:url('AdminTestInfo/index')}">检测报告</a></li>
  33 + <li class="active"><a href="{:url('AdminTestInfo/add')}">添加报告</a></li>
  34 + </ul>
  35 + <!--js-ajax-form margin-top-20-->
  36 + <form action="{:url('AdminTestInfo/addPost')}" method="post" class="form-horizontal js-ajax-form margin-top-20">
  37 + <div class="row">
  38 + <div class="col-md-2"></div>
  39 + <div class="col-md-8">
  40 + <table class="table table-bordered">
  41 +
  42 + <tr>
  43 + <th width="80">设备锁编号<span class="form-required">*</span></th>
  44 + <td>
  45 + <input class="form-control" type="text" name="serial_number"
  46 + id="serial_number" required value="" placeholder="请输入设备锁编号"/>
  47 + </td>
  48 + </tr>
  49 +
  50 + </table>
  51 + </div>
  52 + </div>
  53 + </form>
  54 +</div>
  55 +<script type="text/javascript" src="__STATIC__/js/admin.js"></script>
  56 +
  57 +<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.config.js"></script>
  58 +<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.all.min.js"></script>
  59 +<script type="text/javascript">
  60 + function confirm() {
  61 + var serial_number = $('#serial_number').val();
  62 + var mac_address = $('#mac_address').val();
  63 + var name = $('#name').val();
  64 + var hospital = $('#hospital').val();
  65 + var note = $('#note').val();
  66 + var create_time = $('#create_time').val();
  67 +
  68 + return {
  69 + serial_number:serial_number,
  70 + mac_address :mac_address,
  71 + name :name,
  72 + hospital :hospital,
  73 + note :note,
  74 + create_time :create_time
  75 + };
  76 + }
  77 +</script>
  78 +</body>
  79 +</html>
@@ -30,7 +30,7 @@ @@ -30,7 +30,7 @@
30 onclick="doAdd()">新增 30 onclick="doAdd()">新增
31 </button> 31 </button>
32 <button class="btn btn-primary btn-sm js-ajax-submit" type="submit" 32 <button class="btn btn-primary btn-sm js-ajax-submit" type="submit"
33 - onclick="doAdd()">批量新增 33 + onclick="batch_add()">批量新增
34 </button> 34 </button>
35 <button class="btn btn-primary btn-sm js-ajax-submit" type="submit" 35 <button class="btn btn-primary btn-sm js-ajax-submit" type="submit"
36 data-action="{:url('AdminEquipment/publish',array('yes'=>1))}" data-subcheck="true">批量导出 36 data-action="{:url('AdminEquipment/publish',array('yes'=>1))}" data-subcheck="true">批量导出
@@ -153,6 +153,39 @@ @@ -153,6 +153,39 @@
153 }); 153 });
154 } 154 }
155 155
  156 + function batch_add() {
  157 + openIframeLayer("{:url('AdminEquipment/batch_add')}", '请选择分类', {
  158 + area: ['1000px', '800px'],
  159 + btn: ['确定', '取消'],
  160 + yes: function (index, layero) {
  161 + var iframeWin = window[layero.find('iframe')[0]['name']];
  162 + var data = iframeWin.confirm();
  163 +// $.ajax({
  164 +// url:"{:url('AdminEquipment/addPost')}",
  165 +// type:"POST",
  166 +// data:{
  167 +// serial_number:data.serial_number,
  168 +// mac_address:data.mac_address,
  169 +// name:data.name,
  170 +// hospital:data.hospital,
  171 +// note:data.note,
  172 +// create_time:data.create_time
  173 +// },
  174 +// dataType:"JSON",
  175 +// success:function (data) {
  176 +// if (data.code===200){
  177 +// alert('添加成功');
  178 +// window.location.reload();
  179 +// }
  180 +// },
  181 +// error:function () {
  182 +// alert('接口异常')
  183 +// }
  184 +// });
  185 + layer.close(index); //如果设定了yes回调,需进行手工关闭
  186 + }
  187 + });
  188 + }
156 189
157 190
158 function reloadPage(win) { 191 function reloadPage(win) {