LoginService.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package thyyxxk.webserver.service;
  2. import cn.hutool.crypto.SecureUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.dynamic.datasource.annotation.DS;
  5. import lombok.Data;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import thyyxxk.webserver.config.exception.ExceptionEnum;
  10. import thyyxxk.webserver.dao.his.LoginDao;
  11. import thyyxxk.webserver.entity.ResultVo;
  12. import thyyxxk.webserver.entity.dictionary.CodeName;
  13. import thyyxxk.webserver.entity.login.UserInfo;
  14. import thyyxxk.webserver.entity.settings.IntergrationMenu;
  15. import thyyxxk.webserver.service.externalhttp.CorpWxSrvc;
  16. import thyyxxk.webserver.service.externalhttp.WebSocketService;
  17. import thyyxxk.webserver.service.outpatient.wxapi.SendWxInfoService;
  18. import thyyxxk.webserver.service.redislike.RedisLikeService;
  19. import thyyxxk.webserver.service.settings.MenuSettingsService;
  20. import thyyxxk.webserver.utils.*;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.util.*;
  23. /**
  24. * @author dj
  25. */
  26. @Slf4j
  27. @Service
  28. public class LoginService {
  29. private final LoginDao dao;
  30. private final TokenService tokenService;
  31. private final RedisLikeService redisLikeService;
  32. private final CorpWxSrvc srvc;
  33. private final PublicServer publicServer;
  34. private final SendWxInfoService sendWxInfoService;
  35. private final MenuSettingsService menuSettingsService;
  36. @Autowired
  37. public LoginService(LoginDao dao,
  38. TokenService tokenService,
  39. RedisLikeService redisLikeService,
  40. CorpWxSrvc srvc,
  41. PublicServer publicServer,
  42. SendWxInfoService sendWxInfoService, MenuSettingsService menuSettingsService) {
  43. this.dao = dao;
  44. this.tokenService = tokenService;
  45. this.redisLikeService = redisLikeService;
  46. this.srvc = srvc;
  47. this.publicServer = publicServer;
  48. this.sendWxInfoService = sendWxInfoService;
  49. this.menuSettingsService = menuSettingsService;
  50. }
  51. @Data
  52. public static class Verification {
  53. private String codeRs;
  54. private String code;
  55. private String newPwd;
  56. }
  57. public ResultVo<UserInfo> login(UserInfo userInfo, HttpServletRequest request, boolean encrypt) {
  58. UserInfo tempUserInfo = dao.findUserByCodeRsFromDjUserBase(userInfo.getCodeRs());
  59. if (null == tempUserInfo) {
  60. tempUserInfo = dao.findUserByCodeRsFromEmployeeMi(userInfo.getCodeRs());
  61. if (null == tempUserInfo) {
  62. return ResultVoUtil.fail(ExceptionEnum.USER_NOT_EXIST);
  63. }
  64. dao.insertNewUserToDjUserBase(tempUserInfo);
  65. }
  66. String ip = IpAddressUtil.getIPAddress(request);
  67. String pwd = encrypt ? SecureUtil.md5(userInfo.getPassword()) : userInfo.getPassword();
  68. boolean through
  69. = !Objects.equals("fromTriageScreen", userInfo.getSid())
  70. && !pwd.equals(tempUserInfo.getPassword());
  71. if (through) {
  72. return ResultVoUtil.fail(ExceptionEnum.INVALID_PASSWORD, userInfo);
  73. }
  74. String token = TokenUtil.getInstance().createToken(tempUserInfo.getCode());
  75. try {
  76. JSONObject json = srvc.getUserinfo(tokenService.getWeComAddressBookToken(), tempUserInfo.getCodeRs());
  77. tempUserInfo.setAvatar(json.getString("avatar"));
  78. } catch (Exception ignored) {
  79. }
  80. tempUserInfo = redisLikeService.dbUserInfo(tempUserInfo.getCode());
  81. tempUserInfo.setToken(token);
  82. tempUserInfo.setIp(ip);
  83. tempUserInfo.setSid(makeSid(tempUserInfo.getCode(), userInfo.getSid(), ip));
  84. redisLikeService.setUserInfo(tempUserInfo);
  85. return ResultVoUtil.success(tempUserInfo);
  86. }
  87. public ResultVo<String> sendAVerificationCode(String codeRs) {
  88. SendWxInfoService.Verification v = sendWxInfoService.sendResetPassword(codeRs);
  89. if (v.getVerificationCode() != null) {
  90. dao.updateVerificationCodeByCodeRs(codeRs, v.getVerificationCode(), DateUtil.offsetHour(new Date(), 30));
  91. return ResultVoUtil.success("验证码已发送。");
  92. }
  93. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "发送验证码错误,请联系人资,把您企业微信的账号修改为您的工号。");
  94. }
  95. public ResultVo<String> checkVerificationCode(String codeRs, String verificationCode) {
  96. int count = dao.checkVerificationCode(codeRs, verificationCode);
  97. if (count > 0) {
  98. dao.updatePasswordByCodeRs(codeRs);
  99. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "密码已重置,默认密码为123456");
  100. }
  101. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "验证码过期或者验证码错误。");
  102. }
  103. public ResultVo<String> checkVerificationCodeV2(Verification verification) {
  104. int count = dao.checkVerificationCode(verification.getCodeRs(), verification.getCode());
  105. if (count > 0) {
  106. dao.updatePasswordByCodeRsV2(verification.getCodeRs(), SecureUtil.md5(verification.getNewPwd()));
  107. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "验证成功,密码已修改");
  108. }
  109. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "验证码过期或者验证码错误。");
  110. }
  111. public ResultVo<UserInfo> simpleLogin(String code, HttpServletRequest request) {
  112. UserInfo us = dao.getUserInfoByCode(code);
  113. if (us == null) {
  114. return ResultVoUtil.fail(ExceptionEnum.NOT_EL_MESSAGE, "用户不存在");
  115. }
  116. return login(us, request, false);
  117. }
  118. @DS("his")
  119. public ResultVo<Map<String, Object>> fetchVueMenus(String code) {
  120. // 63 只有个人中心的权限
  121. List<Integer> roles = dao.getUserRoles(code == null ? TokenUtil.getInstance().getTokenUserId() : code);
  122. if (null == roles || roles.isEmpty()) {
  123. roles = Collections.singletonList(63);
  124. } else {
  125. roles.add(63);
  126. }
  127. Map<String, Object> map = new HashMap<>();
  128. List<IntergrationMenu> list = menuSettingsService.getUserMenu(code);
  129. map.put("routes", list);
  130. map.put("paths", dao.selectVueMenusPathByRoles(roles));
  131. return ResultVoUtil.success(map);
  132. }
  133. public ResultVo<List<CodeName>> getWards() {
  134. String code = TokenUtil.getInstance().getTokenUserId();
  135. if (publicServer.needRule(2, 8, 52)) {
  136. return ResultVoUtil.success(dao.getAllWards());
  137. } else {
  138. return ResultVoUtil.success(dao.getUserWards(code));
  139. }
  140. }
  141. public ResultVo<List<CodeName>> getAllWards() {
  142. return ResultVoUtil.success(dao.getAllWards());
  143. }
  144. private String makeSid(String code, String sid, String ip) {
  145. String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  146. if (StringUtil.notBlank(sid) && "fromTriageScreen".equals(sid)) {
  147. uuid += "-triageFloorScreen";
  148. } else {
  149. uuid = "-" + ip + "-" + uuid;
  150. }
  151. return code + uuid;
  152. }
  153. public UserInfo getUserInfoByCode() {
  154. return redisLikeService.getUserInfoByToken();
  155. }
  156. }