|
@@ -2,23 +2,15 @@ |
|
@@ -2,23 +2,15 @@ |
2
|
|
2
|
|
3
|
namespace app\admin\controller;
|
3
|
namespace app\admin\controller;
|
4
|
|
4
|
|
5
|
-use app\admin\library\Auth;
|
|
|
6
|
use app\common\controller\Backend;
|
5
|
use app\common\controller\Backend;
|
7
|
use app\common\controller\Resource;
|
6
|
use app\common\controller\Resource;
|
8
|
use Exception;
|
7
|
use Exception;
|
9
|
-use GuzzleHttp\Client;
|
|
|
10
|
-use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
11
|
-use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
|
12
|
-use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
|
|
13
|
-use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
|
14
|
use think\Db;
|
8
|
use think\Db;
|
15
|
-use think\db\exception\BindParamException;
|
9
|
+use think\db\exception\DataNotFoundException;
|
|
|
10
|
+use think\db\exception\ModelNotFoundException;
|
16
|
use think\exception\DbException;
|
11
|
use think\exception\DbException;
|
17
|
use think\exception\PDOException;
|
12
|
use think\exception\PDOException;
|
18
|
use think\exception\ValidateException;
|
13
|
use think\exception\ValidateException;
|
19
|
-use http\Header;
|
|
|
20
|
-use think\Env;
|
|
|
21
|
-use app\api\controller\Login;
|
|
|
22
|
|
14
|
|
23
|
/**
|
15
|
/**
|
24
|
* 学生管理
|
16
|
* 学生管理
|
|
@@ -47,9 +39,139 @@ class Study extends Backend |
|
@@ -47,9 +39,139 @@ class Study extends Backend |
47
|
/**
|
39
|
/**
|
48
|
* 导入
|
40
|
* 导入
|
49
|
*
|
41
|
*
|
|
|
42
|
+ * @return void
|
|
|
43
|
+ * @throws PDOException
|
|
|
44
|
+ * @throws BindParamException
|
50
|
*/
|
45
|
*/
|
51
|
- public function import(){
|
|
|
52
|
- return parent::import();
|
46
|
+ protected function import()
|
|
|
47
|
+ {
|
|
|
48
|
+ $file = $this->request->request('file');
|
|
|
49
|
+ if (!$file) {
|
|
|
50
|
+ $this->error(__('Parameter %s can not be empty', 'file'));
|
|
|
51
|
+ }
|
|
|
52
|
+ $filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
|
|
53
|
+ if (!is_file($filePath)) {
|
|
|
54
|
+ $this->error(__('No results were found'));
|
|
|
55
|
+ }
|
|
|
56
|
+ //实例化reader
|
|
|
57
|
+ $ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
58
|
+ if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
|
|
59
|
+ $this->error(__('Unknown data format'));
|
|
|
60
|
+ }
|
|
|
61
|
+ if ($ext === 'csv') {
|
|
|
62
|
+ $file = fopen($filePath, 'r');
|
|
|
63
|
+ $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
|
64
|
+ $fp = fopen($filePath, 'w');
|
|
|
65
|
+ $n = 0;
|
|
|
66
|
+ while ($line = fgets($file)) {
|
|
|
67
|
+ $line = rtrim($line, "\n\r\0");
|
|
|
68
|
+ $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
|
|
69
|
+ if ($encoding !== 'utf-8') {
|
|
|
70
|
+ $line = mb_convert_encoding($line, 'utf-8', $encoding);
|
|
|
71
|
+ }
|
|
|
72
|
+ if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
|
|
73
|
+ fwrite($fp, $line . "\n");
|
|
|
74
|
+ } else {
|
|
|
75
|
+ fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
|
|
76
|
+ }
|
|
|
77
|
+ $n++;
|
|
|
78
|
+ }
|
|
|
79
|
+ fclose($file) || fclose($fp);
|
|
|
80
|
+
|
|
|
81
|
+ $reader = new Csv();
|
|
|
82
|
+ } elseif ($ext === 'xls') {
|
|
|
83
|
+ $reader = new Xls();
|
|
|
84
|
+ } else {
|
|
|
85
|
+ $reader = new Xlsx();
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
|
|
89
|
+ $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
|
|
|
90
|
+
|
|
|
91
|
+ $table = $this->model->getQuery()->getTable();
|
|
|
92
|
+ $database = \think\Config::get('database.database');
|
|
|
93
|
+ $fieldArr = [];
|
|
|
94
|
+ $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
|
|
|
95
|
+ foreach ($list as $k => $v) {
|
|
|
96
|
+ if ($importHeadType == 'comment') {
|
|
|
97
|
+ $v['COLUMN_COMMENT'] = explode(':', $v['COLUMN_COMMENT'])[0]; //字段备注有:时截取
|
|
|
98
|
+ $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
|
|
99
|
+ } else {
|
|
|
100
|
+ $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
|
|
101
|
+ }
|
|
|
102
|
+ }
|
|
|
103
|
+
|
|
|
104
|
+ //加载文件
|
|
|
105
|
+ $insert = [];
|
|
|
106
|
+ try {
|
|
|
107
|
+ if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
108
|
+ $this->error(__('Unknown data format'));
|
|
|
109
|
+ }
|
|
|
110
|
+ $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
|
|
111
|
+ $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
|
|
112
|
+ $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
|
|
113
|
+ $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
|
|
114
|
+ $fields = [];
|
|
|
115
|
+ for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
|
|
116
|
+ for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
117
|
+ $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
118
|
+ $fields[] = $val;
|
|
|
119
|
+ }
|
|
|
120
|
+ }
|
|
|
121
|
+
|
|
|
122
|
+ for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
|
123
|
+ $values = [];
|
|
|
124
|
+ for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
125
|
+ $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
126
|
+ $values[] = is_null($val) ? '' : $val;
|
|
|
127
|
+ }
|
|
|
128
|
+ $row = [];
|
|
|
129
|
+ $temp = array_combine($fields, $values);
|
|
|
130
|
+ foreach ($temp as $k => $v) {
|
|
|
131
|
+ if (isset($fieldArr[$k]) && $k !== '') {
|
|
|
132
|
+ $row[$fieldArr[$k]] = $v;
|
|
|
133
|
+ }
|
|
|
134
|
+ }
|
|
|
135
|
+ if ($row) {
|
|
|
136
|
+ $insert[] = $row;
|
|
|
137
|
+ }
|
|
|
138
|
+ }
|
|
|
139
|
+ } catch (Exception $exception) {
|
|
|
140
|
+ $this->error($exception->getMessage());
|
|
|
141
|
+ }
|
|
|
142
|
+ if (!$insert) {
|
|
|
143
|
+ $this->error(__('No rows were updated'));
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ try {
|
|
|
147
|
+ //是否包含admin_id字段
|
|
|
148
|
+ $has_admin_id = false;
|
|
|
149
|
+ foreach ($fieldArr as $name => $key) {
|
|
|
150
|
+ if ($key == 'admin_id') {
|
|
|
151
|
+ $has_admin_id = true;
|
|
|
152
|
+ break;
|
|
|
153
|
+ }
|
|
|
154
|
+ }
|
|
|
155
|
+ if ($has_admin_id) {
|
|
|
156
|
+ $auth = Auth::instance();
|
|
|
157
|
+ foreach ($insert as &$val) {
|
|
|
158
|
+ if (!isset($val['admin_id']) || empty($val['admin_id'])) {
|
|
|
159
|
+ $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
|
|
|
160
|
+ }
|
|
|
161
|
+ }
|
|
|
162
|
+ }
|
|
|
163
|
+ $this->model->saveAll($insert);
|
|
|
164
|
+ } catch (PDOException $exception) {
|
|
|
165
|
+ $msg = $exception->getMessage();
|
|
|
166
|
+ if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
|
|
|
167
|
+ $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
|
|
|
168
|
+ };
|
|
|
169
|
+ $this->error($msg);
|
|
|
170
|
+ } catch (Exception $e) {
|
|
|
171
|
+ $this->error($e->getMessage());
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ $this->success();
|
53
|
}
|
175
|
}
|
54
|
|
176
|
|
55
|
/**
|
177
|
/**
|
|
@@ -170,6 +292,9 @@ class Study extends Backend |
|
@@ -170,6 +292,9 @@ class Study extends Backend |
170
|
}
|
292
|
}
|
171
|
}
|
293
|
}
|
172
|
|
294
|
|
|
|
295
|
+ /**
|
|
|
296
|
+ * 下载条形码
|
|
|
297
|
+ */
|
173
|
public function down()
|
298
|
public function down()
|
174
|
{
|
299
|
{
|
175
|
$id = input('id');
|
300
|
$id = input('id');
|
|
@@ -276,8 +401,11 @@ class Study extends Backend |
|
@@ -276,8 +401,11 @@ class Study extends Backend |
276
|
/**
|
401
|
/**
|
277
|
* 下载分享码
|
402
|
* 下载分享码
|
278
|
*
|
403
|
*
|
279
|
- * @param $ids
|
404
|
+ * @param null $ids
|
280
|
* @return void
|
405
|
* @return void
|
|
|
406
|
+ * @throws DbException
|
|
|
407
|
+ * @throws DataNotFoundException
|
|
|
408
|
+ * @throws ModelNotFoundException
|
281
|
*/
|
409
|
*/
|
282
|
public function multi($ids = null)
|
410
|
public function multi($ids = null)
|
283
|
{
|
411
|
{
|