|
@@ -38,6 +38,13 @@ class Study extends Backend |
|
@@ -38,6 +38,13 @@ class Study extends Backend |
38
|
}
|
38
|
}
|
39
|
|
39
|
|
40
|
|
40
|
|
|
|
41
|
+ /**
|
|
|
42
|
+ * 导入
|
|
|
43
|
+ *
|
|
|
44
|
+ */
|
|
|
45
|
+ public function import(){
|
|
|
46
|
+ return parent::import();
|
|
|
47
|
+ }
|
41
|
|
48
|
|
42
|
/**
|
49
|
/**
|
43
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
50
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
|
@@ -257,141 +264,5 @@ class Study extends Backend |
|
@@ -257,141 +264,5 @@ class Study extends Backend |
257
|
$this->success();
|
264
|
$this->success();
|
258
|
}
|
265
|
}
|
259
|
|
266
|
|
260
|
- /**
|
|
|
261
|
- * 导入
|
|
|
262
|
- *
|
|
|
263
|
- * @return void
|
|
|
264
|
- * @throws PDOException
|
|
|
265
|
- * @throws BindParamException
|
|
|
266
|
- */
|
|
|
267
|
- protected function import()
|
|
|
268
|
- {
|
|
|
269
|
- $file = $this->request->request('file');
|
|
|
270
|
- if (!$file) {
|
|
|
271
|
- $this->error(__('Parameter %s can not be empty', 'file'));
|
|
|
272
|
- }
|
|
|
273
|
- $filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
|
|
274
|
- if (!is_file($filePath)) {
|
|
|
275
|
- $this->error(__('No results were found'));
|
|
|
276
|
- }
|
|
|
277
|
- //实例化reader
|
|
|
278
|
- $ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
279
|
- if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
|
|
280
|
- $this->error(__('Unknown data format'));
|
|
|
281
|
- }
|
|
|
282
|
- if ($ext === 'csv') {
|
|
|
283
|
- $file = fopen($filePath, 'r');
|
|
|
284
|
- $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
|
285
|
- $fp = fopen($filePath, 'w');
|
|
|
286
|
- $n = 0;
|
|
|
287
|
- while ($line = fgets($file)) {
|
|
|
288
|
- $line = rtrim($line, "\n\r\0");
|
|
|
289
|
- $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
|
|
290
|
- if ($encoding !== 'utf-8') {
|
|
|
291
|
- $line = mb_convert_encoding($line, 'utf-8', $encoding);
|
|
|
292
|
- }
|
|
|
293
|
- if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
|
|
294
|
- fwrite($fp, $line . "\n");
|
|
|
295
|
- } else {
|
|
|
296
|
- fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
|
|
297
|
- }
|
|
|
298
|
- $n++;
|
|
|
299
|
- }
|
|
|
300
|
- fclose($file) || fclose($fp);
|
|
|
301
|
-
|
|
|
302
|
- $reader = new Csv();
|
|
|
303
|
- } elseif ($ext === 'xls') {
|
|
|
304
|
- $reader = new Xls();
|
|
|
305
|
- } else {
|
|
|
306
|
- $reader = new Xlsx();
|
|
|
307
|
- }
|
|
|
308
|
-
|
|
|
309
|
- //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
|
|
310
|
- $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
|
|
|
311
|
-
|
|
|
312
|
- $table = $this->model->getQuery()->getTable();
|
|
|
313
|
- $database = \think\Config::get('database.database');
|
|
|
314
|
- $fieldArr = [];
|
|
|
315
|
- $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
|
|
|
316
|
- foreach ($list as $k => $v) {
|
|
|
317
|
- if ($importHeadType == 'comment') {
|
|
|
318
|
- $v['COLUMN_COMMENT'] = explode(':', $v['COLUMN_COMMENT'])[0]; //字段备注有:时截取
|
|
|
319
|
- $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
|
|
320
|
- } else {
|
|
|
321
|
- $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
|
|
322
|
- }
|
|
|
323
|
- }
|
|
|
324
|
-
|
|
|
325
|
- //加载文件
|
|
|
326
|
- $insert = [];
|
|
|
327
|
- try {
|
|
|
328
|
- if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
329
|
- $this->error(__('Unknown data format'));
|
|
|
330
|
- }
|
|
|
331
|
- $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
|
|
332
|
- $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
|
|
333
|
- $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
|
|
334
|
- $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
|
|
335
|
- $fields = [];
|
|
|
336
|
- for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
|
|
337
|
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
338
|
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
339
|
- $fields[] = $val;
|
|
|
340
|
- }
|
|
|
341
|
- }
|
|
|
342
|
|
267
|
|
343
|
- for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
|
344
|
- $values = [];
|
|
|
345
|
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
346
|
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
347
|
- $values[] = is_null($val) ? '' : $val;
|
|
|
348
|
- }
|
|
|
349
|
- $row = [];
|
|
|
350
|
- $temp = array_combine($fields, $values);
|
|
|
351
|
- foreach ($temp as $k => $v) {
|
|
|
352
|
- if (isset($fieldArr[$k]) && $k !== '') {
|
|
|
353
|
- $row[$fieldArr[$k]] = $v;
|
|
|
354
|
- }
|
|
|
355
|
- }
|
|
|
356
|
- if ($row) {
|
|
|
357
|
- $insert[] = $row;
|
|
|
358
|
- }
|
|
|
359
|
- }
|
|
|
360
|
- } catch (Exception $exception) {
|
|
|
361
|
- $this->error($exception->getMessage());
|
|
|
362
|
- }
|
|
|
363
|
- if (!$insert) {
|
|
|
364
|
- $this->error(__('No rows were updated'));
|
|
|
365
|
- }
|
|
|
366
|
-
|
|
|
367
|
- try {
|
|
|
368
|
- //是否包含admin_id字段
|
|
|
369
|
- $has_admin_id = false;
|
|
|
370
|
- foreach ($fieldArr as $name => $key) {
|
|
|
371
|
- if ($key == 'admin_id') {
|
|
|
372
|
- $has_admin_id = true;
|
|
|
373
|
- break;
|
|
|
374
|
- }
|
|
|
375
|
- }
|
|
|
376
|
- if ($has_admin_id) {
|
|
|
377
|
- $auth = Auth::instance();
|
|
|
378
|
- foreach ($insert as &$val) {
|
|
|
379
|
- if (!isset($val['admin_id']) || empty($val['admin_id'])) {
|
|
|
380
|
- $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
|
|
|
381
|
- }
|
|
|
382
|
- }
|
|
|
383
|
- }
|
|
|
384
|
- $this->model->saveAll($insert);
|
|
|
385
|
- } catch (PDOException $exception) {
|
|
|
386
|
- $msg = $exception->getMessage();
|
|
|
387
|
- if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
|
|
|
388
|
- $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
|
|
|
389
|
- };
|
|
|
390
|
- $this->error($msg);
|
|
|
391
|
- } catch (Exception $e) {
|
|
|
392
|
- $this->error($e->getMessage());
|
|
|
393
|
- }
|
|
|
394
|
-
|
|
|
395
|
- $this->success();
|
|
|
396
|
- }
|
|
|
397
|
} |
268
|
} |