AbstractController.class.php
5.1 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Created by PhpStorm.
* User: zhoutao
* Date: 16/8/9
* Time: 上午10:44
*/
namespace Api\Controller;
use Common\Common\Cache;
use \Common\Controller\Api;
abstract class AbstractController extends Api\AbstractController
{
/** 数据含义:开启 */
const OPEN = 1;
/** 数据含义:关闭 */
const CLOSE = 2;
/** 定义数据含义: 是 */
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;
/**
* 积分配置
* @var array
*/
protected $integralCache = [];
/**
* 学分配置
* @var array
*/
protected $creditCache = [];
/** 策略类型 mi_type0: 积分策略 mi_type1: 学分策略 */
const MI_TYPE_IS_INTEGRAL = 'mi_type0';
const MI_TYPE_IS_CREDIT = 'mi_type1';
public function before_action($action = '')
{
$beforeAction = parent::before_action($action);
if ($this->autoGetData) {
// 自动获取提交值
$this->getData();
// 校验
$this->verifyData();
}
$cache = Cache::instance();
$this->integralCache = $cache->get('Common.StrategySetting');
$this->creditCache = $cache->get('Common.StrategySettingCredit');
// 灯塔系统没有是否开启积分的逻辑 zs_anything 2017-06-06
// if ($this->integralCache['eirsEnable'] != self::OPEN) {
// E('_ERR_INTEGRAL_IS_NOT_OPEN');
// return false;
// }
return $beforeAction;
}
public function after_action($action = '')
{
return parent::after_action();
}
/**
* 验证数据
* @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()
{
$this->data = [];
$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];
}
}