UserRoleService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Models\ActionLogModel;
  14. use App\Services\BaseService;
  15. /**
  16. * 用户角色关系-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class UserRoleService
  20. * @package App\Services\Common
  21. */
  22. class UserRoleService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * UserRoleService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new UserRoleModel();
  33. }
  34. /**
  35. * 根据用户ID获取角色列表
  36. * @param $userId 用户ID
  37. * @return mixed
  38. * @author laravel开发员
  39. * @since 2020/11/11
  40. */
  41. public function getUserRoleList($userId)
  42. {
  43. $userRoleModel = new UserRoleModel();
  44. $roleList = $userRoleModel::from("role as r")
  45. ->select('r.*')
  46. ->join('user_role as ur', 'ur.role_id', '=', 'r.id')
  47. ->distinct(true)
  48. ->where('ur.user_id', '=', $userId)
  49. ->where('r.status', '=', 1)
  50. ->where('r.mark', '=', 1)
  51. ->orderBy('r.sort')
  52. ->get()->toArray();
  53. return $roleList;
  54. }
  55. /**
  56. * 删除用户角色关系数据
  57. * @param $userId 用户ID
  58. * @since 2020/11/11
  59. * @author laravel开发员
  60. */
  61. public function deleteUserRole($userId)
  62. {
  63. $this->model->where("user_id", '=', $userId)->delete();
  64. }
  65. /**
  66. * 批量插入用户角色关系数据
  67. * @param $userId 用户ID
  68. * @param $roleIds 角色ID集合
  69. * @author laravel开发员
  70. * @since 2020/11/11
  71. */
  72. public function insertUserRole($userId, $roleIds)
  73. {
  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. /**
  87. * 删除七天之前标记软删除的数据
  88. */
  89. public function delete()
  90. {
  91. // 设置日志标题
  92. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除用户角色关系信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  93. ActionLogModel::record();
  94. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  95. return parent::delete();
  96. }
  97. }