diff --git a/composer.json b/composer.json index 871e83b..e0a615a 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,8 @@ "zoujingli/think-plugs-wechat": "^1.0", "zoujingli/think-plugs-center": "^1.0", "zoujingli/think-plugs-account": "^1.0", - "ext-json": "*" + "ext-json": "*", + "phpoffice/phpspreadsheet": "^4.0" }, "repositories": { "jerryyan/think-plugs-cms": { diff --git a/plugs/think-plugs-ticket/src/controller/Ticket.php b/plugs/think-plugs-ticket/src/controller/Ticket.php index d31e6cf..36ff137 100644 --- a/plugs/think-plugs-ticket/src/controller/Ticket.php +++ b/plugs/think-plugs-ticket/src/controller/Ticket.php @@ -14,6 +14,7 @@ use think\admin\Controller; use think\admin\helper\QueryHelper; use think\admin\model\SystemUser; use think\exception\HttpResponseException; +use \PhpOffice\PhpSpreadsheet\IOFactory; /** * 工单管理 @@ -363,4 +364,56 @@ class Ticket extends Controller ->append(['imgs_arr', 'source_type_name', 'status_text', 'type_name', 'last_reply'])->with('approval'); }); } + + /** + * @return void + * @auth true + */ + public function import() { + $file = $this->app->request->post('file'); + if (!$file) { + // 创建一个临时Excel模板用于下载 + $this->redirect('/static/excel/ticket_import_template.xlsx'); + }; + $file = '.' . str_replace($this->app->request->domain(), '', $file); + //表格字段对应 + $fields = [ + 'A' => 'title', + 'B' => 'type_id', + 'C' => 'content', + 'D' => 'contact_name', + 'E' => 'contact_phone', + 'F' => 'contact_address', + 'G' => 'ticket_address', + ]; + //加载文件 + $spreadsheet = IOFactory::load($file); + + $sheet = $spreadsheet->getActiveSheet(); // 获取表格 + $highestRow = $sheet->getHighestRow(); // 取得总行数 + $sheetData = []; + for ($row = 2; $row <= $highestRow; $row++) { // $row表示从第几行开始读取 + foreach ($fields as $cell => $field) { + $value = $sheet->getCell($cell . $row)->getValue(); + $value = trim($value); + $sheetData[$row][$field] = $value; + } + } + $sheetData = array_values($sheetData); + foreach ($sheetData as $key => $value) { + if (!empty($value['type_id'])) { + $type = TicketType::query()->where(['id' => $value['type_id']])->find(); + if (empty($type)) { + $this->error('工单类型【'.$value['type_id'].'】不存在'); + } else { + $sheetData[$key]['type_id'] = $type['id']; + } + } + $sheetData[$key]['create_at'] = date('Y-m-d H:i:s'); + $sheetData[$key]['user_id'] = $this->request->session('user')['id']; + $sheetData[$key]['status'] = 0; + } + TicketTicket::mk()->saveAll($sheetData); + $this->success("成功"); + } } \ No newline at end of file diff --git a/plugs/think-plugs-ticket/src/view/ticket/index.html b/plugs/think-plugs-ticket/src/view/ticket/index.html index 448f835..d13dd72 100644 --- a/plugs/think-plugs-ticket/src/view/ticket/index.html +++ b/plugs/think-plugs-ticket/src/view/ticket/index.html @@ -5,6 +5,10 @@ + +下载导入模板 + + {/block} {block name="content"} @@ -99,6 +103,28 @@ ]], page: true }) + /*!文件上传过程及事件处理 */ + $('[data-file]').on('upload.choose', function (files) { + // 文件选择后的事件 + }).on('upload.hash', function (event, file) { + // file 当前文件对象 + }).on('upload.progress', function (event, obj) { + // obj.file 当前文件对象 + // obj.event 文件上传进度事件 + // obj.number 当前上传进度值 + }).on('upload.done', function (event, obj) { + // obj.file 当前完成的文件对象,每个文件上传成功将会调用 + // obj.data 当前文件上传后服务端返回的内容,部分云上传不会返回数据 + + // obj.file 当前完成的文件对象 + // obj.data 当前文件上传后服务端返回的内容 + console.log(obj.file); + console.log(obj.data); + /*! 提交数据并返回结果 */ + if(obj.file.xurl) $.form.load('{:url("import")}', {file: obj.file.xurl}, 'post'); + }).on('upload.complete', function (event) { + // 全部文件上传成功 + }); }) {/block} \ No newline at end of file diff --git a/public/static/ticket_import_template.xlsx b/public/static/ticket_import_template.xlsx new file mode 100644 index 0000000..3487c81 Binary files /dev/null and b/public/static/ticket_import_template.xlsx differ