123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class UserRole extends Model
- {
- protected $table = "user_roles";
- public $timestamps = false;
- /**
- * 配置用户的角色,当前的做法是,
- * 每次更新都先把userId匹配的数据全部标记删除
- * 然后批量插入新的配置的角色数据
- *
- * @param $uid
- * @param array $roles
- * @return array
- */
- public function AssignUserRoles($uid, array $roles)
- {
- if ($uid < 1) {
- return ["code" => INVALID_USER_ID];
- }
- // 删除当前用户所有的角色
- $this->where("user_id", $uid)->where("is_del", false)->update(["is_del" => true]);
- if (count($roles) < 1) {
- return ["code" => SUCCESS, "data" => ["roles" => []]];
- }
- // 重新添加用户角色新配置的角色数据
- $data = [];
- foreach ($roles as $role) {
- array_push($data, ["user_id" => $uid, "role_id" => $role]);
- }
- $this->insert($data);
- // 查出批量插入的数据,并返回给前端
- $new_data = $this->select("id", "user_id", "role_id", "status")->where("user_id", $uid)->where("is_del", false)
- ->orderBy("role_id", "asc")->get();
- $rids = [];
- foreach ($new_data as $nd) {
- array_push($rids, $nd["role_id"]);
- }
- if (count($rids) > 0) {
- $role = new Role();
- $roleInfo = $role->LoadRoleByIds_KV($rids);
- }
- $result = ["roles" => $new_data, "roleInfo" => $roleInfo];
- return ["code" => SUCCESS, "data" => $result];
- }
- /**
- * 配置角色下的用户数据,当前的做法是,
- * 每次更新都先把roleId匹配的数据全部标记删除
- * 然后批量插入新的配置的用户数据
- *
- * @param $rid
- * @param array $userIds
- * @return array
- */
- public function AssignRoleUsers($rid, array $userIds)
- {
- if ($rid < 1) {
- return ["code" => INVALID_ROLE_ID];
- }
- // 删除当前角色所有的用户
- $this->where("role_id", $rid)->where("is_del", false)->update(["is_del" => true]);
- if (count($userIds) < 1) {
- return ["code" => SUCCESS, "data" => ["users" => []]];
- }
- // 重新添加指定角色下的用户数据
- $data = [];
- foreach ($userIds as $userId) {
- array_push($data, ["user_id" => $userId, "role_id" => $rid]);
- }
- $this->insert($data);
- //查出批量插入的数据,并返回给前端
- $new_data = $this->select("id", "user_id", "role_id", "status")->where("role_id", $rid)->where("is_del", false)
- ->orderBy("user_id", "asc")->get();
- $uids = [];
- foreach ($new_data as $nd) {
- array_push($uids, $nd["user_id"]);
- }
- if (count($uids) > 0) {
- $user = new User();
- $userInfo = $user->ListUserByIds_KV($uids);
- }
- $result = ["users" => $new_data, "userInfo" => $userInfo];
- return ["code" => SUCCESS, "data" => $result];
- }
- public function LoadRoleByUid($uid)
- {
- $role = $this->select("user_id", "role_id", "status")
- ->where("user_id", $uid)
- ->where("is_del", false)
- ->first();
- return $role;
- }
- /**
- * 根据角色id,列出用户-角色数据
- *
- * @param $rids 多个角色id
- * @return array
- */
- public function LoadUsersWithRoles($rids)
- {
- $user_roles = $this->select("id", "user_id", "role_id", "status")
- ->whereIn("role_id", $rids)->where("is_del", false)
- ->get();
- $uids = [];
- foreach ($user_roles as $ur) {
- array_push($uids, $ur["user_id"]);
- }
- if (count($uids) == 0) {
- return ["user_role" => [], "userInfo" => null];
- }
- // load user info
- $u = new User();
- $users = $u->ListUserByIds_KV($uids);
- return ["user_role" => $user_roles, "userInfo" => $users];
- }
- }
|