UserRoleService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\UserRoleModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 用户角色关系-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class UserRoleService
  19. * @package App\Services\Common
  20. */
  21. class UserRoleService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * UserRoleService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new UserRoleModel();
  32. }
  33. /**
  34. * 根据用户ID获取角色列表
  35. * @param $userId 用户ID
  36. * @return mixed
  37. * @author laravel开发员
  38. * @since 2020/11/11
  39. */
  40. public function getUserRoleList($userId)
  41. {
  42. $userRoleModel = new UserRoleModel();
  43. $roleList = $userRoleModel::from("role as r")
  44. ->select('r.*')
  45. ->join('user_role as ur', 'ur.role_id', '=', 'r.id')
  46. ->distinct(true)
  47. ->where('ur.user_id', '=', $userId)
  48. ->where('r.status', '=', 1)
  49. ->where('r.mark', '=', 1)
  50. ->orderBy('r.sort')
  51. ->get()->toArray();
  52. return $roleList;
  53. }
  54. /**
  55. * 删除用户角色关系数据
  56. * @param $userId 用户ID
  57. * @since 2020/11/11
  58. * @author laravel开发员
  59. */
  60. public function deleteUserRole($userId)
  61. {
  62. $this->model->where("user_id", '=', $userId)->delete();
  63. }
  64. /**
  65. * 批量插入用户角色关系数据
  66. * @param $userId 用户ID
  67. * @param $roleIds 角色ID集合
  68. * @author laravel开发员
  69. * @since 2020/11/11
  70. */
  71. public function insertUserRole($userId, $roleIds)
  72. {
  73. $roleIds = is_array($roleIds)? $roleIds : ($roleIds? [$roleIds] : []);
  74. if (!empty($roleIds)) {
  75. $list = [];
  76. foreach ($roleIds as $val) {
  77. $data = [
  78. 'user_id' => $userId,
  79. 'role_id' => $val,
  80. ];
  81. $list[] = $data;
  82. }
  83. $this->model->insert($list);
  84. }
  85. }
  86. }