ConfigService.class.php
3.88 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
<?php
/**
* 签到配置表
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-04-24 10:27:52
* @version $Id$
*/
namespace Common\Service;
use Common\Model\ConfigModel;
class ConfigService extends AbstractService
{
// 构造方法
public function __construct()
{
$this->_d = new ConfigModel();
parent::__construct();
}
/**
* 设置默认数据
* @return bool
*/
public function set_default_data()
{
$sign_config = $this->_d->get_by_conds(array());
if (empty($sign_config)) {
// 默认数据文件所在路径
$data_path = APP_PATH . 'Common' . D_S . 'Sql' . D_S . 'data.php';
// 如果默认数据所在文件不存在就不再执行下面的
if (!file_exists($data_path)) {
return false;
}
// 读取默认数据
$data = require $data_path;
// 如果默认数据不是数组或者为空,就不继续执行剩下的了
if (!is_array($data) || empty($data)) {
return false;
}
// 循环默认数据,插入默认数据
foreach ($data as $k => $v) {
switch ($k) {
case 'sign_config_default':
$this->_d->insert_all($v);
break;
}
}
}
return true;
}
/**
* 保存签到配置(后台)
*
* @param array $result 返回签到信息
* @param array $reqData 请求数据
*
* @return bool
*/
public function save_config(&$result, $reqData)
{
// 数据验证
if (!$this->validate_params($reqData)) {
return false;
}
// 获取签到配置信息
$config = $this->_d->get_by_conds(array());
// 配置信息是否存在
if (empty($config)) {
// 插入配置信息
$this->_d->insert($reqData);
} else {
$reqData['integral_rules'] = serialize($reqData['integral_rules']);
// 是否修改积分规则
if ($reqData['integral_rules'] != $config['integral_rules']) {
$reqData['rules_updated'] = MILLI_TIME;
}
// 更新配置信息
$this->_d->update_by_conds(array(), $reqData);
}
return true;
}
/**
* [验证签到配置数据]
*
* @param [array] $data [签到配置数组]
*
* @return [array]
*/
private function validate_params($data = array())
{
$data['sign_rules'] = addslashes($data['sign_rules']);
// 如果不是数组或为空
if (!is_array($data) || empty($data)) {
$this->_set_error('_ERR_SIGN_CONFIG');
return false;
}
// 签到周期不能为空
if (empty($data['cycle']) || !intval($data['cycle'])) {
$this->_set_error('_EMPTY_SIGN_CYCLE');
return false;
}
// 积分规则不能有空数据
if (empty($data['integral_rules'])) {
$this->_set_error('_EMPTY_INTEGRAL_RULES');
return false;
} else {
foreach ($data['integral_rules'] as $key => $val) {
$integral = intval($val);
if (!$integral) {
$this->_set_error('_EMPTY_INTEGRAL_RULES');
return false;
}
}
}
// 积分规则数据个数与周期不一致
if (count($data['integral_rules']) != $data['cycle']) {
$this->_set_error('_ERR_INTEGRAL_RULES');
return false;
}
// 签到规则不能为空
if (empty($data['sign_rules'])) {
$this->_set_error('_EMPTY_SIGN_RULES');
return false;
}
return true;
}
}