AbstractController.class.php
4.12 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* AbstractController.class.php
* $author$
*/
namespace Apicp\Controller;
abstract class AbstractController extends \Common\Controller\Apicp\AbstractController
{
protected $_require_login = false;
/** 定义数据含义: 是 */
const MEAN_TRUE = 1;
/** 定义数据含义: 否 */
const MEAN_FALSE = 2;
/**
* 提交的字段 [判断不断完善中... 2016-12-09 11:17:40]
* 'require' => true, // 是否必填
* 'default' => '', // 默认数据 (没有则 '')
* 'verify' => 'strval', // 验证方法 (没有则 null)
* 'cn' => '名称', // 字段名 (比如为空时的报错提示里用)
* 'maxLength' => PrizeModel::MAX_NAME_COUNT // 最长限制
* 'area' => [1, 2] // in_array 数据范围约束
* 'regexp' => '' // 正则匹配
*/
protected $field = [];
// 提交的数据
protected $data = [];
// 使用自动获取验证方法
protected $autoGetData = false;
public function before_action($action = '')
{
$beforeAction = parent::before_action($action);
if ($this->autoGetData) {
// 自动获取提交值
$this->getData();
// 校验
$this->verifyData();
}
return $beforeAction;
}
/**
* 验证数据
* @return bool
*/
protected function verifyData()
{
$error = [];
foreach ($this->field as $name => $todo) {
// 判断长度限制
if (isset($todo['maxLength']) && !empty($this->data[$name]) &&
(is_array($this->data[$name]) ? count($this->data[$name]) : mb_strlen($this->data[$name], 'utf-8')) > $todo['maxLength']) {
$error['maxLength'][] = $todo['cn'] . '(' . $todo['maxLength'] . ')';
continue;
}
// 判断数据范围
if (isset($todo['area']) && !empty($this->data[$name]) && !in_array($this->data[$name], $todo['area'])) {
$error['area'][] = $todo['cn'];
continue;
}
// 正则验证
if (isset($todo['regexp']) && !empty($this->data[$name]) && !preg_match($todo['regexp'], $this->data[$name])) {
$error['regexp'][] = $todo['cn'];
continue;
}
}
// 为了合并报错
if (!empty($error['maxLength'])) {
E(L('_ERR_OVER_MAX_COUNT', ['name' => implode(',', $error['maxLength'])]));
return false;
}
if (!empty($error['area'])) {
E(L('_ERR_OVER_DATA_AREA', ['name' => implode(',', $error['area'])]));
return false;
}
if (!empty($error['regexp'])) {
E(L('_ERR_OVER_DATA_REGEXP', ['name' => implode(',', $error['regexp'])]));
return false;
}
return true;
}
/**
* 获取提交数据
* @return array|bool
*/
protected function getData()
{
$emptyKey = [];
foreach ($this->field as $name => $todo) {
// 获取提交数据
$data = I('post.' . $name,
!empty($todo['default']) ? $todo['default'] : '',
!empty($todo['verify']) ? $todo['verify'] : null);
// 验证必填项
if (empty($data) && !isset($todo['default'])) {
if ($todo['require']) {
$emptyKey[] = $todo['cn'];
}
continue;
}
$this->data[$name] = $data;
}
if (!empty($emptyKey)) {
E(L('_ERR_CANNOT_EMPTY', ['name' => implode('|', $emptyKey)]));
return false;
}
return true;
}
/**
* 获取分页数据
* @param String $pageKey 当前页的键值名
* @param String $limitKey 每页数量的键值名
* @return array
* + 开始数
* + 每页数量
*/
protected function getPageOption($pageKey, $limitKey)
{
list($start, $perpage, $page) = page_limit($this->data[$pageKey], $this->data[$limitKey]);
unset($this->data[$pageKey]);
unset($this->data[$limitKey]);
return [$start, $perpage, $page];
}
}