UserRole.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * 根据角色id,列出用户-角色数据
  98. *
  99. * @param $rids 多个角色id
  100. * @return array
  101. */
  102. public function LoadUsersWithRoles($rids)
  103. {
  104. $user_roles = $this->select("id", "user_id", "role_id", "status")
  105. ->whereIn("role_id", $rids)->where("is_del", false)
  106. ->get();
  107. $uids = [];
  108. foreach ($user_roles as $ur) {
  109. array_push($uids, $ur["user_id"]);
  110. }
  111. if (count($uids) == 0) {
  112. return ["user_role" => [], "userInfo" => null];
  113. }
  114. // load user info
  115. $u = new User();
  116. $users = $u->ListUserByIds_KV($uids);
  117. return ["user_role" => $user_roles, "userInfo" => $users];
  118. }
  119. }