LoginServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package com.xjrsoft.module.system.service.impl;
  2. import cn.dev33.satoken.context.SaHolder;
  3. import cn.dev33.satoken.secure.BCrypt;
  4. import cn.dev33.satoken.session.SaSession;
  5. import cn.dev33.satoken.stp.StpUtil;
  6. import cn.dev33.satoken.temp.SaTempUtil;
  7. import cn.hutool.core.util.IdUtil;
  8. import cn.hutool.core.util.ObjectUtil;
  9. import cn.hutool.core.util.StrUtil;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  12. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  13. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  14. import com.xjrsoft.common.constant.GlobalConstant;
  15. import com.xjrsoft.common.enums.EnabledMark;
  16. import com.xjrsoft.common.enums.RoleEnum;
  17. import com.xjrsoft.common.exception.MyException;
  18. import com.xjrsoft.common.utils.FixedArithmeticCaptcha;
  19. import com.xjrsoft.common.utils.RSAUtil;
  20. import com.xjrsoft.common.utils.RedisUtil;
  21. import com.xjrsoft.common.utils.WeChatUtil;
  22. import com.xjrsoft.config.LicenseConfig;
  23. import com.xjrsoft.module.organization.entity.Department;
  24. import com.xjrsoft.module.organization.entity.Post;
  25. import com.xjrsoft.module.organization.entity.User;
  26. import com.xjrsoft.module.organization.entity.UserDeptRelation;
  27. import com.xjrsoft.module.organization.entity.UserPostRelation;
  28. import com.xjrsoft.module.organization.entity.UserRoleRelation;
  29. import com.xjrsoft.module.organization.mapper.UserRoleRelationMapper;
  30. import com.xjrsoft.module.organization.service.IDepartmentService;
  31. import com.xjrsoft.module.organization.service.IPostService;
  32. import com.xjrsoft.module.organization.service.IUserDeptRelationService;
  33. import com.xjrsoft.module.organization.service.IUserPostRelationService;
  34. import com.xjrsoft.module.organization.service.IUserService;
  35. import com.xjrsoft.module.system.dto.CreateTokenDto;
  36. import com.xjrsoft.module.system.dto.LoginByCodeDto;
  37. import com.xjrsoft.module.system.dto.LoginCaptchaDto;
  38. import com.xjrsoft.module.system.dto.LoginDto;
  39. import com.xjrsoft.module.system.dto.LoginQRCodeDto;
  40. import com.xjrsoft.module.system.service.ILoginService;
  41. import com.xjrsoft.module.system.vo.CreateTokenVo;
  42. import com.xjrsoft.module.system.vo.ImgCaptchaVo;
  43. import com.xjrsoft.module.system.vo.LoginByCodeVo;
  44. import com.xjrsoft.module.system.vo.LoginVo;
  45. import com.xjrsoft.module.system.vo.WeChatUserInfo;
  46. import lombok.AllArgsConstructor;
  47. import org.springframework.stereotype.Service;
  48. import java.util.List;
  49. import java.util.UUID;
  50. import java.util.stream.Collectors;
  51. /**
  52. * @Author: tzx
  53. * @Date: 2023/4/21 14:22
  54. */
  55. @Service
  56. @AllArgsConstructor
  57. public class LoginServiceImpl implements ILoginService {
  58. private final IUserService userService;
  59. private final IUserDeptRelationService userDeptRelationService;
  60. private final IUserPostRelationService userPostRelationService;
  61. private final IPostService postService;
  62. private final IDepartmentService departmentService;
  63. private final RedisUtil redisUtil;
  64. private final LicenseConfig licenseConfig;
  65. private final WeChatUtil weChatUtil;
  66. private final UserRoleRelationMapper userRoleRelationMapper;
  67. @Override
  68. public LoginVo login(LoginDto dto) throws Exception {
  69. if (licenseConfig.getEnabled()) {
  70. //查出所有在线用户
  71. List<String> onlineUser = StpUtil.searchSessionId("", 0, -1, true);
  72. //如果已经登录人数超过授权人数 不允许登录
  73. if (onlineUser.size() >= licenseConfig.getLoginMax()) {
  74. throw new MyException("登录人数超过授权人数,无法登录,请联系管理员!");
  75. }
  76. }
  77. String captchaCode = redisUtil.get(dto.getKey(), 0);
  78. if (captchaCode == null) {
  79. throw new MyException("验证码已过期,请刷新验证码!");
  80. }
  81. if (!captchaCode.equals(dto.getCode())) {
  82. throw new MyException("验证码不正确,请刷新验证码!");
  83. }
  84. // rsa解密
  85. String decryptData = RSAUtil.decrypt(dto.getPassword());
  86. dto.setPassword(decryptData);
  87. User loginUser = userService.getOne(
  88. Wrappers.lambdaQuery(User.class)
  89. .eq(User::getUserName, dto.getUserName())
  90. .or()
  91. .eq(User::getMobile, dto.getUserName()));
  92. if (loginUser == null || !BCrypt.checkpw(dto.getPassword(), loginUser.getPassword())) {
  93. throw new MyException("账号或密码不正确");
  94. }
  95. return getLoginInfo(loginUser, "PC");
  96. }
  97. @Override
  98. public LoginByCodeVo loginByCode(LoginByCodeDto dto) throws Exception {
  99. WeChatUserInfo weChatUserInfo;
  100. if (dto.getType() == 0) {
  101. weChatUserInfo = weChatUtil.getMpOpenid(dto.getCode());
  102. }else{
  103. weChatUserInfo = weChatUtil.getOpenid(dto.getCode());
  104. }
  105. LoginByCodeVo result = new LoginByCodeVo(){{
  106. setOpenId(weChatUserInfo.getOpenid());
  107. setUnionId(weChatUserInfo.getUnionid());
  108. }};
  109. if (weChatUserInfo == null) throw new MyException("code无效");
  110. LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
  111. queryWrapper.eq(User::getUnionId, weChatUserInfo.getUnionid());
  112. User loginUser = userService.getOne(queryWrapper);
  113. if (loginUser == null) {
  114. return result;
  115. }
  116. LoginVo loginVo = getLoginInfo(loginUser, "WX-MP");
  117. result.setToken(loginVo.getToken());
  118. result.setUserType(loginVo.getUserType());
  119. return result;
  120. }
  121. @Override
  122. public LoginByCodeVo bindOpenid(LoginByCodeDto dto) throws Exception {
  123. LoginByCodeVo result = new LoginByCodeVo();
  124. WeChatUserInfo weChatUserInfo = weChatUtil.getMpOpenid(dto.getCode());
  125. if (weChatUserInfo == null) throw new MyException("code无效");
  126. if (StrUtil.isEmpty(weChatUserInfo.getUnionid())) {
  127. throw new MyException("无法获取Uid" + weChatUserInfo.getOpenid() + "-" + weChatUserInfo.getUnionid());
  128. }
  129. List<User> userList = userService.list(Wrappers.lambdaQuery(User.class).eq(User::getUnionId, weChatUserInfo.getUnionid()));
  130. if (userList == null || userList.isEmpty()) throw new MyException("code无效");
  131. User user = userList.get(0);
  132. user.setOpenId(weChatUserInfo.getOpenid());
  133. userService.updateById(user);
  134. result.setToken(weChatUserInfo.getOpenid());
  135. return result;
  136. }
  137. /**
  138. * 手机验证码登录
  139. *
  140. * @param dto
  141. * @return
  142. * @throws Exception
  143. */
  144. @Override
  145. public LoginVo loginByCaptcha(LoginCaptchaDto dto) throws Exception {
  146. User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getMobile, dto.getMobile()), false);
  147. if (user == null) {
  148. throw new MyException("用户不存在!");
  149. }
  150. return getLoginInfo(user, "Captcha");
  151. }
  152. /**
  153. * 图形验证码
  154. */
  155. @Override
  156. public ImgCaptchaVo imgCaptcha() {
  157. // 算术类型
  158. FixedArithmeticCaptcha captcha = new FixedArithmeticCaptcha(130, 48);
  159. String rKey = GlobalConstant.LOGIN_IMG_CAPTCHA + UUID.randomUUID().toString();
  160. // 存入redis并设置过期时间为30分钟
  161. redisUtil.set(rKey, captcha.text(), 30 * 60);
  162. return new ImgCaptchaVo(rKey, captcha.toBase64());
  163. }
  164. @Override
  165. public LoginVo loginQRCode(LoginQRCodeDto dto) throws Exception {
  166. if (licenseConfig.getEnabled()) {
  167. //查出所有在线用户
  168. List<String> onlineUser = StpUtil.searchSessionId("", 0, -1, true);
  169. //如果已经登录人数超过授权人数 不允许登录
  170. if (onlineUser.size() >= licenseConfig.getLoginMax()) {
  171. throw new MyException("登录人数超过授权人数,无法登录,请联系管理员!");
  172. }
  173. }
  174. User loginUser = userService.getOne(
  175. Wrappers.lambdaQuery(User.class)
  176. .eq(User::getOpenId, dto.getOpenId())
  177. );
  178. if (loginUser == null) {
  179. throw new MyException("未能查询到您的用户信息");
  180. }
  181. return getLoginInfo(loginUser, "PC");
  182. }
  183. private LoginVo getLoginInfo(User loginUser, String loginType) throws Exception {
  184. LoginVo result = new LoginVo();
  185. if (loginUser.getEnabledMark() == EnabledMark.DISABLED.getCode()) {
  186. throw new MyException("账户未启用");
  187. }
  188. //此登录接口登录web端
  189. StpUtil.login(loginUser.getId(), loginType);
  190. // 获取用户类型(根据固定角色进行匹配)
  191. List<UserRoleRelation> relations = userRoleRelationMapper.selectList(Wrappers.lambdaQuery(UserRoleRelation.class)
  192. .select(UserRoleRelation::getRoleId)
  193. .eq(UserRoleRelation::getUserId, StpUtil.getLoginIdAsLong()));
  194. result.setUserType(roleMatching(relations));
  195. result.setIsChangePassword(loginUser.getIsChangePassword());
  196. SaSession tokenSession = StpUtil.getTokenSession();
  197. List<UserDeptRelation> userDeptRelations = userDeptRelationService.list(Wrappers.lambdaQuery(UserDeptRelation.class)
  198. .eq(UserDeptRelation::getUserId, StpUtil.getLoginIdAsLong()));
  199. List<UserPostRelation> userPostRelations = userPostRelationService.list(Wrappers.lambdaQuery(UserPostRelation.class)
  200. .eq(UserPostRelation::getUserId, StpUtil.getLoginIdAsLong()));
  201. //获取登陆人所选择的身份缓存
  202. String postId = redisUtil.get(GlobalConstant.LOGIN_IDENTITY_CACHE_PREFIX + loginUser.getId());
  203. Post post = new Post();
  204. if (StrUtil.isNotBlank(postId) && !postId.equals("null")) {
  205. post = postService.getById(Long.valueOf(postId));
  206. }
  207. if (userPostRelations.size() > 0) {
  208. List<Long> postIds = userPostRelations.stream().map(UserPostRelation::getPostId).collect(Collectors.toList());
  209. List<Post> postList = postService.listByIds(postIds);
  210. if (StrUtil.isBlank(postId) && CollectionUtils.isNotEmpty(postList)) {
  211. post = postList.get(0);
  212. }
  213. tokenSession.set(GlobalConstant.LOGIN_USER_POST_INFO_KEY, post);
  214. tokenSession.set(GlobalConstant.LOGIN_USER_POST_LIST_KEY, postList);
  215. loginUser.setPostId(post.getId());
  216. //将登陆人所选择的身份缓存起来
  217. //切换身份的时候 会一起修改
  218. redisUtil.set(GlobalConstant.LOGIN_IDENTITY_CACHE_PREFIX + loginUser.getId(), post.getId());
  219. }
  220. if (userDeptRelations.size() > 0) {
  221. // 存当前用户所有部门到缓存
  222. List<Long> departmentIds = userDeptRelations.stream().map(UserDeptRelation::getDeptId).collect(Collectors.toList());
  223. List<Department> departmentList = departmentService.listByIds(departmentIds);
  224. tokenSession.set(GlobalConstant.LOGIN_USER_DEPT_LIST_KEY, departmentList);
  225. //如果此人有岗位 使用岗位的deptId 找到当前组织机构
  226. if (post != null && ObjectUtil.isNotNull(post.getId())) {
  227. Department department = departmentService.getById(post.getDeptId());
  228. tokenSession.set(GlobalConstant.LOGIN_USER_DEPT_INFO_KEY, department);
  229. loginUser.setDepartmentId(department.getId());
  230. } else if (departmentList.size() > 0) {
  231. Department department = departmentList.get(0);
  232. tokenSession.set(GlobalConstant.LOGIN_USER_DEPT_INFO_KEY, department);
  233. loginUser.setDepartmentId(department.getId());
  234. }
  235. }
  236. //根据登录信息 将post 和 department 信息存入用户信息中
  237. tokenSession.set(GlobalConstant.LOGIN_USER_INFO_KEY, loginUser);
  238. result.setToken(StpUtil.getTokenValue());
  239. // 判断是不是OAuth2
  240. String oauth2Info = SaHolder.getRequest().getCookieValue("Oauth2Info");
  241. if (oauth2Info != null) {
  242. result.setRedirectUri(redisUtil.get(oauth2Info));
  243. }
  244. return result;
  245. }
  246. @Override
  247. public CreateTokenVo createToken(CreateTokenDto dto) {
  248. CreateTokenVo vo = new CreateTokenVo();
  249. if (dto.getExpire() == -1) {
  250. String token = SaTempUtil.createToken(IdUtil.fastSimpleUUID() + StringPool.UNDERSCORE + GlobalConstant.SECRET_KEY, Integer.MAX_VALUE);
  251. vo.setToken(token);
  252. return vo;
  253. } else {
  254. String token = SaTempUtil.createToken(IdUtil.fastSimpleUUID() + StringPool.UNDERSCORE + GlobalConstant.SECRET_KEY, dto.getExpire());
  255. vo.setToken(token);
  256. return vo;
  257. }
  258. }
  259. /**
  260. * 角色匹配
  261. *
  262. * @return
  263. */
  264. private Long roleMatching(List<UserRoleRelation> relations) {
  265. for (UserRoleRelation role : relations) {
  266. if (role.getRoleId() == RoleEnum.PARENT.getCode()) {
  267. return RoleEnum.PARENT.getCode();
  268. }
  269. if (role.getRoleId() == RoleEnum.TEACHER.getCode()) {
  270. return RoleEnum.TEACHER.getCode();
  271. }
  272. if (role.getRoleId() == RoleEnum.STUDENT.getCode()) {
  273. return RoleEnum.STUDENT.getCode();
  274. }
  275. if (role.getRoleId() == RoleEnum.ADMIN.getCode()) {
  276. return RoleEnum.ADMIN.getCode();
  277. }
  278. }
  279. return 0L;
  280. }
  281. }