|
@@ -143,25 +143,55 @@ class UserRole extends Model
|
|
/**
|
|
/**
|
|
* 根据角色id,列出用户-角色数据
|
|
* 根据角色id,列出用户-角色数据
|
|
*
|
|
*
|
|
- * @param $rids 多个角色id
|
|
|
|
|
|
+ * @param array $rids 多个角色id
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
- public function LoadUsersWithRoles($rids)
|
|
|
|
|
|
+ public function LoadUsersWithRoles(array $rids)
|
|
{
|
|
{
|
|
- $user_roles = $this->select("id", "user_id", "role_id", "status")
|
|
|
|
|
|
+ $userRoles = $this->select("id", "user_id", "role_id", "status")
|
|
->whereIn("role_id", $rids)->where("is_del", false)
|
|
->whereIn("role_id", $rids)->where("is_del", false)
|
|
->get();
|
|
->get();
|
|
- $uids = [];
|
|
|
|
- foreach ($user_roles as $ur) {
|
|
|
|
- array_push($uids, $ur["user_id"]);
|
|
|
|
|
|
+ if (count($userRoles) == 0) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+ // 根据不同角色收集各自的user_id,同时收集所有的用户id,用于查出用户信息
|
|
|
|
+ $allUids = [];
|
|
|
|
+ $roleUsers = [];
|
|
|
|
+ foreach ($userRoles as $userRole) {
|
|
|
|
+ $key = $userRole["role_id"];
|
|
|
|
+ $userId = $userRole["user_id"];
|
|
|
|
+
|
|
|
|
+ if (!key_exists($key, $roleUsers)) {
|
|
|
|
+ $roleUsers[$key] = [];
|
|
|
|
+ }
|
|
|
|
+ $tmpUsers = $roleUsers[$key];
|
|
|
|
+ array_push($tmpUsers, $userId);
|
|
|
|
+ $roleUsers[$key] = $tmpUsers;
|
|
|
|
+ array_push($allUids, $userId);
|
|
}
|
|
}
|
|
- if (count($uids) == 0) {
|
|
|
|
- return ["user_role" => [], "userInfo" => null];
|
|
|
|
|
|
+
|
|
|
|
+ // 这个if正常情况下是不会进入的,前面已经有if了。
|
|
|
|
+ if (count($allUids) == 0) {
|
|
|
|
+ return [];
|
|
}
|
|
}
|
|
|
|
|
|
// load user info
|
|
// load user info
|
|
- $u = new User();
|
|
|
|
- $users = $u->ListUserByIds_KV($uids);
|
|
|
|
- return ["user_role" => $user_roles, "userInfo" => $users];
|
|
|
|
|
|
+ $userObj = new User();
|
|
|
|
+ $userInfo = $userObj->ListUserByIds_KV($allUids);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $result = [];
|
|
|
|
+ // 每个角色分别收取自己拥有的用户信息
|
|
|
|
+ foreach ($roleUsers as $roleId => $uids) {
|
|
|
|
+ $tmpUsers = [];
|
|
|
|
+ foreach ($uids as $uid) {
|
|
|
|
+ if (!key_exists($uid, $userInfo)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ array_push($tmpUsers, $userInfo[$uid]);
|
|
|
|
+ }
|
|
|
|
+ $result[$roleId] = $tmpUsers;
|
|
|
|
+ }
|
|
|
|
+ return $result;
|
|
}
|
|
}
|
|
}
|
|
}
|