| | |
| | | package org.dromara.web.service; |
| | | |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.dev33.satoken.stp.SaLoginModel; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import me.zhyd.oauth.model.AuthResponse; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import org.dromara.common.core.constant.Constants; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | |
| | | import org.dromara.common.core.domain.R; |
| | | import org.dromara.common.core.domain.dto.RoleDTO; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.enums.DeviceType; |
| | | import org.dromara.common.core.enums.LoginType; |
| | | import org.dromara.common.core.enums.TenantStatus; |
| | | import org.dromara.common.core.enums.UserStatus; |
| | | import org.dromara.common.core.exception.user.UserException; |
| | | import org.dromara.common.core.utils.*; |
| | | import org.dromara.common.core.utils.DateUtils; |
| | | import org.dromara.common.core.utils.MessageUtils; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.log.event.LogininforEvent; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.dromara.system.domain.SysUser; |
| | | import org.dromara.system.domain.bo.SysSocialBo; |
| | | import org.dromara.system.domain.vo.SysSocialVo; |
| | | import org.dromara.system.domain.vo.SysTenantVo; |
| | | import org.dromara.system.domain.vo.SysUserVo; |
| | | import org.dromara.system.mapper.SysUserMapper; |
| | | import org.dromara.system.service.ISysPermissionService; |
| | | import org.dromara.system.service.ISysSocialService; |
| | | import org.dromara.system.service.ISysTenantService; |
| | | import org.dromara.web.domain.vo.LoginVo; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | private final ISysSocialService sysSocialService; |
| | | private final SysUserMapper userMapper; |
| | | |
| | | /** |
| | | * 社交登录 |
| | | * |
| | | * @param source 登录来源 |
| | | * @param authUser 授权响应实体 |
| | | * @return 统一响应实体 |
| | | */ |
| | | public R<String> socialLogin(String source, AuthResponse<AuthUser> authUser) { |
| | | // 判断授权响应是否成功 |
| | | if (!authUser.ok()) { |
| | | return R.fail("对不起,授权信息验证不通过,请退出重试!"); |
| | | } |
| | | AuthUser authUserData = authUser.getData(); |
| | | SysSocialVo social = sysSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid()); |
| | | if (ObjectUtil.isNotNull(social)) { |
| | | SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getUserId, social.getUserId())); |
| | | // 执行登录和记录登录信息操作 |
| | | return loginAndRecord(user.getTenantId(), user.getUserName(), authUserData); |
| | | } else { |
| | | // 判断是否已登录 |
| | | if (!StpUtil.isLogin()) { |
| | | return R.fail("授权失败,请先登录才能绑定"); |
| | | } |
| | | SysSocialBo bo = new SysSocialBo(); |
| | | bo.setUserId(LoginHelper.getUserId()); |
| | | bo.setAuthId(authUserData.getSource() + authUserData.getUuid()); |
| | | bo.setSource(authUserData.getSource()); |
| | | bo.setUserName(authUserData.getUsername()); |
| | | bo.setNickName(authUserData.getNickname()); |
| | | bo.setAvatar(authUserData.getAvatar()); |
| | | bo.setOpenId(authUserData.getUuid()); |
| | | BeanUtils.copyProperties(authUserData.getToken(), bo); |
| | | |
| | | sysSocialService.insertByBo(bo); |
| | | SysUserVo sysUser = loadUserByUsername(LoginHelper.getTenantId(), LoginHelper.getUsername()); |
| | | // 执行登录和记录登录信息操作 |
| | | return loginAndRecord(sysUser.getTenantId(), sysUser.getUserName(), authUserData); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 执行登录和记录登录信息操作 |
| | | * |
| | | * @param tenantId 租户ID |
| | | * @param userName 用户名 |
| | | * @param authUser 授权用户信息 |
| | | * 绑定第三方用户 |
| | | * @param authUserData 授权响应实体 |
| | | * @return 统一响应实体 |
| | | */ |
| | | private R<String> loginAndRecord(String tenantId, String userName, AuthUser authUser) { |
| | | checkTenant(tenantId); |
| | | SysUserVo user = loadUserByUsername(tenantId, userName); |
| | | SaLoginModel model = new SaLoginModel(); |
| | | model.setDevice(DeviceType.PC.getDevice()); |
| | | // 生成token |
| | | LoginHelper.login(buildLoginUser(user), model); |
| | | recordLogininfor(user.getTenantId(), userName, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); |
| | | recordLoginInfo(user.getUserId()); |
| | | return R.ok(StpUtil.getTokenValue()); |
| | | public R<LoginVo> sociaRegister(AuthUser authUserData ){ |
| | | SysSocialBo bo = new SysSocialBo(); |
| | | bo.setUserId(LoginHelper.getUserId()); |
| | | bo.setAuthId(authUserData.getSource() + authUserData.getUuid()); |
| | | bo.setOpenId(authUserData.getUuid()); |
| | | bo.setUserName(authUserData.getUsername()); |
| | | BeanUtils.copyProperties(authUserData, bo); |
| | | BeanUtils.copyProperties(authUserData.getToken(), bo); |
| | | sysSocialService.insertByBo(bo); |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 退出登录 |
| | | */ |