123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package thyyxxk.webserver.service;
- import cn.hutool.crypto.SecureUtil;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.dynamic.datasource.annotation.DS;
- import lombok.Data;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import thyyxxk.webserver.config.exception.ExceptionEnum;
- import thyyxxk.webserver.dao.his.LoginDao;
- import thyyxxk.webserver.entity.ResultVo;
- import thyyxxk.webserver.entity.dictionary.CodeName;
- import thyyxxk.webserver.entity.login.UserInfo;
- import thyyxxk.webserver.entity.settings.IntergrationMenu;
- import thyyxxk.webserver.service.externalhttp.CorpWxSrvc;
- import thyyxxk.webserver.service.externalhttp.WebSocketService;
- import thyyxxk.webserver.service.outpatient.wxapi.SendWxInfoService;
- import thyyxxk.webserver.service.redislike.RedisLikeService;
- import thyyxxk.webserver.service.settings.MenuSettingsService;
- import thyyxxk.webserver.utils.*;
- import javax.servlet.http.HttpServletRequest;
- import java.util.*;
- /**
- * @author dj
- */
- @Slf4j
- @Service
- public class LoginService {
- private final LoginDao dao;
- private final TokenService tokenService;
- private final RedisLikeService redisLikeService;
- private final CorpWxSrvc srvc;
- private final PublicServer publicServer;
- private final SendWxInfoService sendWxInfoService;
- private final MenuSettingsService menuSettingsService;
- @Autowired
- public LoginService(LoginDao dao,
- TokenService tokenService,
- RedisLikeService redisLikeService,
- CorpWxSrvc srvc,
- PublicServer publicServer,
- SendWxInfoService sendWxInfoService, MenuSettingsService menuSettingsService) {
- this.dao = dao;
- this.tokenService = tokenService;
- this.redisLikeService = redisLikeService;
- this.srvc = srvc;
- this.publicServer = publicServer;
- this.sendWxInfoService = sendWxInfoService;
- this.menuSettingsService = menuSettingsService;
- }
- @Data
- public static class Verification {
- private String codeRs;
- private String code;
- private String newPwd;
- }
- public ResultVo<UserInfo> login(UserInfo userInfo, HttpServletRequest request, boolean encrypt) {
- UserInfo tempUserInfo = dao.findUserByCodeRsFromDjUserBase(userInfo.getCodeRs());
- if (null == tempUserInfo) {
- tempUserInfo = dao.findUserByCodeRsFromEmployeeMi(userInfo.getCodeRs());
- if (null == tempUserInfo) {
- return ResultVoUtil.fail(ExceptionEnum.USER_NOT_EXIST);
- }
- dao.insertNewUserToDjUserBase(tempUserInfo);
- }
- String ip = IpAddressUtil.getIPAddress(request);
- String pwd = encrypt ? SecureUtil.md5(userInfo.getPassword()) : userInfo.getPassword();
- boolean through
- = !Objects.equals("fromTriageScreen", userInfo.getSid())
- && !pwd.equals(tempUserInfo.getPassword());
- if (through) {
- return ResultVoUtil.fail(ExceptionEnum.INVALID_PASSWORD, userInfo);
- }
- String token = TokenUtil.getInstance().createToken(tempUserInfo.getCode());
- try {
- JSONObject json = srvc.getUserinfo(tokenService.getWeComAddressBookToken(), tempUserInfo.getCodeRs());
- tempUserInfo.setAvatar(json.getString("avatar"));
- } catch (Exception ignored) {
- }
- tempUserInfo = redisLikeService.dbUserInfo(tempUserInfo.getCode());
- tempUserInfo.setToken(token);
- tempUserInfo.setIp(ip);
- tempUserInfo.setSid(makeSid(tempUserInfo.getCode(), userInfo.getSid(), ip));
- redisLikeService.setUserInfo(tempUserInfo);
- return ResultVoUtil.success(tempUserInfo);
- }
- public ResultVo<String> sendAVerificationCode(String codeRs) {
- SendWxInfoService.Verification v = sendWxInfoService.sendResetPassword(codeRs);
- if (v.getVerificationCode() != null) {
- dao.updateVerificationCodeByCodeRs(codeRs, v.getVerificationCode(), DateUtil.offsetHour(new Date(), 30));
- return ResultVoUtil.success("验证码已发送。");
- }
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "发送验证码错误,请联系人资,把您企业微信的账号修改为您的工号。");
- }
- public ResultVo<String> checkVerificationCode(String codeRs, String verificationCode) {
- int count = dao.checkVerificationCode(codeRs, verificationCode);
- if (count > 0) {
- dao.updatePasswordByCodeRs(codeRs);
- return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "密码已重置,默认密码为123456");
- }
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "验证码过期或者验证码错误。");
- }
- public ResultVo<String> checkVerificationCodeV2(Verification verification) {
- int count = dao.checkVerificationCode(verification.getCodeRs(), verification.getCode());
- if (count > 0) {
- dao.updatePasswordByCodeRsV2(verification.getCodeRs(), SecureUtil.md5(verification.getNewPwd()));
- return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "验证成功,密码已修改");
- }
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "验证码过期或者验证码错误。");
- }
- public ResultVo<UserInfo> simpleLogin(String code, HttpServletRequest request) {
- UserInfo us = dao.getUserInfoByCode(code);
- if (us == null) {
- return ResultVoUtil.fail(ExceptionEnum.NOT_EL_MESSAGE, "用户不存在");
- }
- return login(us, request, false);
- }
- @DS("his")
- public ResultVo<Map<String, Object>> fetchVueMenus(String code) {
- // 63 只有个人中心的权限
- List<Integer> roles = dao.getUserRoles(code == null ? TokenUtil.getInstance().getTokenUserId() : code);
- if (null == roles || roles.isEmpty()) {
- roles = Collections.singletonList(63);
- } else {
- roles.add(63);
- }
- Map<String, Object> map = new HashMap<>();
- List<IntergrationMenu> list = menuSettingsService.getUserMenu(code);
- map.put("routes", list);
- map.put("paths", dao.selectVueMenusPathByRoles(roles));
- return ResultVoUtil.success(map);
- }
- public ResultVo<List<CodeName>> getWards() {
- String code = TokenUtil.getInstance().getTokenUserId();
- if (publicServer.needRule(2, 8, 52)) {
- return ResultVoUtil.success(dao.getAllWards());
- } else {
- return ResultVoUtil.success(dao.getUserWards(code));
- }
- }
- public ResultVo<List<CodeName>> getAllWards() {
- return ResultVoUtil.success(dao.getAllWards());
- }
- private String makeSid(String code, String sid, String ip) {
- String uuid = UUID.randomUUID().toString().replaceAll("-", "");
- if (StringUtil.notBlank(sid) && "fromTriageScreen".equals(sid)) {
- uuid += "-triageFloorScreen";
- } else {
- uuid = "-" + ip + "-" + uuid;
- }
- return code + uuid;
- }
- public UserInfo getUserInfoByCode() {
- return redisLikeService.getUserInfoByToken();
- }
- }
|