|
@@ -3,15 +3,22 @@ |
|
@@ -3,15 +3,22 @@ |
3
|
namespace app\admin\controller;
|
3
|
namespace app\admin\controller;
|
4
|
|
4
|
|
5
|
use app\common\controller\Backend;
|
5
|
use app\common\controller\Backend;
|
|
|
6
|
+use app\admin\library\Auth;
|
|
|
7
|
+use Exception;
|
|
|
8
|
+use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
9
|
+use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
|
10
|
+use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
|
|
11
|
+use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
|
12
|
+use think\exception\PDOException;
|
6
|
|
13
|
|
7
|
/**
|
14
|
/**
|
8
|
- *
|
15
|
+ *
|
9
|
*
|
16
|
*
|
10
|
* @icon fa fa-circle-o
|
17
|
* @icon fa fa-circle-o
|
11
|
*/
|
18
|
*/
|
12
|
class Baoxiudan extends Backend
|
19
|
class Baoxiudan extends Backend
|
13
|
{
|
20
|
{
|
14
|
-
|
21
|
+
|
15
|
/**
|
22
|
/**
|
16
|
* Baoxiudan模型对象
|
23
|
* Baoxiudan模型对象
|
17
|
* @var \app\admin\model\Baoxiudan
|
24
|
* @var \app\admin\model\Baoxiudan
|
|
@@ -30,13 +37,13 @@ class Baoxiudan extends Backend |
|
@@ -30,13 +37,13 @@ class Baoxiudan extends Backend |
30
|
$this->view->assign("fuwuList", $this->model->getFuwuList());
|
37
|
$this->view->assign("fuwuList", $this->model->getFuwuList());
|
31
|
$this->view->assign("taiuduList", $this->model->getTaiuduList());
|
38
|
$this->view->assign("taiuduList", $this->model->getTaiuduList());
|
32
|
}
|
39
|
}
|
33
|
-
|
40
|
+
|
34
|
/**
|
41
|
/**
|
35
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
42
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
36
|
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
43
|
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
37
|
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
44
|
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
38
|
*/
|
45
|
*/
|
39
|
-
|
46
|
+
|
40
|
|
47
|
|
41
|
/**
|
48
|
/**
|
42
|
* 查看
|
49
|
* 查看
|
|
@@ -47,32 +54,30 @@ class Baoxiudan extends Backend |
|
@@ -47,32 +54,30 @@ class Baoxiudan extends Backend |
47
|
$this->relationSearch = true;
|
54
|
$this->relationSearch = true;
|
48
|
//设置过滤方法
|
55
|
//设置过滤方法
|
49
|
$this->request->filter(['strip_tags', 'trim']);
|
56
|
$this->request->filter(['strip_tags', 'trim']);
|
50
|
- if ($this->request->isAjax())
|
|
|
51
|
- {
|
57
|
+ if ($this->request->isAjax()) {
|
52
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
58
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
53
|
- if ($this->request->request('keyField'))
|
|
|
54
|
- {
|
59
|
+ if ($this->request->request('keyField')) {
|
55
|
return $this->selectpage();
|
60
|
return $this->selectpage();
|
56
|
}
|
61
|
}
|
57
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
62
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
58
|
$total = $this->model
|
63
|
$total = $this->model
|
59
|
- ->with(['user','zhandian','xitong'])
|
|
|
60
|
- ->where($where)
|
|
|
61
|
- ->order($sort, $order)
|
|
|
62
|
- ->count();
|
64
|
+ ->with(['user', 'zhandian', 'xitong'])
|
|
|
65
|
+ ->where($where)
|
|
|
66
|
+ ->order($sort, $order)
|
|
|
67
|
+ ->count();
|
63
|
|
68
|
|
64
|
$list = $this->model
|
69
|
$list = $this->model
|
65
|
- ->with(['user','zhandian','xitong'])
|
|
|
66
|
- ->where($where)
|
|
|
67
|
- ->order($sort, $order)
|
|
|
68
|
- ->limit($offset, $limit)
|
|
|
69
|
- ->select();
|
70
|
+ ->with(['user', 'zhandian', 'xitong'])
|
|
|
71
|
+ ->where($where)
|
|
|
72
|
+ ->order($sort, $order)
|
|
|
73
|
+ ->limit($offset, $limit)
|
|
|
74
|
+ ->select();
|
70
|
|
75
|
|
71
|
foreach ($list as $row) {
|
76
|
foreach ($list as $row) {
|
72
|
-
|
77
|
+
|
73
|
$row->getRelation('user')->visible(['name']);
|
78
|
$row->getRelation('user')->visible(['name']);
|
74
|
- $row->getRelation('zhandian')->visible(['zhandian']);
|
|
|
75
|
- $row->getRelation('xitong')->visible(['xitong']);
|
79
|
+ $row->getRelation('zhandian')->visible(['zhandian']);
|
|
|
80
|
+ $row->getRelation('xitong')->visible(['xitong']);
|
76
|
}
|
81
|
}
|
77
|
$list = collection($list)->toArray();
|
82
|
$list = collection($list)->toArray();
|
78
|
$result = array("total" => $total, "rows" => $list);
|
83
|
$result = array("total" => $total, "rows" => $list);
|
|
@@ -81,4 +86,138 @@ class Baoxiudan extends Backend |
|
@@ -81,4 +86,138 @@ class Baoxiudan extends Backend |
81
|
}
|
86
|
}
|
82
|
return $this->view->fetch();
|
87
|
return $this->view->fetch();
|
83
|
}
|
88
|
}
|
|
|
89
|
+
|
|
|
90
|
+
|
|
|
91
|
+ /**
|
|
|
92
|
+ * 导入
|
|
|
93
|
+ */
|
|
|
94
|
+ protected function import()
|
|
|
95
|
+ {
|
|
|
96
|
+ $file = $this->request->request('file');
|
|
|
97
|
+ if (!$file) {
|
|
|
98
|
+ $this->error(__('Parameter %s can not be empty', 'file'));
|
|
|
99
|
+ }
|
|
|
100
|
+ $filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
|
|
101
|
+ if (!is_file($filePath)) {
|
|
|
102
|
+ $this->error(__('No results were found'));
|
|
|
103
|
+ }
|
|
|
104
|
+ //实例化reader
|
|
|
105
|
+ $ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
106
|
+ if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
|
|
107
|
+ $this->error(__('Unknown data format'));
|
|
|
108
|
+ }
|
|
|
109
|
+ if ($ext === 'csv') {
|
|
|
110
|
+ $file = fopen($filePath, 'r');
|
|
|
111
|
+ $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
|
112
|
+ $fp = fopen($filePath, "w");
|
|
|
113
|
+ $n = 0;
|
|
|
114
|
+ while ($line = fgets($file)) {
|
|
|
115
|
+ $line = rtrim($line, "\n\r\0");
|
|
|
116
|
+ $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
|
|
117
|
+ if ($encoding != 'utf-8') {
|
|
|
118
|
+ $line = mb_convert_encoding($line, 'utf-8', $encoding);
|
|
|
119
|
+ }
|
|
|
120
|
+ if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
|
|
121
|
+ fwrite($fp, $line . "\n");
|
|
|
122
|
+ } else {
|
|
|
123
|
+ fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
|
|
124
|
+ }
|
|
|
125
|
+ $n++;
|
|
|
126
|
+ }
|
|
|
127
|
+ fclose($file) || fclose($fp);
|
|
|
128
|
+
|
|
|
129
|
+ $reader = new Csv();
|
|
|
130
|
+ } elseif ($ext === 'xls') {
|
|
|
131
|
+ $reader = new Xls();
|
|
|
132
|
+ } else {
|
|
|
133
|
+ $reader = new Xlsx();
|
|
|
134
|
+ }
|
|
|
135
|
+
|
|
|
136
|
+ //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
|
|
137
|
+ $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
|
|
|
138
|
+
|
|
|
139
|
+ $table = $this->model->getQuery()->getTable();
|
|
|
140
|
+ $database = \think\Config::get('database.database');
|
|
|
141
|
+ $fieldArr = [];
|
|
|
142
|
+ $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
|
|
|
143
|
+ foreach ($list as $k => $v) {
|
|
|
144
|
+ if ($importHeadType == 'comment') {
|
|
|
145
|
+ $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
|
|
146
|
+ } else {
|
|
|
147
|
+ $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
|
|
148
|
+ }
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ //加载文件
|
|
|
152
|
+ $insert = [];
|
|
|
153
|
+ try {
|
|
|
154
|
+ if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
155
|
+ $this->error(__('Unknown data format'));
|
|
|
156
|
+ }
|
|
|
157
|
+ $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
|
|
158
|
+ $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
|
|
159
|
+ $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
|
|
160
|
+ $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
|
|
161
|
+ $fields = [];
|
|
|
162
|
+ for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
|
|
163
|
+ for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
164
|
+ $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
165
|
+ $fields[] = $val;
|
|
|
166
|
+ }
|
|
|
167
|
+ }
|
|
|
168
|
+
|
|
|
169
|
+ for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
|
170
|
+ $values = [];
|
|
|
171
|
+ for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
172
|
+ $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
173
|
+ $values[] = is_null($val) ? '' : $val;
|
|
|
174
|
+ }
|
|
|
175
|
+ $row = [];
|
|
|
176
|
+ $temp = array_combine($fields, $values);
|
|
|
177
|
+ foreach ($temp as $k => $v) {
|
|
|
178
|
+ if (isset($fieldArr[$k]) && $k !== '') {
|
|
|
179
|
+ $row[$fieldArr[$k]] = $v;
|
|
|
180
|
+ }
|
|
|
181
|
+ }
|
|
|
182
|
+ if ($row) {
|
|
|
183
|
+ $insert[] = $row;
|
|
|
184
|
+ }
|
|
|
185
|
+ }
|
|
|
186
|
+ } catch (Exception $exception) {
|
|
|
187
|
+ $this->error($exception->getMessage());
|
|
|
188
|
+ }
|
|
|
189
|
+ if (!$insert) {
|
|
|
190
|
+ $this->error(__('No rows were updated'));
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ try {
|
|
|
194
|
+ //是否包含admin_id字段
|
|
|
195
|
+ $has_admin_id = false;
|
|
|
196
|
+ foreach ($fieldArr as $name => $key) {
|
|
|
197
|
+ if ($key == 'admin_id') {
|
|
|
198
|
+ $has_admin_id = true;
|
|
|
199
|
+ break;
|
|
|
200
|
+ }
|
|
|
201
|
+ }
|
|
|
202
|
+ if ($has_admin_id) {
|
|
|
203
|
+ $auth = Auth::instance();
|
|
|
204
|
+ foreach ($insert as &$val) {
|
|
|
205
|
+ if (!isset($val['admin_id']) || empty($val['admin_id'])) {
|
|
|
206
|
+ $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
|
|
|
207
|
+ }
|
|
|
208
|
+ }
|
|
|
209
|
+ }
|
|
|
210
|
+ $this->model->saveAll($insert);
|
|
|
211
|
+ } catch (PDOException $exception) {
|
|
|
212
|
+ $msg = $exception->getMessage();
|
|
|
213
|
+ if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
|
|
|
214
|
+ $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
|
|
|
215
|
+ };
|
|
|
216
|
+ $this->error($msg);
|
|
|
217
|
+ } catch (Exception $e) {
|
|
|
218
|
+ $this->error($e->getMessage());
|
|
|
219
|
+ }
|
|
|
220
|
+
|
|
|
221
|
+ $this->success();
|
|
|
222
|
+ }
|
84
|
} |
223
|
} |