123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package com.ruoyi.auth.service;
- import com.ruoyi.common.core.constant.Constants;
- import com.ruoyi.common.core.constant.SecurityConstants;
- import com.ruoyi.common.core.constant.UserConstants;
- import com.ruoyi.common.core.domain.R;
- import com.ruoyi.common.core.enums.UserStatus;
- import com.ruoyi.common.core.exception.ServiceException;
- import com.ruoyi.common.core.utils.StringUtils;
- import com.ruoyi.common.core.web.domain.AjaxResult;
- import com.ruoyi.common.security.utils.SecurityUtils;
- import com.ruoyi.lnst.domain.InLnstLrxx;
- import com.ruoyi.system.api.RemoteLnstService;
- import com.ruoyi.system.api.RemoteUserService;
- import com.ruoyi.system.api.domain.SysUser;
- import com.ruoyi.system.api.model.LoginUser;
- import org.apache.commons.lang3.ObjectUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- /**
- * 登录校验方法
- *
- * @author ruoyi
- */
- @Component
- public class SysLoginService
- {
- @Autowired
- private RemoteUserService remoteUserService;
- @Autowired
- private RemoteLnstService remoteLnstService;
- @Autowired
- private SysPasswordService passwordService;
- @Autowired
- private SysRecordLogService recordLogService;
- /**
- * 登录
- */
- public LoginUser login(String username, String password)
- {
- // 用户名或密码为空 错误
- if (StringUtils.isAnyBlank(username, password))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
- throw new ServiceException("用户/密码必须填写");
- }
- // 密码如果不在指定范围内 错误
- if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
- || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
- throw new ServiceException("用户密码不在指定范围");
- }
- // 用户名不在指定范围内 错误
- if (username.length() < UserConstants.USERNAME_MIN_LENGTH
- || username.length() > UserConstants.USERNAME_MAX_LENGTH)
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
- throw new ServiceException("用户名不在指定范围");
- }
- // 查询用户信息
- R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
- if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
- throw new ServiceException("登录用户:" + username + " 不存在");
- }
- if (R.FAIL == userResult.getCode())
- {
- throw new ServiceException(userResult.getMsg());
- }
- LoginUser userInfo = userResult.getData();
- SysUser user = userResult.getData().getSysUser();
- if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
- throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
- }
- if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
- throw new ServiceException("对不起,您的账号:" + username + " 已停用");
- }
- passwordService.validate(user, password);
- recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
- return userInfo;
- }
- public LoginUser loginMini(String username, String password)
- {
- // 用户名或密码为空 错误
- if (StringUtils.isAnyBlank(username, password))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
- throw new ServiceException("用户/密码必须填写");
- }
- // 查询用户信息
- R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
- if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
- throw new ServiceException("登录用户:" + username + " 不存在");
- }
- if (R.FAIL == userResult.getCode())
- {
- throw new ServiceException(userResult.getMsg());
- }
- LoginUser userInfo = userResult.getData();
- SysUser user = userResult.getData().getSysUser();
- if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
- throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
- }
- if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
- throw new ServiceException("对不起,您的账号:" + username + " 已停用");
- }
- passwordService.validate(user, password);
- recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
- return userInfo;
- }
- public void logout(String loginName)
- {
- recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
- }
- /**
- * 注册
- */
- public void register(String username, String password)
- {
- // 用户名或密码为空 错误
- if (StringUtils.isAnyBlank(username, password))
- {
- throw new ServiceException("用户/密码必须填写");
- }
- if (username.length() < UserConstants.USERNAME_MIN_LENGTH
- || username.length() > UserConstants.USERNAME_MAX_LENGTH)
- {
- throw new ServiceException("账户长度必须在2到20个字符之间");
- }
- if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
- || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
- {
- throw new ServiceException("密码长度必须在5到20个字符之间");
- }
- // 注册用户信息
- SysUser sysUser = new SysUser();
- sysUser.setUserName(username);
- sysUser.setNickName(username);
- sysUser.setPassword(SecurityUtils.encryptPassword(password));
- R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
- if (R.FAIL == registerResult.getCode())
- {
- throw new ServiceException(registerResult.getMsg());
- }
- recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
- }
- /**
- * 养老食堂专用注册和登录
- * @param username
- * @param password
- * @param openId
- * @return
- */
- public LoginUser login(String username, String password, String openId)
- {
- // 用户名或密码为空 错误
- if (StringUtils.isAnyBlank(username, password))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
- throw new ServiceException("用户/密码必须填写");
- }
- // 用户名不在指定范围内 错误
- if (username.length() < UserConstants.USERNAME_MIN_LENGTH
- || username.length() > UserConstants.USERNAME_MAX_LENGTH)
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
- throw new ServiceException("用户名不在指定范围");
- }
- // 查询用户信息
- R<LoginUser> userResult = remoteUserService.getLrUserInfo(username, SecurityConstants.INNER);
- if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
- {
- recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
- throw new ServiceException("登录用户:" + username + " 不存在");
- }
- if (R.FAIL == userResult.getCode())
- {
- throw new ServiceException(userResult.getMsg());
- }
- LoginUser userInfo = userResult.getData();
- SysUser user = userResult.getData().getSysUser();
- passwordService.validate(user, password);
- recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
- if (StringUtils.isNotEmpty(openId)) {
- if (StringUtils.isNotEmpty(user.getOpenid())) {
- throw new ServiceException("您的账号已经绑定其他微信,请解绑后登录");
- }
- SysUser in = new SysUser();
- in.setUserId(user.getUserId());
- in.setOpenid(openId);
- remoteUserService.updateOpenId(in, SecurityConstants.INNER);
- }
- return userInfo;
- }
- /**
- * 养老食堂专用openId登录
- * @param openId
- * @return
- */
- public LoginUser login(String openId) {
- SysUser query = new SysUser();
- query.setOpenid(openId);
- // 查询用户信息
- R<LoginUser> userResult = remoteUserService.getUserInfoByOpenId(query, SecurityConstants.INNER);
- if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
- recordLogService.recordLogininfor(openId, Constants.LOGIN_FAIL, "登录用户不存在");
- throw new ServiceException("登录用户不存在");
- }
- if (R.FAIL == userResult.getCode()) {
- throw new ServiceException(userResult.getMsg());
- }
- LoginUser userInfo = userResult.getData();
- SysUser user = userResult.getData().getSysUser();
- recordLogService.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, "登录成功");
- return userInfo;
- }
- /**
- * 养老食堂专用解除openId绑定
- * @param
- * @return
- */
- public void wxUnbind() {
- // 查询用户信息
- R<LoginUser> userResult = remoteUserService.getLrUserInfo(SecurityUtils.getUsername(), SecurityConstants.INNER);
- if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
- throw new ServiceException("登录用户不存在");
- }
- if (R.FAIL == userResult.getCode()) {
- throw new ServiceException(userResult.getMsg());
- }
- SysUser user = userResult.getData().getSysUser();
- SysUser in = new SysUser();
- in.setUserId(user.getUserId());
- in.setOpenid(null);
- remoteUserService.updateOpenId(in, SecurityConstants.INNER);
- }
- public LoginUser gzhregister(InLnstLrxx form, String openId)
- {
- // 用户名或密码为空 错误
- if (StringUtils.isAnyBlank(form.getLrZjhm())) {
- recordLogService.recordLogininfor(form.getLrZjhm(), Constants.LOGIN_FAIL, "用户名必须填写");
- throw new ServiceException("用户名必须填写");
- }
- // 用户名不在指定范围内 错误
- if (form.getLrZjhm().length() < UserConstants.USERNAME_MIN_LENGTH
- || form.getLrZjhm().length() > UserConstants.USERNAME_MAX_LENGTH) {
- recordLogService.recordLogininfor(form.getLrZjhm(), Constants.LOGIN_FAIL, "用户名不在指定范围");
- throw new ServiceException("用户名不在指定范围");
- }
- form.setOpenId(openId);
- AjaxResult res = remoteLnstService.add(form, SecurityConstants.INNER);
- String userName = "";
- if (ObjectUtils.isNotEmpty(res)) {
- if (res.containsKey("code") && res.containsKey("data")) {
- if (StringUtils.equals(res.get("code").toString(), "200")) {
- userName = res.get("data").toString();
- }
- }
- }
- if (StringUtils.isEmpty(userName)) {
- throw new ServiceException(res.get("msg").toString());
- }
- return remoteUserService.getLrUserInfo(userName, SecurityConstants.INNER).getData();
- }
- }
|