Jonlin 6 年 前
コミット
5404a66b70

+ 48 - 0
application/admin/controller/Robot.php

@@ -1,6 +1,9 @@
 <?php
 namespace app\admin\controller;
 
+use think\Loader;
+use think\Controller;
+
 /**
  * 智能问答类
  */
@@ -175,4 +178,49 @@ class Robot extends Base
 
         return $operate;
     }
+
+    //导入智能问答
+    function inserExcel()
+    {
+        Loader::import('PHPExcel.PHPExcel');
+        Loader::import('PHPExcel.PHPExcel.PHPExcel_IOFactory');
+        Loader::import('PHPExcel.PHPExcel.PHPExcel_Cell');
+        //获取表单上传文件
+        $file = request()->file('excel');
+        $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');
+        //上传验证后缀名,以及上传之后移动的地址
+        if ($info) {
+//            echo $info->getFilename();
+            $exclePath = $info->getSaveName();  //获取文件名
+            $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath;   //上传文件的地址
+            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
+            $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
+            $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
+            array_shift($excel_array);  //删除第一个数组(标题);
+            $info = [];
+            foreach ($excel_array as $k => $v) {
+                $info[$k]['robot_name'] = $v[0];
+                $info[$k]['robot_content'] = $v[1];
+                $info[$k]['robot_status'] = $v[2];
+                $info[$k]['robot_host'] = $v[3];
+                $info[$k]['groups_id'] = 1;
+                $info[$k]['robotgroups_id'] = 1;
+                $info[$k]['robot_addTime'] = date('Y-m-d H:m:s',time());
+                $info[$k]['robot_updateTime'] = date('Y-m-d H:m:s',time());
+                $info[$k]['admin_id'] = session('user_id');
+            }
+            $robot = db('robot')->select();
+            for($i=0;$i<count($info);$i++){
+                for($j=0;$j<count($robot);$j++){
+                    if($info[$i]['robot_name'] == $robot[$j]['robot_name']){
+                        return json(['code' => -1, 'data' => url('robot/index'), 'msg' => 'excel表第'.($i+1).'行智能问答问题已存在']);
+                    }
+                }
+            }
+            db('robot')->insertAll($info); //批量插入数据
+            return json(['code' => 1, 'data' => $this->redirect('robot/index'), 'msg' => '插入智能问答数据成功']);
+        } else {
+            return json(['code' => -2, 'data' => url('robot/index'), 'msg' => '插入智能问答数据失败']);
+        }
+    }
 }

+ 14 - 0
application/admin/view/robot/index.html

@@ -23,6 +23,20 @@
                 <button class="btn btn-primary" type="button" style="margin-top:5px"><strong>新 增</strong>
                 </button>
             </a>
+            <form action="/admin/robot/inserExcel" enctype="multipart/form-data" method="post" role="form" class="form-inline pull-right">
+                <div class="content clearfix m-b" style="margin-left: 30px;">
+                    <div class="form-group">
+                        <a href="/static/admin/file/robot.xlsx" class="btn btn-primary" style="margin-top:5px" >下载智能问答模板文件</a>
+                    </div>
+                    <div class="form-group">
+                        <label>导入智能问答:</label>
+                        <input type="file" class="form-control" name="excel" style="width: 200px;border: none;" />
+                    </div>
+                    <div class="form-group">
+                        <input type="submit" class="btn btn-primary" style="margin-top:5px" value="导入">
+                    </div>
+                </div>
+            </form>
             <!--搜索框开始-->
             <form id='commentForm' role="form" method="post" class="form-inline pull-right">
                 <div class="content clearfix m-b">

BIN
public/static/admin/file/robot.xlsx