UserRoleService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if (!empty($roleIds)) {
  74. $list = [];
  75. foreach ($roleIds as $val) {
  76. $data = [
  77. 'user_id' => $userId,
  78. 'role_id' => $val,
  79. ];
  80. $list[] = $data;
  81. }
  82. $this->model->insert($list);
  83. }
  84. }
  85. }