EditController.class.php
3.14 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
<?php
namespace Api\Controller\User;
use Common\Common\User;
use Common\Model\AttrModel;
use Common\Service\AttrService;
use Common\Service\UserService;
class EditController extends AbstractController
{
public function Index_post()
{
// 是否有此人
$uid = I('post.uid', '', 'trim');
$userSDK = new User();
$userData = $userSDK->getByUid($uid);
if (empty($userData)) {
E('_ERR_MEMBER_IS_NOT_EXIST');
}
// 获取人员属性更新数据
$infoList = array_combine_by_key(I('post.list'), 'field_name');
$userUpdateData = $this->getUpdateData($infoList, $userData);
// 保存
$userServ = new UserService();
$userServ->saveUser($uid, $userUpdateData);
return true;
}
/**
* 获取人员属性更新数据
* @param $infoList array 提交数据
* @param $userData array 用户原数据
* @return array
*/
protected function getUpdateData($infoList, $userData)
{
// 获取 人员属性设置
$attrServ = new AttrService();
$attrArr = $attrServ->getAttrList(true, array(), true);
// 更新数据
$userUpdateData = [];
// 需要特殊数据结构处理的字段
$serializeAttr = [
AttrModel::ATTR_TYPE_CHECKBOX,
AttrModel::ATTR_TYPE_PICTURE,
];
foreach ($attrArr as $attr) {
// 提交属性字段值
$fieldName = $attr['field_name'];
$attrValue = $infoList[$fieldName]['attr_value'];
// 原来的值
$originalAttrValue = $userData[$fieldName];
// 不需要修改的字段
$doNotNeedToBeModified = is_null($infoList[$fieldName]);
// 没有变更
$noChange = $originalAttrValue == $attrValue;
// 不显示
$doNotShow = $attr['is_show'] == AttrModel::ATTR_IS_SHOW_FALSE;
// 不能编辑
$doNotModify = $attr['is_allow_user_modify'] == AttrModel::ATTR_NOT_ALLOWED_USER_MODIFY;
if ($doNotNeedToBeModified || $noChange || $doNotShow || $doNotModify) {
continue;
}
// 必填项
if (empty($attrValue) && $attr['is_required_cp'] == AttrModel::ATTR_IS_REQUIRED_TRUE) {
E(L('_ERR_PARAM_CAN_NOT_EMPTY', ['name' => $attr['attr_name']]));
}
if (!in_array($attr['type'], $serializeAttr)) {
$userUpdateData[$fieldName] = $attrValue;
} else {
// 图片、多选项 特殊数据结构处理
$userUpdateData[$fieldName] = serialize(
// 这里的二维空数组 是因为前端在初始化有option字段的时候需要
!empty($attrValue) ? $attrValue : [[], []]);
}
}
// 部门数据
$userUpdateData['dpIdList'] = array_column($userData['dpName'], 'dpId');
// UC修改人员 属性字段不能传空 不然会删除原来的数据
// 所以这里要返回更改的数据 加上 原有的数据
return array_merge($userData, $userUpdateData);
}
}