UserFacade.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package com.diagbot.facade;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.diagbot.client.AuthServiceClient;
  5. import com.diagbot.dto.GetConsoleUserInfoDTO;
  6. import com.diagbot.dto.LoginDTO;
  7. import com.diagbot.dto.RespDTO;
  8. import com.diagbot.dto.UserOrgDTO;
  9. import com.diagbot.entity.JWT;
  10. import com.diagbot.entity.Organization;
  11. import com.diagbot.entity.User;
  12. import com.diagbot.entity.UserAuthentication;
  13. import com.diagbot.entity.UserOrganization;
  14. import com.diagbot.entity.UserRole;
  15. import com.diagbot.entity.wrapper.MenuWrapper;
  16. import com.diagbot.exception.CommonErrorCode;
  17. import com.diagbot.exception.CommonException;
  18. import com.diagbot.exception.ServiceErrorCode;
  19. import com.diagbot.service.impl.UserServiceImpl;
  20. import com.diagbot.util.BeanUtil;
  21. import com.diagbot.util.EntityUtil;
  22. import com.diagbot.util.ListUtil;
  23. import com.diagbot.util.RegexValidateUtil;
  24. import com.diagbot.util.StringUtil;
  25. import com.diagbot.util.UserUtils;
  26. import com.diagbot.vo.ImgVerVerVO;
  27. import com.diagbot.vo.UserSaveVO;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.cache.annotation.CacheEvict;
  30. import org.springframework.cache.annotation.CachePut;
  31. import org.springframework.cache.annotation.Cacheable;
  32. import org.springframework.security.crypto.factory.PasswordEncoderFactories;
  33. import org.springframework.security.crypto.password.PasswordEncoder;
  34. import org.springframework.stereotype.Component;
  35. import java.io.Serializable;
  36. import java.util.Date;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. /**
  41. * @Description: 用户业务层
  42. * @author: gaodm
  43. * @time: 2018/8/6 9:00
  44. */
  45. @Component
  46. public class UserFacade extends UserServiceImpl {
  47. private static final String CACHE_NAME = "UserInfo";
  48. @Autowired
  49. AuthServiceClient authServiceClient;
  50. @Autowired
  51. VerFacade verFacade;
  52. @Autowired
  53. OrganizationFacade organizationFacade;
  54. @Autowired
  55. UserOrganizationFacade userOrganizationFacade;
  56. @Autowired
  57. UserRoleFacade userRoleFacade;
  58. @Autowired
  59. MenuFacade menuFacade;
  60. @Autowired
  61. UserAuthenticationFacade userAuthenticationFacade;
  62. @Autowired
  63. PermissionFacade permissionFacade;
  64. public User createUser(UserSaveVO userSaveVO){
  65. User user = new User();
  66. BeanUtil.copyProperties(userSaveVO, user);
  67. user.setGmtCreate(new Date());
  68. ImgVerVerVO imgVerVerVO = new ImgVerVerVO();
  69. BeanUtil.copyProperties(userSaveVO, imgVerVerVO);
  70. User bean = findByName(user.getUsername());
  71. if(bean != null) {
  72. throw new CommonException(ServiceErrorCode.USER_EXIST);
  73. }
  74. if(!RegexValidateUtil.checkMobileNumber(userSaveVO.getUsername())) {
  75. throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "请输入正确的手机号!");
  76. }
  77. //TODO 测试注释了图形验证和短信验证,后面要开启
  78. // verFacade.verifyImgVerification(imgVerVerVO); //保存时再次校验图形验证码,主要是为了防止跳过前端校验直接调用接口
  79. PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
  80. String entryPassword= passwordEncoder.encode(user.getPassword());
  81. user.setPassword(entryPassword);
  82. this.save(user);
  83. // 机构相关业务
  84. doOrganization(userSaveVO, user);
  85. //权限相关业务
  86. doPermisson(userSaveVO, user);
  87. //用户认证业务
  88. doUserAuthentication(userSaveVO, user);
  89. return user;
  90. }
  91. public RespDTO<IPage<User>> index(Page page, UserSaveVO userSaveVO){
  92. User user = new User();
  93. BeanUtil.copyProperties(userSaveVO, user);
  94. IPage<User> res = this.indexPage(page, user);
  95. return RespDTO.onSuc(res);
  96. }
  97. //添加一条默认用户认证信息
  98. public void doUserAuthentication(UserSaveVO userSaveVO, User user) {
  99. UserAuthentication ua = new UserAuthentication();
  100. ua.setGmtCreate(new Date());
  101. ua.setUserId(user.getId());
  102. userAuthenticationFacade.save(ua);
  103. }
  104. //如果机构已存在,只要添加用户与机构的映射即可;如果机构不存在,先添加机构信息
  105. public void doPermisson(UserSaveVO userSaveVO, User user) {
  106. UserRole userRole = new UserRole();
  107. userRole.setUserId(user.getId());
  108. userRole.setRoleId(1L);
  109. userRole.setGmtCreate(new Date());
  110. userRoleFacade.save(userRole);
  111. }
  112. //如果机构已存在,只要添加用户与机构的映射即可;如果机构不存在,先添加机构信息
  113. public void doOrganization(UserSaveVO userSaveVO, User user) {
  114. Map<String, Object> paramMap = new HashMap<>();
  115. String name = userSaveVO.getOrganization();
  116. paramMap.put("name", name);
  117. Long orgId = 0L;
  118. List<Organization> list = organizationFacade.selectByMap(paramMap);
  119. if(ListUtil.isEmpty(list)) {
  120. Organization org = new Organization();
  121. org.setName(name);
  122. org.setGmtCreate(new Date());
  123. organizationFacade.save(org);
  124. orgId = org.getId();
  125. } else {
  126. orgId = list.get(0).getId();
  127. //查询机构下是否已存在用户信息,如已存在,就抛异常,需求待核实
  128. Map paramMap1 = new HashMap();
  129. paramMap1.put("organization_id", orgId);
  130. paramMap1.put("is_deleted", "N");
  131. List<UserOrganization> res1 = userOrganizationFacade.selectByMap(paramMap1);
  132. if(ListUtil.isNotEmpty(res1)) {
  133. throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "该机构下已存在用户,不能再添加别的用户!");
  134. }
  135. }
  136. UserOrganization userOrganization = new UserOrganization();
  137. userOrganization.setUserId(user.getId());
  138. userOrganization.setOrganizationId(orgId);
  139. userOrganization.setGmtCreate(new Date());
  140. userOrganizationFacade.save(userOrganization);
  141. }
  142. //@Cacheable(value = "UserName", key = "'username:'+#p0")
  143. public User getUserInfo(String username){
  144. return this.findByName(username);
  145. }
  146. public RespDTO login(String username , String password){
  147. User user= this.findByName(username);
  148. if(null==user){
  149. throw new CommonException(ServiceErrorCode.USER_NOT_FOUND);
  150. }
  151. PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
  152. if(!passwordEncoder.matches(password,user.getPassword())){
  153. throw new CommonException(ServiceErrorCode.USER_PASSWORD_ERROR);
  154. }
  155. JWT jwt = authServiceClient.getToken("Basic dWFhLXNlcnZpY2U6MTIzNDU2", "password", username, password);
  156. // 获得用户菜单
  157. if(null==jwt){
  158. throw new CommonException(ServiceErrorCode.GET_TOKEN_FAIL);
  159. }
  160. LoginDTO loginDTO=new LoginDTO();
  161. //添加用户
  162. loginDTO.setUser(user);
  163. //添加token
  164. loginDTO.setToken(jwt.getAccess_token());
  165. //添加机构信息
  166. Organization org = organizationFacade.getById(user.getId());
  167. loginDTO.setOrganization(org);
  168. //添加菜单信息
  169. List<MenuWrapper> menuList = menuFacade.getByRole(user.getId());
  170. getMenuStruct(menuList);
  171. loginDTO.setMenuWrappers(menuList);
  172. return RespDTO.onSuc(loginDTO);
  173. }
  174. /**
  175. * @Description: 递归获取菜单结构
  176. * @Author: ztg
  177. * @Date: 2018/9/14 13:56
  178. */
  179. public void getMenuStruct(List<MenuWrapper> menuList) {
  180. for(MenuWrapper m : menuList) {
  181. m.setSubMenuList(recursion(m));
  182. }
  183. }
  184. public List<MenuWrapper> recursion(MenuWrapper m) {
  185. List<MenuWrapper> subMenu = menuFacade.getSubMenuById(m.getId());
  186. for (MenuWrapper sub : subMenu) {
  187. sub.setSubMenuList(recursion(sub));
  188. }
  189. return subMenu;
  190. }
  191. //获取用户相关信息和机构信息
  192. public Map<Long, UserOrgDTO> getUserAndOrg(List<Long> userIds) {
  193. Map<Long, UserOrgDTO> data = new HashMap();
  194. if(ListUtil.isEmpty(userIds)) {
  195. return data;
  196. }
  197. List<UserOrgDTO> list = this.getUserOrgByIds(userIds);
  198. // for(UserOrgDTO bean : list) {
  199. // data.put(bean.getUserId(), bean);
  200. // }
  201. data = EntityUtil.makeEntityMap(list,"userId");
  202. return data;
  203. }
  204. public RespDTO resetPassword(String username, String password, String diffPassword) {
  205. if (StringUtil.isBlank(username)){
  206. throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
  207. "请输入手机号!");
  208. }
  209. if (StringUtil.isBlank(password)){
  210. throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
  211. "请输入密码!");
  212. }
  213. User user= this.findByName(username);
  214. if(null==user){
  215. throw new CommonException(ServiceErrorCode.USER_NOT_FOUND);
  216. }
  217. PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
  218. if(!StringUtil.isBlank(diffPassword) && "Y".equals(diffPassword)) {
  219. if(passwordEncoder.matches(password,user.getPassword())){
  220. throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "修改后密码不能与修改前相同!");
  221. }
  222. }
  223. String entryPassword= passwordEncoder.encode(password);
  224. user.setPassword(entryPassword);
  225. user.setGmtModified(new Date());
  226. this.updateById(user);
  227. return RespDTO.onSuc(true);
  228. }
  229. //缓存演示
  230. @CachePut(value = CACHE_NAME, key = "'user:'+#p0['id']")
  231. public User saveUser(User user){
  232. PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
  233. String entryPassword= passwordEncoder.encode(user.getPassword());
  234. user.setPassword(entryPassword);
  235. user.setGmtCreate(new Date());
  236. user.setCreator(UserUtils.getCurrentPrincipleID());
  237. super.save(user);
  238. return user;
  239. }
  240. @CachePut(value = CACHE_NAME, key = "'user:'+#p0['id']")
  241. public User updateUser(User user){
  242. PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
  243. String entryPassword= passwordEncoder.encode(user.getPassword());
  244. user.setPassword(entryPassword);
  245. user.setGmtModified(new Date());
  246. user.setModifier(UserUtils.getCurrentPrincipleID());
  247. super.updateById(user);
  248. return user;
  249. }
  250. @Override
  251. @Cacheable(value = CACHE_NAME, key = "'user:'+#p0")
  252. public User getById(Serializable id){
  253. return super.getById(id);
  254. }
  255. @Override
  256. @CacheEvict(value = CACHE_NAME, key = "'user:'+#p0")
  257. public boolean removeById(Serializable id){
  258. return super.removeById(id);
  259. }
  260. public GetConsoleUserInfoDTO getConsoleUserInfo() {
  261. Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
  262. User user = this.getById(userId);
  263. Organization organization = organizationFacade.getByUserId(userId);
  264. UserAuthentication userAuthentication = userAuthenticationFacade.getByUserId(userId);
  265. GetConsoleUserInfoDTO getConsoleUserInfoDTO = new GetConsoleUserInfoDTO();
  266. getConsoleUserInfoDTO.setUser(user);
  267. getConsoleUserInfoDTO.setOrganization(organization);
  268. getConsoleUserInfoDTO.setUserAuthentication(userAuthentication);
  269. return getConsoleUserInfoDTO;
  270. }
  271. }