Repository navigation
Expand file tree
/
Copy pathApi_User_UpdateController.php
More file actions
127 lines (107 loc) · 4.19 KB
/
Copy pathApi_User_UpdateController.php
File metadata and controls
127 lines (107 loc) · 4.19 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
<?php
/**
* Created by PhpStorm.
* User: zhangjun
* Date: 03/08/2018
* Time: 5:21 PM
*/
class Api_User_UpdateController extends BaseController
{
private $classNameForRequest = '\Zaly\Proto\Site\ApiUserUpdateRequest';
private $classNameForResponse = '\Zaly\Proto\Site\ApiUserUpdateResponse';
public function rpcRequestClassName()
{
return $this->classNameForRequest;
}
/**
* @param \Zaly\Proto\Site\ApiUserUpdateRequest $request
* @param \Google\Protobuf\Internal\Message $transportData
*/
public function rpc(\Google\Protobuf\Internal\Message $request, \Google\Protobuf\Internal\Message $transportData)
{
$tag = __CLASS__ . '-' . __FUNCTION__;
$response = new \Zaly\Proto\Site\ApiUserUpdateResponse();
try {
$userId = $this->userId;
$values = $request->getValues();
$this->updateUserProfile($userId, $values);
$userProfile = $this->getUserProfileResponse($userId);
$response->setProfile($userProfile);
$this->setRpcError($this->defaultErrorCode, "");
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, $ex);
$errorCode = $this->zalyError->errorFriendUpdate;
$errorInfo = $this->zalyError->getErrorInfo($errorCode);
$this->setRpcError("error.update.user.profile", $errorInfo);
}
$this->rpcReturn($transportData->getAction(), $response);
}
/**
* @param string $userId
* @param array $values
*/
private function updateUserProfile($userId, $values)
{
$updateData = [];
foreach ($values as $v) {
$type = $v->getType();
switch ($type) {
case \Zaly\Proto\Site\ApiUserUpdateType::ApiUserUpdateAvatar:
//update user avatar
$avatar = $v->getAvatar();
$updateData['avatar'] = $avatar;
break;
case \Zaly\Proto\Site\ApiUserUpdateType::ApiUserUpdateNickname:
//update user name
$nickName = $v->getNickname();
if (empty($nickName)) {
throw new Exception("nickname is null");
}
if (mb_strlen($nickName) > 16) {
$nickName = mb_substr($nickName, 0, 16);
}
$updateData['nickname'] = $nickName;
$pinyin = new \Overtrue\Pinyin\Pinyin();
$updateData['nicknameInLatin'] = $pinyin->permalink($nickName, "");
break;
}
}
$where = [
"userId" => $userId
];
$this->ctx->SiteUserTable->updateUserData($where, $updateData);
}
protected function getUserProfileResponse($userId)
{
$profile = $this->getUserSelfProfile($userId);
if (!empty($profile)) {
$publicProfile = new Zaly\Proto\Core\PublicUserProfile();
$publicProfile->setUserId($profile['userId']);
$publicProfile->setAvatar($profile['avatar']);
$publicProfile->setLoginName($profile['loginName']);
$publicProfile->setNickname($profile['nickname']);
$publicProfile->setNicknameInLatin($profile['nicknameInLatin']);
$publicProfile->setRealNickname($profile['nickname']);
if ($profile['availableType']) {
$publicProfile->setAvailableType($profile['availableType']);
} else {
$publicProfile->setAvailableType(\Zaly\Proto\Core\UserAvailableType::UserAvailableNormal);
}
$AllUserProfile = new \Zaly\Proto\Core\AllUserProfile();
$AllUserProfile->setPublic($publicProfile);
$AllUserProfile->setTimeReg($profile['timeReg']);
return $AllUserProfile;
}
return null;
}
protected function getUserSelfProfile($userId)
{
$tag = __CLASS__ . "->" . __FUNCTION__;
try {
return $this->ctx->SiteUserTable->getUserByUserId($userId);
} catch (Exception $e) {
$this->ctx->Wpf_Logger->error($tag, e);
}
return [];
}
}