|
|
@@ -5,9 +5,13 @@ import cn.dev33.satoken.secure.BCrypt;
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import cn.xyh.common.config.WxAppConfig;
|
|
|
import cn.xyh.common.constant.CacheConstants;
|
|
|
import cn.xyh.common.constant.Constants;
|
|
|
import cn.xyh.common.core.domain.dto.RoleDTO;
|
|
|
+import cn.xyh.common.core.domain.dto.XcxLoginDto;
|
|
|
import cn.xyh.common.core.domain.entity.SysUser;
|
|
|
import cn.xyh.common.core.domain.event.LogininforEvent;
|
|
|
import cn.xyh.common.core.domain.model.LoginUser;
|
|
|
@@ -15,6 +19,7 @@ import cn.xyh.common.core.domain.model.XcxLoginUser;
|
|
|
import cn.xyh.common.enums.DeviceType;
|
|
|
import cn.xyh.common.enums.LoginType;
|
|
|
import cn.xyh.common.enums.UserStatus;
|
|
|
+import cn.xyh.common.exception.ServiceException;
|
|
|
import cn.xyh.common.exception.user.CaptchaException;
|
|
|
import cn.xyh.common.exception.user.CaptchaExpireException;
|
|
|
import cn.xyh.common.exception.user.UserException;
|
|
|
@@ -49,6 +54,8 @@ public class SysLoginService {
|
|
|
private final SysUserMapper userMapper;
|
|
|
private final ISysConfigService configService;
|
|
|
private final SysPermissionService permissionService;
|
|
|
+ private final ISysUserService userService;
|
|
|
+ private final WxAppConfig wxAppConfig;
|
|
|
|
|
|
@Value("${user.password.maxRetryCount}")
|
|
|
private Integer maxRetryCount;
|
|
|
@@ -116,19 +123,19 @@ public class SysLoginService {
|
|
|
|
|
|
public String xcxLogin(String xcxCode) {
|
|
|
// xcxCode 为 小程序调用 wx.login 授权后获取
|
|
|
- // todo 以下自行实现
|
|
|
// 校验 appid + appSecret + xcxCode 调用登录凭证校验接口 获取 session_key 与 openid
|
|
|
- String openid = "";
|
|
|
+ XcxLoginDto xcxLoginDto = getOpenIdByCode(xcxCode);
|
|
|
|
|
|
// 框架登录不限制从什么表查询 只要最终构建出 LoginUser 即可
|
|
|
- SysUser user = loadUserByOpenid(openid);
|
|
|
-
|
|
|
+ SysUser user = loadUserByOpenid(xcxLoginDto);
|
|
|
+ if (user == null) {
|
|
|
+ // 用户不存在 抛出异常 openId交给全局异常处理 返回401
|
|
|
+ throw new ServiceException(xcxLoginDto.getOpenid(), 401);
|
|
|
+ }
|
|
|
// 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
|
|
|
- XcxLoginUser loginUser = new XcxLoginUser();
|
|
|
- loginUser.setUserId(user.getUserId());
|
|
|
- loginUser.setUsername(user.getUserName());
|
|
|
- loginUser.setUserType(user.getUserType());
|
|
|
- loginUser.setOpenid(openid);
|
|
|
+ XcxLoginUser loginUser = buildXcxLoginUser(user);
|
|
|
+ loginUser.setOpenid(xcxLoginDto.getOpenid());
|
|
|
+ loginUser.setUnionId(xcxLoginDto.getUnionid());
|
|
|
// 生成token
|
|
|
LoginHelper.loginByDevice(loginUser, DeviceType.XCX);
|
|
|
|
|
|
@@ -137,6 +144,7 @@ public class SysLoginService {
|
|
|
return StpUtil.getTokenValue();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 退出登录
|
|
|
*/
|
|
|
@@ -254,20 +262,55 @@ public class SysLoginService {
|
|
|
return userMapper.selectUserByEmail(email);
|
|
|
}
|
|
|
|
|
|
- private SysUser loadUserByOpenid(String openid) {
|
|
|
- // 使用 openid 查询绑定用户 如未绑定用户 则根据业务自行处理 例如 创建默认用户
|
|
|
- // todo 自行实现 userService.selectUserByOpenid(openid);
|
|
|
- SysUser user = new SysUser();
|
|
|
+ private XcxLoginDto getOpenIdByCode(String code) {
|
|
|
+ //通过前端给的code获取openid和unionid
|
|
|
+ String url = StringUtils.format("https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorizatinon_code", wxAppConfig.getAppId(), wxAppConfig.getAppSecret(), code);
|
|
|
+ String data = HttpUtil.get(url);
|
|
|
+ log.info("微信获取获取openid和unionid,返回结果:{}", data);
|
|
|
+ try {
|
|
|
+ XcxLoginDto dto = JSONUtil.toBean(data, XcxLoginDto.class);
|
|
|
+ if (dto.getErrcode() != null) {
|
|
|
+ throw new UserException("user.wx.openid.error");
|
|
|
+ }
|
|
|
+ return dto;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("微信获取获取openid和unionid异常", e);
|
|
|
+ throw new UserException("user.wx.openid.error");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private SysUser loadUserByOpenid(XcxLoginDto wxLoginDto) {
|
|
|
+ // 使用 openid 查询绑定用户
|
|
|
+ SysUser user = userService.selectUserByOpenid(wxLoginDto.getOpenid());
|
|
|
if (ObjectUtil.isNull(user)) {
|
|
|
- log.info("登录用户:{} 不存在.", openid);
|
|
|
- // todo 用户不存在 业务逻辑自行实现
|
|
|
+ log.info("登录用户:{} 不存在.", wxLoginDto.getOpenid());
|
|
|
+ return null;
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
- log.info("登录用户:{} 已被停用.", openid);
|
|
|
- // todo 用户已被停用 业务逻辑自行实现
|
|
|
+ log.info("登录用户:{} 已被停用.", wxLoginDto.getOpenid());
|
|
|
+ throw new UserException("user.blocked", user.getUserName());
|
|
|
}
|
|
|
return user;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建登录用户
|
|
|
+ */
|
|
|
+ private XcxLoginUser buildXcxLoginUser(SysUser user) {
|
|
|
+ XcxLoginUser loginUser = new XcxLoginUser();
|
|
|
+ loginUser.setUserId(user.getUserId());
|
|
|
+ loginUser.setOrgId(user.getOrgId());
|
|
|
+ loginUser.setUsername(user.getUserName());
|
|
|
+ loginUser.setUserType(user.getUserType());
|
|
|
+ loginUser.setMenuPermission(permissionService.getMenuPermission(user));
|
|
|
+ loginUser.setRolePermission(permissionService.getRolePermission(user));
|
|
|
+ loginUser.setOrgName(ObjectUtil.isNull(user.getOrg()) ? "" : user.getOrg().getOrgName());
|
|
|
+ List<RoleDTO> roles = BeanUtil.copyToList(user.getRoles(), RoleDTO.class);
|
|
|
+ loginUser.setRoles(roles);
|
|
|
+ return loginUser;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 构建登录用户
|
|
|
*/
|