| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.moral.api.entity.ManageAccount; |
| | | import com.moral.api.entity.ManageAccountRole; |
| | | import com.moral.api.entity.ManageMenu; |
| | | import com.moral.api.entity.ManageRole; |
| | | import com.moral.api.mapper.ManageAccountMapper; |
| | | import com.moral.api.mapper.ManageAccountRoleMapper; |
| | | import com.moral.api.mapper.ManageMenuMapper; |
| | | import com.moral.api.mapper.ManageRoleMapper; |
| | | import com.moral.api.pojo.dto.account.*; |
| | | import com.moral.api.pojo.dto.login.AccountInfoDTO; |
| | | import com.moral.api.pojo.dto.login.LoginDTO; |
| | | import com.moral.api.pojo.form.*; |
| | | import com.moral.api.service.ManageAccountService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import com.moral.util.AESUtils; |
| | | import com.moral.util.MD5Utils; |
| | | import com.moral.util.TokenUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class ManageAccountServiceImpl extends ServiceImpl<ManageAccountMapper, ManageAccount> implements ManageAccountService { |
| | | |
| | | @Value("${AES.KEY}") |
| | | private String AESKey; |
| | | @Resource |
| | | ManageAccountMapper accountMapper; |
| | | @Resource |
| | | ManageRoleMapper roleMapper; |
| | | @Resource |
| | | |
| | | @Autowired |
| | | ManageAccountMapper manageAccountMapper; |
| | | @Autowired |
| | | ManageRoleMapper manageRoleMapper; |
| | | @Autowired |
| | | ManageMenuMapper manageMenuMapper; |
| | | @Autowired |
| | | ManageAccountRoleMapper manageAccountRoleMapper; |
| | | |
| | | |
| | | public Map<String, Object> login(Map<String, Object> paramters) { |
| | | Map<String,Object> result = new HashMap<>(); |
| | | //接收参数 |
| | | String cyrpAccount = (String) paramters.get("account"); |
| | | String cyrpPassword = (String) paramters.get("password"); |
| | | /** |
| | | * @Description: 登陆 |
| | | * @Param: [parameters] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | @Override |
| | | public LoginDTO login(LoginForm loginForm) { |
| | | LoginDTO loginDTO = new LoginDTO(); |
| | | //取参 |
| | | String account = loginForm.getAccount(); |
| | | String AESPassword = loginForm.getPassword(); |
| | | //解密 |
| | | String account = AESUtils.decrypt(cyrpAccount, AESKey); |
| | | String password = AESUtils.decrypt(cyrpPassword, AESKey); |
| | | String password = AESUtils.decrypt(AESPassword); |
| | | //查询是否存在 |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("account", account); |
| | | ManageAccount manageAccount = accountMapper.selectOne(wrapper); |
| | | if(ObjectUtils.isEmpty(manageAccount)){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","用户不存在"); |
| | | return result; |
| | | List<ManageAccount> manageAccounts = manageAccountMapper.selectList(wrapper); |
| | | if (ObjectUtils.isEmpty(manageAccounts)) { |
| | | loginDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode()); |
| | | loginDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); |
| | | return loginDTO; |
| | | } |
| | | //查询是否逻辑删除 |
| | | if(manageAccount.getIsDelete().equals("1")){ |
| | | result.put("accountId",-2); |
| | | result.put("msg","用户已被封禁"); |
| | | return result; |
| | | ManageAccount manageAccount = null; |
| | | for (ManageAccount value : manageAccounts) { |
| | | if (Constants.NOT_DELETE.equals(value.getIsDelete())) |
| | | manageAccount = value; |
| | | } |
| | | |
| | | if (ObjectUtils.isEmpty(manageAccount)) { |
| | | loginDTO.setCode(ResponseCodeEnum.ACCOUNT_IS_DELETE.getCode()); |
| | | loginDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); |
| | | return loginDTO; |
| | | } |
| | | //校验密码 |
| | | if(!MD5Utils.saltMD5Verify(password,manageAccount.getPassword())){ |
| | | result.put("accountId",-3); |
| | | result.put("msg","用户名密码错误"); |
| | | return result; |
| | | if (!MD5Utils.saltMD5Verify(password, manageAccount.getPassword())) { |
| | | loginDTO.setCode(ResponseCodeEnum.PASSWORD_ERROR.getCode()); |
| | | loginDTO.setMsg(ResponseCodeEnum.PASSWORD_ERROR.getMsg()); |
| | | return loginDTO; |
| | | } |
| | | |
| | | //查询角色 |
| | | List<ManageRole> roles = roleMapper.getManageRoleByAccountId(manageAccount.getId()); |
| | | if(ObjectUtils.isEmpty(roles)){ |
| | | result.put("accountId",-4); |
| | | result.put("msg","用户尚未分配角色"); |
| | | return result; |
| | | } |
| | | List<ManageRole> roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId()); |
| | | |
| | | //查询菜单 |
| | | List<ManageMenu> menus = manageMenuMapper.getParentChildrenMenusByRoles(roles); |
| | | if(ObjectUtils.isEmpty(menus)){ |
| | | result.put("accountId",-5); |
| | | result.put("msg","用户尚未分配菜单"); |
| | | return result; |
| | | List<ManageMenu> menus = null; |
| | | if (!ObjectUtils.isEmpty(roles)) { |
| | | menus = manageMenuMapper.getParentChildrenMenusByRoles(roles); |
| | | } |
| | | |
| | | //获取用户token,并且将基本信息存入缓存 |
| | | Map<String,Object> userInfo = new HashMap<>();//需要保存在缓存中用户的数据 |
| | | userInfo.put("accountId",manageAccount.getId());//用户Id |
| | | userInfo.put("userName",manageAccount.getUserName());//用户名称 |
| | | userInfo.put("roles",roles);//用户角色 |
| | | userInfo.put("menus",menus);//用户菜单 |
| | | Map<String, Object> tokenResult = TokenUtils.getToken(String.valueOf(manageAccount.getId()), userInfo); |
| | | if(tokenResult.get("code").equals(TokenUtils.error)){ |
| | | result.put("accountId",-6); |
| | | result.put("msg","生成token错误"); |
| | | return result; |
| | | } |
| | | //封装用户信息 |
| | | AccountInfoDTO accountInfoDTO = new AccountInfoDTO(); |
| | | accountInfoDTO.setAccount(manageAccount); |
| | | accountInfoDTO.setMenus(menus); |
| | | accountInfoDTO.setRoles(roles); |
| | | |
| | | //打包返回信息 |
| | | result.put("accountId",manageAccount.getId());//用户Id |
| | | result.put("userName",manageAccount.getUserName());//用户名称 |
| | | result.put("roles",roles);//用户角色 |
| | | result.put("menus",menus);//用户菜单 |
| | | result.put("token",tokenResult.get("token")); |
| | | return result; |
| | | //获取token 并且存入缓存 |
| | | String token = TokenUtils.getToken(String.valueOf(manageAccount.getId()), accountInfoDTO); |
| | | |
| | | //封装返回结果 |
| | | loginDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | loginDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | loginDTO.setAccountInfoDTO(accountInfoDTO); |
| | | loginDTO.setToken(token); |
| | | |
| | | return loginDTO; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 注销 |
| | | * @Param: [parameters] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | @Override |
| | | public boolean logout(LogoutForm logoutForm) { |
| | | String accountId = logoutForm.getAccountId(); |
| | | String token = logoutForm.getToken(); |
| | | TokenUtils.destoryToken(accountId, token); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 添加后台账号 |
| | | * @Param: [accountAddRequest] |
| | | * @return: com.moral.api.pojo.dto.AccountDTO |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/13 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public AccountInsertDTO insertAccount(AccountInsertForm accountInsertForm) { |
| | | AccountInsertDTO accountInsertDTO = new AccountInsertDTO(); |
| | | //取参 |
| | | String account = accountInsertForm.getAccount(); |
| | | String AESPassword = accountInsertForm.getPassword(); |
| | | String password = MD5Utils.saltMD5(AESUtils.decrypt(AESPassword)); |
| | | String userName = accountInsertForm.getUserName(); |
| | | String email = accountInsertForm.getEmail(); |
| | | String mobile = accountInsertForm.getMobile(); |
| | | String wechat = accountInsertForm.getWechat(); |
| | | List<String> roleIdsStr = accountInsertForm.getRoleIds(); |
| | | |
| | | /*判断账号是否存在*/ |
| | | ManageAccount exitAccount = new ManageAccount(); |
| | | exitAccount.setAccount(account); |
| | | exitAccount.setIsDelete(Constants.NOT_DELETE); |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); |
| | | wrapper.setEntity(exitAccount); |
| | | List<ManageAccount> exitAccounts = manageAccountMapper.selectList(wrapper); |
| | | if (!ObjectUtils.isEmpty(exitAccounts)) { |
| | | accountInsertDTO.setCode(ResponseCodeEnum.ACCOUNT_EXIST.getCode()); |
| | | accountInsertDTO.setMsg(ResponseCodeEnum.ACCOUNT_EXIST.getMsg()); |
| | | return accountInsertDTO; |
| | | } |
| | | |
| | | //封装account |
| | | ManageAccount manageAccount = new ManageAccount(); |
| | | manageAccount.setAccount(account); |
| | | manageAccount.setPassword(password); |
| | | manageAccount.setUserName(userName); |
| | | manageAccount.setEmail(email); |
| | | manageAccount.setMobile(mobile); |
| | | manageAccount.setWechat(wechat); |
| | | manageAccountMapper.insert(manageAccount); |
| | | //封装account_role |
| | | /*String to Integer*/ |
| | | List<ManageRole> roles = null; |
| | | //如果新建账号没有分配角色则不进行操作 |
| | | if (!ObjectUtils.isEmpty(roleIdsStr)) { |
| | | List<Integer> roleIds = new ArrayList<>(); |
| | | roleIdsStr.forEach(str -> roleIds.add(Integer.parseInt(str))); |
| | | |
| | | Integer accountId = manageAccount.getId(); |
| | | roleIdsStr.forEach( |
| | | value -> { |
| | | ManageAccountRole manageAccountRole = new ManageAccountRole(); |
| | | manageAccountRole.setAccountId(accountId); |
| | | manageAccountRole.setRoleId(Integer.parseInt(value)); |
| | | manageAccountRoleMapper.insert(manageAccountRole); |
| | | } |
| | | ); |
| | | roles = manageRoleMapper.selectBatchIds(roleIds); |
| | | } |
| | | //封装返回结果 |
| | | accountInsertDTO.setAccount(manageAccount); |
| | | accountInsertDTO.setRoles(roles); |
| | | accountInsertDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | accountInsertDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | return accountInsertDTO; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 查询后台账号 |
| | | * @Param: [accountQueryRequest] |
| | | * @return: com.moral.api.pojo.dto.AccountDTO |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/15 |
| | | */ |
| | | @Override |
| | | public AccountQueryDTO queryAccount(AccountQueryForm accountQueryForm) { |
| | | AccountQueryDTO accountQueryDTO = new AccountQueryDTO(); |
| | | //取参 |
| | | Integer pageCount = accountQueryForm.getPage(); |
| | | Integer size = accountQueryForm.getSize(); |
| | | Integer accountId = accountQueryForm.getAccountId(); |
| | | String account = accountQueryForm.getAccount(); |
| | | String userName = accountQueryForm.getUserName(); |
| | | String email = accountQueryForm.getEmail(); |
| | | String mobile = accountQueryForm.getMobile(); |
| | | String wechat = accountQueryForm.getWechat(); |
| | | String isDelete = accountQueryForm.getIsDelete(); |
| | | |
| | | //查询用户 |
| | | Page<ManageAccount> page = new Page<>(pageCount, size); |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper(); |
| | | if(!ObjectUtils.isEmpty(accountId)){ |
| | | wrapper.eq("id",accountId); |
| | | } |
| | | |
| | | if(!ObjectUtils.isEmpty(userName)){ |
| | | wrapper.like("user_name",userName); |
| | | } |
| | | |
| | | if(!ObjectUtils.isEmpty(account)){ |
| | | wrapper.like("account",account); |
| | | } |
| | | |
| | | if(!ObjectUtils.isEmpty(email)){ |
| | | wrapper.like("email",email); |
| | | } |
| | | |
| | | if(!ObjectUtils.isEmpty(mobile)){ |
| | | wrapper.like("mobile",mobile); |
| | | } |
| | | |
| | | if(!ObjectUtils.isEmpty(wechat)){ |
| | | wrapper.like("wechat",wechat); |
| | | } |
| | | |
| | | if (!ObjectUtils.isEmpty(isDelete)) |
| | | wrapper.eq("is_delete", isDelete); |
| | | else |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | |
| | | Page resultPage = manageAccountMapper.selectPage(page, wrapper); |
| | | //查询用户对应的角色 |
| | | List<ManageAccount> accounts = resultPage.getRecords(); |
| | | List<AccountDTO> accountDTOS = new ArrayList<>(); |
| | | if (!ObjectUtils.isEmpty(accounts)) { |
| | | for (ManageAccount manageAccount : accounts) { |
| | | AccountDTO accountDTO = new AccountDTO(); |
| | | List<ManageRole> roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId()); |
| | | accountDTO.setRoles(roles); |
| | | accountDTO.setAccount(manageAccount); |
| | | accountDTOS.add(accountDTO); |
| | | } |
| | | } |
| | | //封装返回结果 |
| | | accountQueryDTO.setAccountDTOS(accountDTOS); |
| | | accountQueryDTO.setCurrent(resultPage.getCurrent()); |
| | | accountQueryDTO.setPages(resultPage.getPages()); |
| | | accountQueryDTO.setSize(resultPage.getSize()); |
| | | accountQueryDTO.setTotal(resultPage.getTotal()); |
| | | accountQueryDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | accountQueryDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | |
| | | return accountQueryDTO; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 更新后台账号 |
| | | * @Param: [accountUpdateRequest] |
| | | * @return: com.moral.api.pojo.dto.account.AccountUpdateDTO |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/16 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public AccountUpdateDTO updateAccount(AccountUpdateForm accountUpdateForm) { |
| | | AccountUpdateDTO accountUpdateDTO = new AccountUpdateDTO(); |
| | | //取参 |
| | | Integer accountId = accountUpdateForm.getAccountId(); |
| | | String email = accountUpdateForm.getEmail(); |
| | | String mobile = accountUpdateForm.getMobile(); |
| | | String wechat = accountUpdateForm.getWechat(); |
| | | String userName = accountUpdateForm.getUserName(); |
| | | List<Integer> roleIds = accountUpdateForm.getRoleIds(); |
| | | //校验参数是否符合逻辑 |
| | | /*判断要更新的用户是否存在*/ |
| | | QueryWrapper<ManageAccount> exitWrapper = new QueryWrapper<>(); |
| | | ManageAccount exitManageAccount = new ManageAccount(); |
| | | exitManageAccount.setId(accountId); |
| | | exitManageAccount.setIsDelete(Constants.NOT_DELETE); |
| | | exitWrapper.setEntity(exitManageAccount); |
| | | List<ManageAccount> manageAccounts = manageAccountMapper.selectList(exitWrapper); |
| | | if (ObjectUtils.isEmpty(manageAccounts)) { |
| | | accountUpdateDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode()); |
| | | accountUpdateDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); |
| | | return accountUpdateDTO; |
| | | } |
| | | //更新ManageAccount表 |
| | | ManageAccount manageAccount = new ManageAccount(); |
| | | manageAccount.setEmail(email); |
| | | manageAccount.setMobile(mobile); |
| | | manageAccount.setWechat(wechat); |
| | | manageAccount.setUserName(userName); |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("id", accountId); |
| | | manageAccountMapper.update(manageAccount, wrapper); |
| | | //更新ManageAccountRole表 |
| | | /*删除该用户的所有角色*/ |
| | | QueryWrapper<ManageAccountRole> deleteWrapper = new QueryWrapper<>(); |
| | | deleteWrapper.eq("account_id", accountId); |
| | | manageAccountRoleMapper.delete(deleteWrapper); |
| | | /*重新添加角色*/ |
| | | for (Integer roleId : roleIds) { |
| | | ManageAccountRole manageAccountRole = new ManageAccountRole(); |
| | | manageAccountRole.setAccountId(accountId); |
| | | manageAccountRole.setRoleId(roleId); |
| | | manageAccountRoleMapper.insert(manageAccountRole); |
| | | } |
| | | //获取用户所有角色 |
| | | List<ManageRole> manageRoles = manageRoleMapper.selectBatchIds(roleIds); |
| | | //封装返回结果 |
| | | accountUpdateDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | accountUpdateDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | accountUpdateDTO.setRoles(manageRoles); |
| | | accountUpdateDTO.setAccount(manageAccountMapper.selectById(accountId)); |
| | | return accountUpdateDTO; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 删除后台账号 |
| | | * @Param: [accountDeleteRequest] |
| | | * @return: com.moral.api.pojo.dto.account.AccountDeleteDTO |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/16 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public AccountDeleteDTO deleteAccount(AccountDeleteForm accountDeleteForm) { |
| | | AccountDeleteDTO accountDeleteDTO = new AccountDeleteDTO(); |
| | | //取参 |
| | | Integer accountId = accountDeleteForm.getAccountId(); |
| | | //查询是否存在 |
| | | ManageAccount manageAccount = new ManageAccount(); |
| | | manageAccount.setIsDelete(Constants.NOT_DELETE); |
| | | manageAccount.setId(accountId); |
| | | QueryWrapper<ManageAccount> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.setEntity(manageAccount); |
| | | ManageAccount existManageAccount = manageAccount.selectOne(queryWrapper); |
| | | if (ObjectUtils.isEmpty(existManageAccount)) { |
| | | accountDeleteDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode()); |
| | | accountDeleteDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); |
| | | return accountDeleteDTO; |
| | | } |
| | | //逻辑删除账号 |
| | | existManageAccount.setIsDelete(Constants.DELETE); |
| | | UpdateWrapper<ManageAccount> deleteAccountWrapper = new UpdateWrapper<>(); |
| | | deleteAccountWrapper.set("is_delete", Constants.DELETE).eq("id", manageAccount.getId()); |
| | | manageAccountMapper.update(null, deleteAccountWrapper); |
| | | //逻辑删除账号相关角色 |
| | | UpdateWrapper<ManageAccountRole> deleteManageAccountRoleWrapper = new UpdateWrapper<>(); |
| | | deleteManageAccountRoleWrapper.set("is_delete", Constants.DELETE).eq("account_id", manageAccount.getId()); |
| | | manageAccountRoleMapper.update(null, deleteManageAccountRoleWrapper); |
| | | //封装返回结果 |
| | | accountDeleteDTO.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | accountDeleteDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | accountDeleteDTO.setAccount(existManageAccount); |
| | | return accountDeleteDTO; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |