AbstractController.class.php
7.99 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* Created by PhpStorm.
* User: tony
* Time: 2016年9月27日11:40:37
*/
namespace Api\Controller\Invite;
use Com\Cookie;
use Common\Common\Cache;
use Common\Common\Department;
use Common\Common\User;
use Common\Model\AttrModel;
use Common\Model\InviteSettingModel;
use Common\Service\AttrService;
use Common\Service\InviteLinkService;
use Common\Service\InviteSettingService;
use Common\Service\InviteUserService;
abstract class AbstractController extends \Api\Controller\AbstractController
{
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
return true;
}
public function after_action($action = '')
{
return parent::after_action();
}
/**
* 判断是否已经接受邀请并填写资料
* @param array $user 用户信息
* @param int $linkId 邀请链接ID
* @return bool
*/
protected function _hasAcceptAndWrite(&$user, $linkId = 0)
{
$wx_openid = Cookie::instance()->getx('wx_openid');
if (null == $wx_openid) {
E('PLEASE_LOGIN');
}
$condition = array('wx_openid' => $wx_openid);
if (0 < $linkId) {
$condition['link_id'] = $linkId;
}
$inviteUserService = new InviteUserService();
$user = $inviteUserService->get_by_conds($condition);
return !empty($user);
}
/**
* 获取邀请配置
* @return array|bool
*/
protected function _getInviteSetting()
{
static $setting = array();
if (!empty($setting)) {
return $setting;
}
// 读取邀请配置
$settingService = new InviteSettingService();
$setting = $settingService->get_by_conds([]);
if (empty($setting)) {
E('1007:请通知管理员配置邀请设置');
return false;
}
return $setting;
}
/**
* 检查邀请链接是否合法
* @param $link_id
* @return array $user
*/
protected function _checkLinkId($link_id)
{
if (empty($link_id)) {
E('_ERR_LINK_ID_IS_NULL');
}
$inviteLinkService = new InviteLinkService();
$link = $inviteLinkService->get($link_id);
// 邀请连接不存在或已被删除
if (empty($link)) {
E('_ERR_LINK_NOT_FOUND');
}
// 读取字段配置
$attrService = new AttrService();
$fields = $attrService->getAttrList(true, array(), false, false);
$fields = array_combine_by_key($fields, 'field_name');
// 读取邀请配置
$inviteSetting = $this->_getInviteSetting();
if (empty($inviteSetting['inviter_write'])) {
$inviteWrite = array();
} else {
$inviteWrite = unserialize($inviteSetting['inviter_write']);
}
$default_data = unserialize($link['default_data']);
// 如果岗位已经开启并且必填, 邀请岗位也开启时, 邀请链接中岗位为空则说明邀请链接过期
$memJobIsOpen = AttrModel::ATTR_IS_OPEN_TRUE == $fields['memJob']['is_open'];
$memJobIsRequired = AttrModel::ATTR_IS_REQUIRED_TRUE == $fields['memJob']['is_required'];
if ($memJobIsOpen && $memJobIsRequired &&
in_array('job', $inviteWrite) && empty($default_data['job'])) {
E('1009:邀请链接已过期');
return false;
}
// 如果角色已经开启并且必填, 邀请角色也开启时, 邀请链接中角色为空则说明邀请链接过期
$memRoleIsOpen = AttrModel::ATTR_IS_OPEN_TRUE == $fields['memRole']['is_open'];
$memRoleIsRequired = AttrModel::ATTR_IS_REQUIRED_TRUE == $fields['memRole']['is_required'];
if ($memRoleIsOpen && $memRoleIsRequired &&
in_array('role', $inviteWrite) && empty($default_data['role'])) {
E('1009:邀请链接已过期');
return false;
}
// 检查管理权限
$user = User::instance()->getByUid($link['invite_uid']);
$this->checkCurrentInvitePower($user);
return $user;
}
/**
* 检查是否有审核权限
* @param $inviteUser
* @return bool
*/
protected function _hasCheckPower($inviteUser)
{
// 读取邀请配置
$settingService = new InviteSettingService();
$data = $settingService->get_by_conds([]);
// 如果配置为空, 则说明配置有问题
if (empty($data)) {
return false;
}
// 当前记录的审核权限
$auths = explode(',', $inviteUser['udpid']);
// 如果用户uid在审核权限中
$approveInviter = InviteSettingModel::APPROVE_INVITER & $data['check_type'];
if (InviteSettingModel::APPROVE_INVITER == $approveInviter && in_array($this->_login->user['memUid'], $auths)) {
return true;
}
// 判断负责人
$approveLeader = InviteSettingModel::APPROVE_LEADER & $data['check_type'];
if (InviteSettingModel::APPROVE_LEADER == $approveLeader) {
$departments = Department::instance()->listAll();
foreach ($auths as $_id) {
if (empty($departments[$_id])) {
continue;
}
if ($this->_login->user['memUid'] == $departments[$_id]['dpLead']) {
return true;
}
}
}
return false;
}
/**
* 把默认数据追加到用户提交数据中
* @param $data
* @param $form
* @return bool
*/
protected function _addDefaultData(&$form, $data)
{
if (!empty($data['job'])) {
$form['memJob'] = array(
'type' => '11',
'field_name' => 'memJob',
'attr_name' => '职位',
'attr_value' => $data['job'],
'option' => array(
array(
'name' => $data['job'],
'value' => $data['job']
)
)
);
}
if (!empty($data['role'])) {
$form['memRole'] = array(
'type' => '11',
'field_name' => 'memJob',
'attr_name' => '角色',
'attr_value' => $data['role'],
'option' => array(
array(
'name' => $data['role'],
'value' => $data['role']
)
)
);
}
return true;
}
/**
* 检查邀请数据是否正常
* @param $data
* @return bool
*/
protected function _checkDefaultData($data)
{
if (empty($data) || empty($data['department']) || empty($data['department']['dpId'])) {
E('1009:邀请链接非法');
}
$department = Department::instance()->getById($data['department']['dpId']);
if (empty($department)) {
E('1009:邀请失效');
}
// 读取字段配置
$attrService = new AttrService();
$fields = $attrService->getAttrList(true, array(), false, false);
$fields = array_combine_by_key($fields, 'field_name');
// 读取邀请配置
$settingService = new InviteSettingService();
$setting = $settingService->get_by_conds([]);
if (empty($setting)) {
E('1007:请通知管理员配置邀请设置');
return false;
}
if (empty($setting['inviter_write'])) {
$inviteWrite = array();
} else {
$inviteWrite = unserialize($setting['inviter_write']);
}
if (in_array('job', $inviteWrite) && empty($data['job']) && 1 == $fields['memJob']['is_open']) {
E('1009:邀请链接已过期');
}
if (in_array('role', $inviteWrite) && empty($data['role']) && 1 == $fields['memRole']['is_open']) {
E('1009:邀请链接已过期');
}
return true;
}
}