SysLoginService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package com.ruoyi.auth.service;
  2. import com.ruoyi.common.core.constant.Constants;
  3. import com.ruoyi.common.core.constant.SecurityConstants;
  4. import com.ruoyi.common.core.constant.UserConstants;
  5. import com.ruoyi.common.core.domain.R;
  6. import com.ruoyi.common.core.enums.UserStatus;
  7. import com.ruoyi.common.core.exception.ServiceException;
  8. import com.ruoyi.common.core.utils.StringUtils;
  9. import com.ruoyi.common.core.web.domain.AjaxResult;
  10. import com.ruoyi.common.security.utils.SecurityUtils;
  11. import com.ruoyi.lnst.domain.InLnstLrxx;
  12. import com.ruoyi.system.api.RemoteLnstService;
  13. import com.ruoyi.system.api.RemoteUserService;
  14. import com.ruoyi.system.api.domain.SysUser;
  15. import com.ruoyi.system.api.model.LoginUser;
  16. import org.apache.commons.lang3.ObjectUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Component;
  19. /**
  20. * 登录校验方法
  21. *
  22. * @author ruoyi
  23. */
  24. @Component
  25. public class SysLoginService
  26. {
  27. @Autowired
  28. private RemoteUserService remoteUserService;
  29. @Autowired
  30. private RemoteLnstService remoteLnstService;
  31. @Autowired
  32. private SysPasswordService passwordService;
  33. @Autowired
  34. private SysRecordLogService recordLogService;
  35. /**
  36. * 登录
  37. */
  38. public LoginUser login(String username, String password)
  39. {
  40. // 用户名或密码为空 错误
  41. if (StringUtils.isAnyBlank(username, password))
  42. {
  43. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  44. throw new ServiceException("用户/密码必须填写");
  45. }
  46. // 密码如果不在指定范围内 错误
  47. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  48. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  49. {
  50. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
  51. throw new ServiceException("用户密码不在指定范围");
  52. }
  53. // 用户名不在指定范围内 错误
  54. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  55. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  56. {
  57. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
  58. throw new ServiceException("用户名不在指定范围");
  59. }
  60. // 查询用户信息
  61. R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
  62. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
  63. {
  64. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  65. throw new ServiceException("登录用户:" + username + " 不存在");
  66. }
  67. if (R.FAIL == userResult.getCode())
  68. {
  69. throw new ServiceException(userResult.getMsg());
  70. }
  71. LoginUser userInfo = userResult.getData();
  72. SysUser user = userResult.getData().getSysUser();
  73. if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
  74. {
  75. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  76. throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
  77. }
  78. if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
  79. {
  80. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  81. throw new ServiceException("对不起,您的账号:" + username + " 已停用");
  82. }
  83. passwordService.validate(user, password);
  84. recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
  85. return userInfo;
  86. }
  87. public LoginUser loginMini(String username, String password)
  88. {
  89. // 用户名或密码为空 错误
  90. if (StringUtils.isAnyBlank(username, password))
  91. {
  92. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  93. throw new ServiceException("用户/密码必须填写");
  94. }
  95. // 查询用户信息
  96. R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
  97. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
  98. {
  99. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  100. throw new ServiceException("登录用户:" + username + " 不存在");
  101. }
  102. if (R.FAIL == userResult.getCode())
  103. {
  104. throw new ServiceException(userResult.getMsg());
  105. }
  106. LoginUser userInfo = userResult.getData();
  107. SysUser user = userResult.getData().getSysUser();
  108. if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
  109. {
  110. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  111. throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
  112. }
  113. if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
  114. {
  115. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  116. throw new ServiceException("对不起,您的账号:" + username + " 已停用");
  117. }
  118. passwordService.validate(user, password);
  119. recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
  120. return userInfo;
  121. }
  122. public void logout(String loginName)
  123. {
  124. recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
  125. }
  126. /**
  127. * 注册
  128. */
  129. public void register(String username, String password)
  130. {
  131. // 用户名或密码为空 错误
  132. if (StringUtils.isAnyBlank(username, password))
  133. {
  134. throw new ServiceException("用户/密码必须填写");
  135. }
  136. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  137. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  138. {
  139. throw new ServiceException("账户长度必须在2到20个字符之间");
  140. }
  141. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  142. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  143. {
  144. throw new ServiceException("密码长度必须在5到20个字符之间");
  145. }
  146. // 注册用户信息
  147. SysUser sysUser = new SysUser();
  148. sysUser.setUserName(username);
  149. sysUser.setNickName(username);
  150. sysUser.setPassword(SecurityUtils.encryptPassword(password));
  151. R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
  152. if (R.FAIL == registerResult.getCode())
  153. {
  154. throw new ServiceException(registerResult.getMsg());
  155. }
  156. recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
  157. }
  158. /**
  159. * 养老食堂专用注册和登录
  160. * @param username
  161. * @param password
  162. * @param openId
  163. * @return
  164. */
  165. public LoginUser login(String username, String password, String openId)
  166. {
  167. // 用户名或密码为空 错误
  168. if (StringUtils.isAnyBlank(username, password))
  169. {
  170. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  171. throw new ServiceException("用户/密码必须填写");
  172. }
  173. // 用户名不在指定范围内 错误
  174. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  175. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  176. {
  177. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
  178. throw new ServiceException("用户名不在指定范围");
  179. }
  180. // 查询用户信息
  181. R<LoginUser> userResult = remoteUserService.getLrUserInfo(username, SecurityConstants.INNER);
  182. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
  183. {
  184. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  185. throw new ServiceException("登录用户:" + username + " 不存在");
  186. }
  187. if (R.FAIL == userResult.getCode())
  188. {
  189. throw new ServiceException(userResult.getMsg());
  190. }
  191. LoginUser userInfo = userResult.getData();
  192. SysUser user = userResult.getData().getSysUser();
  193. passwordService.validate(user, password);
  194. recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
  195. if (StringUtils.isNotEmpty(openId)) {
  196. if (StringUtils.isNotEmpty(user.getOpenid())) {
  197. throw new ServiceException("您的账号已经绑定其他微信,请解绑后登录");
  198. }
  199. SysUser in = new SysUser();
  200. in.setUserId(user.getUserId());
  201. in.setOpenid(openId);
  202. remoteUserService.updateOpenId(in, SecurityConstants.INNER);
  203. }
  204. return userInfo;
  205. }
  206. /**
  207. * 养老食堂专用openId登录
  208. * @param openId
  209. * @return
  210. */
  211. public LoginUser login(String openId) {
  212. SysUser query = new SysUser();
  213. query.setOpenid(openId);
  214. // 查询用户信息
  215. R<LoginUser> userResult = remoteUserService.getUserInfoByOpenId(query, SecurityConstants.INNER);
  216. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
  217. recordLogService.recordLogininfor(openId, Constants.LOGIN_FAIL, "登录用户不存在");
  218. throw new ServiceException("登录用户不存在");
  219. }
  220. if (R.FAIL == userResult.getCode()) {
  221. throw new ServiceException(userResult.getMsg());
  222. }
  223. LoginUser userInfo = userResult.getData();
  224. SysUser user = userResult.getData().getSysUser();
  225. recordLogService.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, "登录成功");
  226. return userInfo;
  227. }
  228. /**
  229. * 养老食堂专用解除openId绑定
  230. * @param
  231. * @return
  232. */
  233. public void wxUnbind() {
  234. // 查询用户信息
  235. R<LoginUser> userResult = remoteUserService.getLrUserInfo(SecurityUtils.getUsername(), SecurityConstants.INNER);
  236. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
  237. throw new ServiceException("登录用户不存在");
  238. }
  239. if (R.FAIL == userResult.getCode()) {
  240. throw new ServiceException(userResult.getMsg());
  241. }
  242. SysUser user = userResult.getData().getSysUser();
  243. SysUser in = new SysUser();
  244. in.setUserId(user.getUserId());
  245. in.setOpenid(null);
  246. remoteUserService.updateOpenId(in, SecurityConstants.INNER);
  247. }
  248. public LoginUser gzhregister(InLnstLrxx form, String openId)
  249. {
  250. // 用户名或密码为空 错误
  251. if (StringUtils.isAnyBlank(form.getLrZjhm())) {
  252. recordLogService.recordLogininfor(form.getLrZjhm(), Constants.LOGIN_FAIL, "用户名必须填写");
  253. throw new ServiceException("用户名必须填写");
  254. }
  255. // 用户名不在指定范围内 错误
  256. if (form.getLrZjhm().length() < UserConstants.USERNAME_MIN_LENGTH
  257. || form.getLrZjhm().length() > UserConstants.USERNAME_MAX_LENGTH) {
  258. recordLogService.recordLogininfor(form.getLrZjhm(), Constants.LOGIN_FAIL, "用户名不在指定范围");
  259. throw new ServiceException("用户名不在指定范围");
  260. }
  261. form.setOpenId(openId);
  262. AjaxResult res = remoteLnstService.add(form, SecurityConstants.INNER);
  263. String userName = "";
  264. if (ObjectUtils.isNotEmpty(res)) {
  265. if (res.containsKey("code") && res.containsKey("data")) {
  266. if (StringUtils.equals(res.get("code").toString(), "200")) {
  267. userName = res.get("data").toString();
  268. }
  269. }
  270. }
  271. if (StringUtils.isEmpty(userName)) {
  272. throw new ServiceException(res.get("msg").toString());
  273. }
  274. return remoteUserService.getLrUserInfo(userName, SecurityConstants.INNER).getData();
  275. }
  276. }