SaveConfigController.class.php
6.18 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
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* 保存设置接口
* User: tangxingguo
* Date: 2017/4/11
* Time: 18:36
*/
namespace Apicp\Controller\Answer;
use Common\Common\Cache;
use Com\PackageValidate;
use Common\Common\Config;
use Common\Common\Constant;
use Common\Common\TaskCenter;
use Common\Service\AnswerService;
use Common\Service\ConfigService;
use Common\Service\QuestionService;
use Common\Service\SettingService;
class SaveConfigController extends \Apicp\Controller\AbstractController
{
/**
* 保存设置接口
*/
public function Index_post()
{
// 验证规则
$rules = [
'right' => 'require|array',
'is_check' => 'require',
'is_answer_check' => 'require',
'anonymous' => 'require|in:' . SettingService::ADMIN_ANONYMOUS_OPEN . ',' . SettingService::ADMIN_ANONYMOUS_CLOSE,
'is_answer' => 'require',
'is_invite' => 'require',
'invite_right' => 'require|in:1,2',
'invite_number' => 'require',
'invite_max_man' => 'require',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$right = $validate->postData['right'];
// 格式化权限数据
$configServ = new ConfigService();
$right = $configServ->formatPostRight($right);
if (empty($right)) {
// 权限设置不能为空
E('_ERR_CONFIG_RIGHT_IS_EMPTY');
}
// 获取缓存数据
$configComm = &Config::instance();
$config = $configComm->getCacheData();
// 实例化回答表
$answer_service = new AnswerService();
// 实例化问题表
$question_service = new QuestionService();
try {
// 开启事务
$configServ->start_trans();
if ($config['config_id'] > 0) {
// 更新
$configServ->update($config['config_id'], ['rights' => serialize($right)]);
} else {
// 添加
$configServ->insert(['rights' => serialize($right)]);
}
// 是否开启发布审核
$is_check = $validate->postData['is_check'];
$this->save_file('is_check', $is_check);
// 是否开启回答审核
$is_answer_check = $validate->postData['is_answer_check'];
$this->save_file('is_answer_check', $is_answer_check);
// 是否问题匿名
$is_anonymous = $validate->postData['anonymous'];
$this->save_file('anonymous', $is_anonymous);
// 是否开启匿名回答问题
$is_invite = $validate->postData['is_answer'];
$this->save_file('is_answer', $is_invite);
// 是否开启邀请
$is_invite = $validate->postData['is_invite'];
$this->save_file('is_invite', $is_invite);
// 邀请次数
$invite_number = $validate->postData['invite_number'];
$this->save_file('invite_number', $invite_number);
// 邀请最大人数
$invite_max_man = $validate->postData['invite_max_man'];
$this->save_file('invite_max_man', $invite_max_man);
// 邀请权限
$invite_right = $validate->postData['invite_right'];
$this->save_file('invite_right', $invite_right);
// 任务中心-日常任务埋点 准备更新数据审核状态
$taskCenter = &TaskCenter::instance();
$conds = ['check_status' => Constant::QUESTION_CHECK_STATUS_WAIT];
$data = [
'check_status' => Constant::ANSWER_CHECK_STATUS_PASS,
'check_time' => MILLI_TIME
];
//如果开启问题不需要审核
if (SettingService::NOT_CHECK == $is_check) {
// 获取未通过的问题列表
$question_list = $question_service->list_by_conds($conds);
foreach ($question_list as $question_value) {
$taskCenter->triggerDailytask([
'uid' => $question_value['uid'],
'app_data_id' => $question_value['question_id'],
'action_key' => Constant::ACTION_KEYS_ANSWER_SEND_QUESTION,
'description' => '提问审批通过',
]);
}
// 问题表修改为已通过审核
$question_service->update_by_conds($conds, $data);
}
// 如果开启回答不需要审核
if (SettingService::NOT_CHECK == $is_answer_check) {
// 获取未通过的回答列表
$answer_list = $answer_service->list_by_conds($conds);
foreach ($answer_list as $value) {
$taskCenter->triggerDailytask([
'uid' => $value['uid'],
'app_data_id' => $value['question_id'],
'action_key' => Constant::ACTION_KEYS_ANSWER_SEND_ANSWER,
'description' => '回答审批通过',
]);
}
// 回答表修改为已通过审核
$answer_service->update_by_conds($conds, $data);
}
// 提交事务
$configServ->commit();
} catch (\Exception $e) {
E($e->getCode() . ':' . $e->getMessage());
$configServ->rollback();
}
// 清除缓存数据
Config::instance()->clearCacheData();
}
/**
* 保存设置
* @param string $key 键名
* @param string $value 值
* @return bool
*/
public function save_file($key = '', $value = '')
{
// 实例化设置表
$settings = new SettingService();
// 获取缓存
$cache_setting = Cache::instance()->get('Common.AppSetting');
// 严重字段是否存在如果不存在则新增
if (empty($cache_setting[$key]['value']) && !is_numeric($cache_setting[$key]['value'])) {
$settings->insert(['key' => $key, 'value' => $value]);
} else {
$settings->update_by_conds(['key' => $key], ['value' => $value]);
}
return true;
}
}