SaveController.class.php
5.52 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 16/9/17
* Time: 22:43
*/
namespace Apicp\Controller\User;
use Com\Rpc;
use Common\Common\User;
use Common\Common\StudyMapRight;
use Common\Model\AttrModel;
use Common\Service\AttrService;
use Common\Service\UserService;
class SaveController extends AbstractController
{
/**
* 用户加入方式: 管理员添加
*/
const ADMIN_ADD_JOIN = 1;
/**
* 【通讯录】人员保存
* @author liyifei
* @time 2016-09-17 22:43:59
*/
public function Index_post()
{
$uid = I('post.uid', '', 'trim');
$list = I('post.list');
$dpIds = I('post.dp_ids');
if (!$this->_managePower()) {
E('_ERR_ADMIN_PERMISSION_DENIED');
}
if (empty($list) || empty($dpIds)) {
E('_ERR_PARAM_IS_NULL');
}
// 以架构接口参数字段为键,拼接用户信息
$attrServ = new AttrService();
$attrs = $attrServ->getAttrList(true, array(), true);
$data = [
'dpIdList' => $dpIds
];
$list = array_combine(array_column($list, 'field_name'), $list);
foreach ($attrs as $attr) {
$fieldName = $attr['field_name'];
// 日期类型 为空时 跳过
if (($attr['type'] == AttrModel::ATTR_TYPE_DATE) &&
empty($list[$fieldName]['attr_value'])) {
continue;
}
// 防止前端未传参时,赋值为null,UC在保存自定义属性的值时,null会导致保存失败!
$data[$fieldName] = $list[$fieldName]['attr_value'] !== null ? $list[$fieldName]['attr_value'] : '';
// 图片、多选项,序列化存储在架构
$serializeAttr = [
AttrModel::ATTR_TYPE_CHECKBOX,
AttrModel::ATTR_TYPE_PICTURE,
];
if (in_array($list[$fieldName]['type'], $serializeAttr) && !empty($data[$fieldName])) {
$data[$fieldName] = serialize($data[$fieldName]);
}
}
// 调用验证接口,验证参数传值是否符合规范
$errors = $attrServ->checkValue($data);
if (!empty($errors)) {
E($errors[0]);
}
// 编辑的时候不修改人员 加入方式
if (empty($uid)) {
// 邀请记录信息
$data['memJoinType'] = self::ADMIN_ADD_JOIN;
if (!empty($this->_login->user['eaRealname'])) {
$data['memJoinInviter'] = $this->_login->user['eaRealname'];
} elseif (!empty($this->_login->user['eaMobile'])) {
$data['memJoinInviter'] = $this->_login->user['eaMobile'];
} elseif (!empty($this->_login->user['eaEmail'])) {
$data['memJoinInviter'] = $this->_login->user['eaEmail'];
} else {
$data['memJoinInviter'] = '';
}
}
// =========学习地图业务逻辑 Start=========
$userSdk = &User::instance();
$user_old = [];
// 编辑前获取用户完整数据,用于编辑后对比组织、岗位、角色变化
if (!empty($uid)) {
$user_old = $userSdk->getByUid($uid);
// 未关注的用户不进行对比
if (isset($user_old['memSubscribeStatus']) && $user_old['memSubscribeStatus'] != User::SUBSCRIBE_STATUS_SUBSCRIBED) {
$user_old = [];
}
}
// =========学习地图业务逻辑 End=========
$userServ = new UserService();
$result = $userServ->saveUser($uid, $data);
// 新增、修改用户成功时,UC返回该用户的完整信息
if (isset($result['memUid'])) {
$uid = $result['memUid'];
}
// =========学习地图业务逻辑 Start=========
if (!empty($user_old)) {
$user_new = $userSdk->getByUid($uid);
// 用户部门、岗位和角色数据有差异
if ($this->_contrastRight($user_old, $user_new)) {
// 获取新的可参与学习的地图ID
$studyMapRight = &StudyMapRight::instance();
$map_ids_old = $studyMapRight->listMapId($user_old);
$map_ids_new = $studyMapRight->listMapId($user_new);
$diff_ids = array_diff($map_ids_new, $map_ids_old);
if (!empty($diff_ids)) {
$url = rpcUrl('/Map/Rpc/User/SendNotice');
$parmas = ['uid' => $uid, 'map_ids' => array_values($diff_ids)];
Rpc::phprpc($url)->invoke('index', $parmas);
}
}
}
// =========学习地图业务逻辑 End=========
$this->_result = [
'uid' => $uid
];
}
/**
* 对比用户部门、岗位和角色数据,有差异则返回true
* @param array $user_old 旧的用户数据
* @param array $user_new 新的用户数据
* @return bool
*/
private function _contrastRight($user_old, $user_new)
{
// 获取旧数据中部门、岗位和角色ID
$old_ids = array_column($user_old['dpName'], 'dpId');
$old_ids[] = $user_old['job']['jobId'];
$old_ids[] = $user_old['role']['roleId'];
// 获取新数据中部门、岗位和角色ID
$new_ids = array_column($user_new['dpName'], 'dpId');
$new_ids[] = $user_new['job']['jobId'];
$new_ids[] = $user_new['role']['roleId'];
return !empty(array_diff($old_ids, $new_ids)) || !empty(array_diff($new_ids, $old_ids));
}
}