UserRole.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. class UserRole extends Model
  7. {
  8. protected $table = "user_roles";
  9. public $timestamps = false;
  10. /**
  11. * 配置用户的角色,当前的做法是,
  12. * 每次更新都先把userId匹配的数据全部标记删除
  13. * 然后批量插入新的配置的角色数据
  14. *
  15. * @param $uid
  16. * @param array $roles
  17. * @return array
  18. */
  19. public function AssignUserRoles($uid, array $roles)
  20. {
  21. if ($uid < 1) {
  22. return ["code" => INVALID_USER_ID];
  23. }
  24. // 删除当前用户所有的角色
  25. $this->where("user_id", $uid)->where("is_del", false)->update(["is_del" => true]);
  26. if (count($roles) < 1) {
  27. return ["code" => SUCCESS, "data" => ["roles" => []]];
  28. }
  29. // 重新添加用户角色新配置的角色数据
  30. $data = [];
  31. foreach ($roles as $role) {
  32. array_push($data, ["user_id" => $uid, "role_id" => $role]);
  33. }
  34. $this->insert($data);
  35. // 查出批量插入的数据,并返回给前端
  36. $new_data = $this->select("id", "user_id", "role_id", "status")->where("user_id", $uid)->where("is_del", false)
  37. ->orderBy("role_id", "asc")->get();
  38. $rids = [];
  39. foreach ($new_data as $nd) {
  40. array_push($rids, $nd["role_id"]);
  41. }
  42. if (count($rids) > 0) {
  43. $role = new Role();
  44. $roleInfo = $role->LoadRoleByIds_KV($rids);
  45. }
  46. $result = ["roles" => $new_data, "roleInfo" => $roleInfo];
  47. return ["code" => SUCCESS, "data" => $result];
  48. }
  49. /**
  50. * 配置角色下的用户数据,当前的做法是,
  51. * 每次更新都先把roleId匹配的数据全部标记删除
  52. * 然后批量插入新的配置的用户数据
  53. *
  54. * @param $rid
  55. * @param array $userIds
  56. * @return array
  57. */
  58. public function AssignRoleUsers($rid, array $userIds)
  59. {
  60. if ($rid < 1) {
  61. return ["code" => INVALID_ROLE_ID];
  62. }
  63. // 删除当前角色所有的用户
  64. $this->where("role_id", $rid)->where("is_del", false)->update(["is_del" => true]);
  65. if (count($userIds) < 1) {
  66. return ["code" => SUCCESS, "data" => ["users" => []]];
  67. }
  68. // 重新添加指定角色下的用户数据
  69. $data = [];
  70. foreach ($userIds as $userId) {
  71. array_push($data, ["user_id" => $userId, "role_id" => $rid]);
  72. }
  73. $this->insert($data);
  74. //查出批量插入的数据,并返回给前端
  75. $new_data = $this->select("id", "user_id", "role_id", "status")->where("role_id", $rid)->where("is_del", false)
  76. ->orderBy("user_id", "asc")->get();
  77. $uids = [];
  78. foreach ($new_data as $nd) {
  79. array_push($uids, $nd["user_id"]);
  80. }
  81. if (count($uids) > 0) {
  82. $user = new User();
  83. $userInfo = $user->ListUserByIds_KV($uids);
  84. }
  85. $result = ["users" => $new_data, "userInfo" => $userInfo];
  86. return ["code" => SUCCESS, "data" => $result];
  87. }
  88. public function LoadRoleByUid($uid)
  89. {
  90. $role = $this->select("user_id", "role_id", "status")
  91. ->where("user_id", $uid)
  92. ->where("is_del", false)
  93. ->first();
  94. return $role;
  95. }
  96. /**
  97. * 加载指定用户拥有的角色信息
  98. *
  99. * @param $uid
  100. * @return array
  101. */
  102. public function LoadRole($uid)
  103. {
  104. $roles = $this->select("id", "user_id", "role_id", "status")
  105. ->where("user_id", $uid)->where("is_del", false)->get();
  106. if (count($roles) == 0) {
  107. return ["code" => 0, "data" => []];
  108. }
  109. $roleIds = [];
  110. foreach ($roles as $role) {
  111. array_push($roleIds, $role["role_id"]);
  112. }
  113. // 加載角色的信息
  114. $role = new Role();
  115. $roleInfo = $role->LoadRoleByIds_KV($roleIds);
  116. $result = ["roles" => $roles, "roleInfo" => $roleInfo];
  117. return ["code" => SUCCESS, "data" => $result];
  118. }
  119. /**
  120. * 根据角色id,列出用户-角色数据
  121. *
  122. * @param array $rids 多个角色id
  123. * @return array
  124. */
  125. public function LoadUsersWithRoles(array $rids)
  126. {
  127. $userRoles = $this->select("id", "user_id", "role_id", "status")
  128. ->whereIn("role_id", $rids)->where("is_del", false)
  129. ->get();
  130. if (count($userRoles) == 0) {
  131. return [];
  132. }
  133. // 根据不同角色收集各自的user_id,同时收集所有的用户id,用于查出用户信息
  134. $allUids = [];
  135. $roleUsers = [];
  136. foreach ($userRoles as $userRole) {
  137. $key = $userRole["role_id"];
  138. $userId = $userRole["user_id"];
  139. if (!key_exists($key, $roleUsers)) {
  140. $roleUsers[$key] = [];
  141. }
  142. $tmpUsers = $roleUsers[$key];
  143. array_push($tmpUsers, $userId);
  144. $roleUsers[$key] = $tmpUsers;
  145. array_push($allUids, $userId);
  146. }
  147. // 这个if正常情况下是不会进入的,前面已经有if了。
  148. if (count($allUids) == 0) {
  149. return [];
  150. }
  151. // load user info
  152. $userObj = new User();
  153. $userInfo = $userObj->ListUserByIds_KV($allUids);
  154. $result = [];
  155. // 每个角色分别收取自己拥有的用户信息
  156. foreach ($roleUsers as $roleId => $uids) {
  157. $tmpUsers = [];
  158. foreach ($uids as $uid) {
  159. if (!key_exists($uid, $userInfo)) {
  160. continue;
  161. }
  162. array_push($tmpUsers, $userInfo[$uid]);
  163. }
  164. $result[$roleId] = $tmpUsers;
  165. }
  166. return $result;
  167. }
  168. }