123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package com.diagbot.facade;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.diagbot.client.AuthServiceClient;
- import com.diagbot.dto.GetConsoleUserInfoDTO;
- import com.diagbot.dto.LoginDTO;
- import com.diagbot.dto.RespDTO;
- import com.diagbot.dto.UserOrgDTO;
- import com.diagbot.entity.JWT;
- import com.diagbot.entity.Organization;
- import com.diagbot.entity.User;
- import com.diagbot.entity.UserAuthentication;
- import com.diagbot.entity.UserOrganization;
- import com.diagbot.entity.UserRole;
- import com.diagbot.entity.wrapper.MenuWrapper;
- import com.diagbot.exception.CommonErrorCode;
- import com.diagbot.exception.CommonException;
- import com.diagbot.exception.ServiceErrorCode;
- import com.diagbot.service.impl.UserServiceImpl;
- import com.diagbot.util.BeanUtil;
- import com.diagbot.util.EntityUtil;
- import com.diagbot.util.ListUtil;
- import com.diagbot.util.RegexValidateUtil;
- import com.diagbot.util.StringUtil;
- import com.diagbot.util.UserUtils;
- import com.diagbot.vo.ImgVerVerVO;
- import com.diagbot.vo.UserSaveVO;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cache.annotation.CacheEvict;
- import org.springframework.cache.annotation.CachePut;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.security.crypto.factory.PasswordEncoderFactories;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.stereotype.Component;
- import java.io.Serializable;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * @Description: 用户业务层
- * @author: gaodm
- * @time: 2018/8/6 9:00
- */
- @Component
- public class UserFacade extends UserServiceImpl {
- private static final String CACHE_NAME = "UserInfo";
- @Autowired
- AuthServiceClient authServiceClient;
- @Autowired
- VerFacade verFacade;
- @Autowired
- OrganizationFacade organizationFacade;
- @Autowired
- UserOrganizationFacade userOrganizationFacade;
- @Autowired
- UserRoleFacade userRoleFacade;
- @Autowired
- MenuFacade menuFacade;
- @Autowired
- UserAuthenticationFacade userAuthenticationFacade;
- @Autowired
- PermissionFacade permissionFacade;
- public User createUser(UserSaveVO userSaveVO){
- User user = new User();
- BeanUtil.copyProperties(userSaveVO, user);
- user.setGmtCreate(new Date());
- ImgVerVerVO imgVerVerVO = new ImgVerVerVO();
- BeanUtil.copyProperties(userSaveVO, imgVerVerVO);
- User bean = findByName(user.getUsername());
- if(bean != null) {
- throw new CommonException(ServiceErrorCode.USER_EXIST);
- }
- if(!RegexValidateUtil.checkMobileNumber(userSaveVO.getUsername())) {
- throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "请输入正确的手机号!");
- }
- //TODO 测试注释了图形验证和短信验证,后面要开启
- // verFacade.verifyImgVerification(imgVerVerVO); //保存时再次校验图形验证码,主要是为了防止跳过前端校验直接调用接口
- PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
- String entryPassword= passwordEncoder.encode(user.getPassword());
- user.setPassword(entryPassword);
- this.save(user);
- // 机构相关业务
- doOrganization(userSaveVO, user);
- //权限相关业务
- doPermisson(userSaveVO, user);
- //用户认证业务
- doUserAuthentication(userSaveVO, user);
- return user;
- }
- public RespDTO<IPage<User>> index(Page page, UserSaveVO userSaveVO){
- User user = new User();
- BeanUtil.copyProperties(userSaveVO, user);
- IPage<User> res = this.indexPage(page, user);
- return RespDTO.onSuc(res);
- }
- //添加一条默认用户认证信息
- public void doUserAuthentication(UserSaveVO userSaveVO, User user) {
- UserAuthentication ua = new UserAuthentication();
- ua.setGmtCreate(new Date());
- ua.setUserId(user.getId());
- userAuthenticationFacade.save(ua);
- }
- //如果机构已存在,只要添加用户与机构的映射即可;如果机构不存在,先添加机构信息
- public void doPermisson(UserSaveVO userSaveVO, User user) {
- UserRole userRole = new UserRole();
- userRole.setUserId(user.getId());
- userRole.setRoleId(1L);
- userRole.setGmtCreate(new Date());
- userRoleFacade.save(userRole);
- }
- //如果机构已存在,只要添加用户与机构的映射即可;如果机构不存在,先添加机构信息
- public void doOrganization(UserSaveVO userSaveVO, User user) {
- Map<String, Object> paramMap = new HashMap<>();
- String name = userSaveVO.getOrganization();
- paramMap.put("name", name);
- Long orgId = 0L;
- List<Organization> list = organizationFacade.selectByMap(paramMap);
- if(ListUtil.isEmpty(list)) {
- Organization org = new Organization();
- org.setName(name);
- org.setGmtCreate(new Date());
- organizationFacade.save(org);
- orgId = org.getId();
- } else {
- orgId = list.get(0).getId();
- //查询机构下是否已存在用户信息,如已存在,就抛异常,需求待核实
- Map paramMap1 = new HashMap();
- paramMap1.put("organization_id", orgId);
- paramMap1.put("is_deleted", "N");
- List<UserOrganization> res1 = userOrganizationFacade.selectByMap(paramMap1);
- if(ListUtil.isNotEmpty(res1)) {
- throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "该机构下已存在用户,不能再添加别的用户!");
- }
- }
- UserOrganization userOrganization = new UserOrganization();
- userOrganization.setUserId(user.getId());
- userOrganization.setOrganizationId(orgId);
- userOrganization.setGmtCreate(new Date());
- userOrganizationFacade.save(userOrganization);
- }
- //@Cacheable(value = "UserName", key = "'username:'+#p0")
- public User getUserInfo(String username){
- return this.findByName(username);
- }
- public RespDTO login(String username , String password){
- User user= this.findByName(username);
- if(null==user){
- throw new CommonException(ServiceErrorCode.USER_NOT_FOUND);
- }
- PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
- if(!passwordEncoder.matches(password,user.getPassword())){
- throw new CommonException(ServiceErrorCode.USER_PASSWORD_ERROR);
- }
- JWT jwt = authServiceClient.getToken("Basic dWFhLXNlcnZpY2U6MTIzNDU2", "password", username, password);
- // 获得用户菜单
- if(null==jwt){
- throw new CommonException(ServiceErrorCode.GET_TOKEN_FAIL);
- }
- LoginDTO loginDTO=new LoginDTO();
- //添加用户
- loginDTO.setUser(user);
- //添加token
- loginDTO.setToken(jwt.getAccess_token());
- //添加机构信息
- Organization org = organizationFacade.getById(user.getId());
- loginDTO.setOrganization(org);
- //添加菜单信息
- List<MenuWrapper> menuList = menuFacade.getByRole(user.getId());
- getMenuStruct(menuList);
- loginDTO.setMenuWrappers(menuList);
- return RespDTO.onSuc(loginDTO);
- }
- /**
- * @Description: 递归获取菜单结构
- * @Author: ztg
- * @Date: 2018/9/14 13:56
- */
- public void getMenuStruct(List<MenuWrapper> menuList) {
- for(MenuWrapper m : menuList) {
- m.setSubMenuList(recursion(m));
- }
- }
- public List<MenuWrapper> recursion(MenuWrapper m) {
- List<MenuWrapper> subMenu = menuFacade.getSubMenuById(m.getId());
- for (MenuWrapper sub : subMenu) {
- sub.setSubMenuList(recursion(sub));
- }
- return subMenu;
- }
- //获取用户相关信息和机构信息
- public Map<Long, UserOrgDTO> getUserAndOrg(List<Long> userIds) {
- Map<Long, UserOrgDTO> data = new HashMap();
- if(ListUtil.isEmpty(userIds)) {
- return data;
- }
- List<UserOrgDTO> list = this.getUserOrgByIds(userIds);
- // for(UserOrgDTO bean : list) {
- // data.put(bean.getUserId(), bean);
- // }
- data = EntityUtil.makeEntityMap(list,"userId");
- return data;
- }
- public RespDTO resetPassword(String username, String password, String diffPassword) {
- if (StringUtil.isBlank(username)){
- throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
- "请输入手机号!");
- }
- if (StringUtil.isBlank(password)){
- throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
- "请输入密码!");
- }
- User user= this.findByName(username);
- if(null==user){
- throw new CommonException(ServiceErrorCode.USER_NOT_FOUND);
- }
- PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
- if(!StringUtil.isBlank(diffPassword) && "Y".equals(diffPassword)) {
- if(passwordEncoder.matches(password,user.getPassword())){
- throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "修改后密码不能与修改前相同!");
- }
- }
- String entryPassword= passwordEncoder.encode(password);
- user.setPassword(entryPassword);
- user.setGmtModified(new Date());
- this.updateById(user);
- return RespDTO.onSuc(true);
- }
- //缓存演示
- @CachePut(value = CACHE_NAME, key = "'user:'+#p0['id']")
- public User saveUser(User user){
- PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
- String entryPassword= passwordEncoder.encode(user.getPassword());
- user.setPassword(entryPassword);
- user.setGmtCreate(new Date());
- user.setCreator(UserUtils.getCurrentPrincipleID());
- super.save(user);
- return user;
- }
- @CachePut(value = CACHE_NAME, key = "'user:'+#p0['id']")
- public User updateUser(User user){
- PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
- String entryPassword= passwordEncoder.encode(user.getPassword());
- user.setPassword(entryPassword);
- user.setGmtModified(new Date());
- user.setModifier(UserUtils.getCurrentPrincipleID());
- super.updateById(user);
- return user;
- }
- @Override
- @Cacheable(value = CACHE_NAME, key = "'user:'+#p0")
- public User getById(Serializable id){
- return super.getById(id);
- }
- @Override
- @CacheEvict(value = CACHE_NAME, key = "'user:'+#p0")
- public boolean removeById(Serializable id){
- return super.removeById(id);
- }
- public GetConsoleUserInfoDTO getConsoleUserInfo() {
- Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
- User user = this.getById(userId);
- Organization organization = organizationFacade.getByUserId(userId);
- UserAuthentication userAuthentication = userAuthenticationFacade.getByUserId(userId);
-
- GetConsoleUserInfoDTO getConsoleUserInfoDTO = new GetConsoleUserInfoDTO();
- getConsoleUserInfoDTO.setUser(user);
- getConsoleUserInfoDTO.setOrganization(organization);
- getConsoleUserInfoDTO.setUserAuthentication(userAuthentication);
-
- return getConsoleUserInfoDTO;
- }
- }
|