ImportController.class.php
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* 【考试中心-后台】解析题目导入模板
* ImportController.class.php
* User: 何岳龙
* Date: 2017-04-24
*/
namespace Apicp\Controller\Bank;
use Com\PythonExcel;
use Common\Service\TopicService;
class ImportController extends AbstractController
{
/** @var TopicService 初始化题目表 */
protected $topic_service;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
$this->topic_service = new TopicService();
return true;
}
public function Index_post()
{
// 获取上传文件详情
$file = $_FILES['file'];
// 获取题库ID
$eb_Id = I('post.eb_id');
// 验证数据
if (!$this->xls_validation($eb_Id, $_FILES)) {
return false;
}
// xls文件名称
$filename = $file['tmp_name'];
$data = PythonExcel::instance()->read($filename, 0);
// 获取列表
list($data, $title, $total, $headTotal) = $this->topic_service->get_list($data);
// 如果数据为空
if (empty($data)) {
E('_EMPTY_XLS_DATA');
}
//返回值
$this->_result = [
'total' => $total,
'head_total' => $headTotal,
'head' => $title,
'list' => $data
];
return true;
}
/**
* 验证数据
*
* @param string $ed_Id 题库ID
* @param array $file POST数据
*
* @return bool
*/
protected function xls_validation($ed_Id = '', $file = [])
{
// 题库ID不能为空
if (empty($ed_Id)) {
$this->_set_error('_EMPTY_ED_ID');
}
// 请上传文件
if (empty($file)) {
E('_ERR_FILE_UNDEFINED');
}
// 文件上传失败
if ($file['error'] > 0) {
E('_ERR_UPLOAD_FILE');
}
// 文件大小判断(2M以内)
if ($file['size'] > 1024 * 2 * 1000) {
E('_ERR_FILE_SIZE');
}
return true;
}
}