UserRole.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. // 过滤掉无效的roleId
  27. $roleModel = new Role();
  28. $validRoles = $roleModel->select("id")->whereIn("id", $roles)->where("is_del", false)->get();
  29. $validRoleIds = [];
  30. foreach ($validRoles as $validRole) {
  31. array_push($validRoleIds, $validRole["id"]);
  32. }
  33. if (count($validRoleIds) < 1) {
  34. return ["code" => SUCCESS, "data" => ["roles" => []]];
  35. }
  36. // 重新添加用户角色新配置的角色数据
  37. $data = [];
  38. foreach ($validRoleIds as $role) {
  39. array_push($data, ["user_id" => $uid, "role_id" => $role]);
  40. }
  41. $this->insert($data);
  42. // 查出批量插入的数据,并返回给前端
  43. $new_data = $this->select("id", "user_id", "role_id", "status")->where("user_id", $uid)->where("is_del", false)
  44. ->orderBy("role_id", "asc")->get();
  45. $rids = [];
  46. foreach ($new_data as $nd) {
  47. array_push($rids, $nd["role_id"]);
  48. }
  49. if (count($rids) > 0) {
  50. $role = new Role();
  51. $roleInfo = $role->LoadRoleByIds_KV($rids);
  52. }
  53. $result = ["roles" => $new_data, "roleInfo" => $roleInfo];
  54. return ["code" => SUCCESS, "data" => $result];
  55. }
  56. /**
  57. * 配置角色下的用户数据,当前的做法是,
  58. * 每次更新都先把roleId匹配的数据全部标记删除
  59. * 然后批量插入新的配置的用户数据
  60. *
  61. * @param $rid
  62. * @param array $userIds
  63. * @return array
  64. */
  65. public function AssignRoleUsers($rid, array $userIds)
  66. {
  67. if ($rid < 1) {
  68. return ["code" => INVALID_ROLE_ID];
  69. }
  70. // 删除当前角色所有的用户
  71. $this->where("role_id", $rid)->where("is_del", false)->update(["is_del" => true]);
  72. // 过滤掉无效的userId
  73. $userModel = new User();
  74. $validUsers = $userModel->select("id")->whereIn("id", $userIds)->where("is_del", false)->get();
  75. $validUids = [];
  76. foreach ($validUsers as $validUser) {
  77. array_push($validUids, $validUser["id"]);
  78. }
  79. if (count($validUids) < 1) {
  80. return ["code" => SUCCESS, "data" => ["users" => []]];
  81. }
  82. // 重新添加指定角色下的用户数据
  83. $data = [];
  84. foreach ($validUids as $userId) {
  85. array_push($data, ["user_id" => $userId, "role_id" => $rid]);
  86. }
  87. $this->insert($data);
  88. //查出批量插入的数据,并返回给前端
  89. $new_data = $this->select("id", "user_id", "role_id", "status")->where("role_id", $rid)->where("is_del", false)
  90. ->orderBy("user_id", "asc")->get();
  91. $uids = [];
  92. foreach ($new_data as $nd) {
  93. array_push($uids, $nd["user_id"]);
  94. }
  95. if (count($uids) > 0) {
  96. $user = new User();
  97. $userInfo = $user->ListUserByIds_KV($uids);
  98. }
  99. $result = ["users" => $new_data, "userInfo" => $userInfo];
  100. return ["code" => SUCCESS, "data" => $result];
  101. }
  102. public function LoadRoleByUid($uid)
  103. {
  104. $role = $this->select("user_id", "role_id", "status")
  105. ->where("user_id", $uid)
  106. ->where("is_del", false)
  107. ->first();
  108. return $role;
  109. }
  110. /**
  111. * 加载指定用户拥有的角色信息
  112. *
  113. * @param $uid
  114. * @return array
  115. */
  116. public function LoadRole($uid)
  117. {
  118. $roles = $this->select("id", "user_id", "role_id", "status")
  119. ->where("user_id", $uid)->where("is_del", false)->get();
  120. if (count($roles) == 0) {
  121. return ["code" => 0, "data" => []];
  122. }
  123. $roleIds = [];
  124. foreach ($roles as $role) {
  125. array_push($roleIds, $role["role_id"]);
  126. }
  127. // 加載角色的信息
  128. $role = new Role();
  129. $roleInfo = $role->LoadRoleByIds_KV($roleIds);
  130. $result = ["roleUsers" => $roles, "roleInfo" => $roleInfo];
  131. return ["code" => SUCCESS, "data" => $result];
  132. }
  133. /**
  134. * 根据角色id,列出用户-角色数据
  135. *
  136. * @param array $rids 多个角色id
  137. * @return array
  138. */
  139. public function LoadUsersWithRoles(array $rids)
  140. {
  141. $userRoles = $this->select("id", "user_id", "role_id", "status")
  142. ->whereIn("role_id", $rids)->where("is_del", false)
  143. ->get();
  144. if (count($userRoles) == 0) {
  145. return [];
  146. }
  147. // 根据不同角色收集各自的user_id,同时收集所有的用户id,用于查出用户信息
  148. $allUids = [];
  149. $roleUsers = [];
  150. foreach ($userRoles as $userRole) {
  151. $key = $userRole["role_id"];
  152. $userId = $userRole["user_id"];
  153. if (!key_exists($key, $roleUsers)) {
  154. $roleUsers[$key] = [];
  155. }
  156. $tmpUsers = $roleUsers[$key];
  157. array_push($tmpUsers, $userId);
  158. $roleUsers[$key] = $tmpUsers;
  159. array_push($allUids, $userId);
  160. }
  161. // 这个if正常情况下是不会进入的,前面已经有if了。
  162. if (count($allUids) == 0) {
  163. return [];
  164. }
  165. // load user info
  166. $userObj = new User();
  167. $userInfo = $userObj->ListUserByIds_KV($allUids);
  168. $result = [];
  169. // 每个角色分别收取自己拥有的用户信息
  170. foreach ($roleUsers as $roleId => $uids) {
  171. $tmpUsers = [];
  172. foreach ($uids as $uid) {
  173. if (!key_exists($uid, $userInfo)) {
  174. continue;
  175. }
  176. array_push($tmpUsers, $userInfo[$uid]);
  177. }
  178. $result[$roleId] = $tmpUsers;
  179. }
  180. return $result;
  181. }
  182. }