BatchController.class.php
2.15 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
111
<?php
/**
* 【考试中心-后台】导入题目至题库
* BatchController.class.php
* User: 何岳龙
* Date: 2017-04-24
*/
namespace Apicp\Controller\Bank;
use Common\Service\TopicService;
class BatchController 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()
{
// 获取数据
$params = I('post.');
// 验证数据
if (!$this->validation($params)) {
return false;
}
// 重组数组
$this->topic_service->get_data($params['data']);
// 初始化
$i = 0;
// 循环遍历数据
foreach ($params['data'] as $key => $v) {
// 如果大于头部标题长度则删除多余变量
if ($i >= $params['head_total']) {
unset($params['data'][$key]);
}
$i++;
}
// 错误原因
$error = $this->topic_service->is_parameter($params['data']);
// 如果不存在错误
if (empty($error)) {
$this->topic_service->insert_xls_data($params);
}
// 初始化
$list = [];
// 遍历数组
foreach ($params['data'] as $key => $item) {
$list[] = [
'key' => $key,
'name' => $item
];
}
$this->_result = ['list' => $list, 'result' => $error];
return true;
}
/**
* 验证数据
*
* @param array $params POST数据
*
* @return bool
*/
protected function validation($params = [])
{
// 题库ID不能为空
if (empty($params['eb_id'])) {
E('_EMPTY_ED_ID');
}
// 表头长度不能为空或者不是整数
if (empty($params['head_total']) || !is_numeric($params['head_total'])) {
E('_ERR_HEAD_TOTAL');
}
return true;
}
}