UserRoleService.php 2.4 KB

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