LoginServiceImpl.java 14 KB

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