| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\UserRoleModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- /**
- * 用户角色关系-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class UserRoleService
- * @package App\Services\Common
- */
- class UserRoleService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * UserRoleService constructor.
- */
- public function __construct()
- {
- $this->model = new UserRoleModel();
- }
- /**
- * 根据用户ID获取角色列表
- * @param $userId 用户ID
- * @return mixed
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function getUserRoleList($userId)
- {
- $userRoleModel = new UserRoleModel();
- $roleList = $userRoleModel::from("role as r")
- ->select('r.*')
- ->join('user_role as ur', 'ur.role_id', '=', 'r.id')
- ->distinct(true)
- ->where('ur.user_id', '=', $userId)
- ->where('r.status', '=', 1)
- ->where('r.mark', '=', 1)
- ->orderBy('r.sort')
- ->get()->toArray();
- return $roleList;
- }
- /**
- * 删除用户角色关系数据
- * @param $userId 用户ID
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function deleteUserRole($userId)
- {
- $this->model->where("user_id", '=', $userId)->delete();
- }
- /**
- * 批量插入用户角色关系数据
- * @param $userId 用户ID
- * @param $roleIds 角色ID集合
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function insertUserRole($userId, $roleIds)
- {
- if (!empty($roleIds)) {
- $list = [];
- foreach ($roleIds as $val) {
- $data = [
- 'user_id' => $userId,
- 'role_id' => $val,
- ];
- $list[] = $data;
- }
- $this->model->insert($list);
- }
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除用户角色关系信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete();
- }
- }
|