| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | 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.AccountDTO; |
| | | import com.moral.api.pojo.dto.AccountInfoDTO; |
| | | import com.moral.api.pojo.dto.LoginDTO; |
| | | import com.moral.api.pojo.request.AccountAddRequest; |
| | | import com.moral.api.pojo.request.LoginRequest; |
| | | import com.moral.api.pojo.request.LogoutRequest; |
| | | import com.moral.api.service.ManageAccountService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.util.AESUtils; |
| | |
| | | import com.moral.util.TokenUtils; |
| | | 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> |
| | |
| | | @Value("${AES.KEY}") |
| | | private String AESKey; |
| | | @Resource |
| | | ManageAccountMapper accountMapper; |
| | | ManageAccountMapper manageAccountMapper; |
| | | @Resource |
| | | ManageRoleMapper roleMapper; |
| | | ManageRoleMapper manageRoleMapper; |
| | | @Resource |
| | | ManageMenuMapper manageMenuMapper; |
| | | @Resource |
| | | ManageAccountRoleMapper manageAccountRoleMapper; |
| | | |
| | | public final static String specialCharRegEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t"; |
| | | |
| | | public final static String mobileRegEx = "^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$"; |
| | | |
| | | public final static String emailRegEx = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; |
| | | /** |
| | | * @Description: 登陆 |
| | | * @Param: [parameters] |
| | |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | @Override |
| | | public LoginDTO login(String AESAccount,String AESPassword) { |
| | | public LoginDTO login(LoginRequest loginRequest) { |
| | | LoginDTO loginDTO = new LoginDTO(); |
| | | //取参 |
| | | String AESAccount = loginRequest.getAccount(); |
| | | String AESPassword = loginRequest.getPassword(); |
| | | //解密 |
| | | String account = AESUtils.decrypt(AESAccount, AESKey); |
| | | String password = AESUtils.decrypt(AESPassword, AESKey); |
| | | //查询是否存在 |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("account", account); |
| | | ManageAccount manageAccount = accountMapper.selectOne(wrapper); |
| | | ManageAccount manageAccount = manageAccountMapper.selectOne(wrapper); |
| | | if (ObjectUtils.isEmpty(manageAccount)) { |
| | | loginDTO.setCode(LoginDTO.NOT_EXIST); |
| | | return loginDTO; |
| | |
| | | } |
| | | |
| | | //查询角色 |
| | | List<ManageRole> roles = roleMapper.getManageRoleByAccountId(manageAccount.getId()); |
| | | List<ManageRole> roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId()); |
| | | if (ObjectUtils.isEmpty(roles)) {//判断账号是否存在角色 |
| | | loginDTO.setCode(LoginDTO.ROLE_EMPTY); |
| | | return loginDTO; |
| | |
| | | return loginDTO; |
| | | } |
| | | |
| | | |
| | | //封装用户信息 |
| | | AccountInfoDTO accountInfoDTO = new AccountInfoDTO(); |
| | | /*获取角色名称集合*/ |
| | | ArrayList<String> roleNames = new ArrayList<>(); |
| | | roles.forEach(role->roleNames.add(role.getName())); |
| | | /*过滤menu无用属性*/ |
| | | for (ManageMenu menu : menus) { |
| | | menu.setCreateTime(null); |
| | | menu.setIsDelete(null); |
| | | menu.setUpdateTime(null); |
| | | menu.setParentId(null); |
| | | List<ManageMenu> children = menu.getChildren(); |
| | | for (ManageMenu child : children) { |
| | | child.setCreateTime(null); |
| | | child.setIsDelete(null); |
| | | child.setUpdateTime(null); |
| | | child.setParentId(null); |
| | | } |
| | | } |
| | | accountInfoDTO.setAccountId(String.valueOf(manageAccount.getId())); |
| | | accountInfoDTO.setAccountName(manageAccount.getUserName()); |
| | | accountInfoDTO.setAccount(manageAccount); |
| | | accountInfoDTO.setMenus(menus); |
| | | accountInfoDTO.setRoles(roleNames); |
| | | |
| | | accountInfoDTO.setRoles(roles); |
| | | |
| | | //获取token 并且存入缓存 |
| | | String token = TokenUtils.getToken(String.valueOf(manageAccount.getId()), accountInfoDTO); |
| | | |
| | | //封装返回结果 |
| | | loginDTO.setCode(LoginDTO.SUCCESS); |
| | | loginDTO.setAccountInfo(accountInfoDTO); |
| | | loginDTO.setAccountInfoDTO(accountInfoDTO); |
| | | loginDTO.setToken(token); |
| | | |
| | | return loginDTO; |
| | |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | @Override |
| | | public boolean logout(String accountId,String token) { |
| | | public boolean logout(LogoutRequest logoutRequest) { |
| | | String accountId = logoutRequest.getAccountId(); |
| | | String token = logoutRequest.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 AccountDTO addAccount(AccountAddRequest accountAddRequest) { |
| | | AccountDTO accountDTO = new AccountDTO(); |
| | | //取参 |
| | | String AESAccount = accountAddRequest.getAccount(); |
| | | String AESPassword = accountAddRequest.getPassword(); |
| | | String account = AESUtils.decrypt(AESAccount,AESKey); |
| | | String password = MD5Utils.saltMD5(AESUtils.decrypt(AESPassword,AESKey)); |
| | | String userName = accountAddRequest.getUserName(); |
| | | String email = accountAddRequest.getEmail(); |
| | | String mobile = accountAddRequest.getMobile(); |
| | | String wechat = accountAddRequest.getWechat(); |
| | | String isDelete = accountAddRequest.getIsDelete(); |
| | | List<String> roleIdsStr = accountAddRequest.getRoleIds(); |
| | | //校验参数是否符合业务逻辑 |
| | | /*判断用户名是否包含特殊字符*/ |
| | | if(isSpecialChar(account)){ |
| | | accountDTO.setCode(AccountDTO.ACCOUNT_EXIST_SPECIAL_CHAR); |
| | | return accountDTO; |
| | | } |
| | | /*判断密码是否包含特殊字符*/ |
| | | if(isSpecialChar(password)){ |
| | | accountDTO.setCode(AccountDTO.PASSWORD_EXIST_SPECIAL_CHAR); |
| | | return accountDTO; |
| | | } |
| | | /*判断用户名是否超过长度*/ |
| | | if(account.length()>=20){ |
| | | accountDTO.setCode(AccountDTO.ACCOUNT_LENGTH_INVALID); |
| | | return accountDTO; |
| | | } |
| | | /*判断密码是否超过长度*/ |
| | | if(AESUtils.decrypt(AESPassword,AESKey).length()>=20){ |
| | | accountDTO.setCode(AccountDTO.PASSWORD_LENGTH_INVALID); |
| | | return accountDTO; |
| | | } |
| | | /*判断手机号是否符合规则*/ |
| | | if(!isValidMobile(mobile)){ |
| | | accountDTO.setCode(AccountDTO.MOBILE_INVALID); |
| | | return accountDTO; |
| | | } |
| | | /*判断邮箱是否符合规则*/ |
| | | if(!isValidEmail(email)){ |
| | | accountDTO.setCode(AccountDTO.EMAIL_INVALID); |
| | | return accountDTO; |
| | | } |
| | | /*判断用户名是否存在*/ |
| | | ManageAccount exitAccount = new ManageAccount(); |
| | | exitAccount.setAccount(account); |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); |
| | | wrapper.setEntity(exitAccount); |
| | | List<ManageAccount> exitAccounts = manageAccountMapper.selectList(wrapper); |
| | | if(!ObjectUtils.isEmpty(exitAccounts)){ |
| | | accountDTO.setCode(AccountDTO.ACCOUNT_EXIST); |
| | | return accountDTO; |
| | | } |
| | | |
| | | //String to Integer |
| | | List<Integer> roleIds = new ArrayList<>(); |
| | | roleIdsStr.forEach(str->roleIds.add(Integer.parseInt(str))); |
| | | |
| | | //封装account |
| | | ManageAccount manageAccount = new ManageAccount(); |
| | | manageAccount.setAccount(account); |
| | | manageAccount.setPassword(password); |
| | | manageAccount.setUserName(userName); |
| | | manageAccount.setEmail(email); |
| | | manageAccount.setMobile(mobile); |
| | | manageAccount.setWechat(wechat); |
| | | manageAccount.setIsDelete(isDelete); |
| | | manageAccountMapper.insert(manageAccount); |
| | | //封装account_role |
| | | Integer accountId = manageAccount.getId(); |
| | | roleIdsStr.forEach( |
| | | value->{ |
| | | ManageAccountRole manageAccountRole = new ManageAccountRole(); |
| | | manageAccountRole.setAccountId(accountId); |
| | | manageAccountRole.setRoleId(Integer.parseInt(value)); |
| | | manageAccountRoleMapper.insert(manageAccountRole); |
| | | } |
| | | ); |
| | | //封装返回结果 |
| | | List<ManageRole> roles = manageRoleMapper.selectBatchIds(roleIds); |
| | | accountDTO.setAccount(manageAccount); |
| | | accountDTO.setRoles(roles); |
| | | accountDTO.setCode(AccountDTO.SUCCESS); |
| | | return accountDTO; |
| | | } |
| | | |
| | | private static boolean isSpecialChar(String str){ |
| | | Pattern pattern = Pattern.compile(specialCharRegEx); |
| | | Matcher matcher = pattern.matcher(str); |
| | | return matcher.find(); |
| | | } |
| | | |
| | | private static boolean isValidMobile(String str){ |
| | | Pattern pattern = Pattern.compile(mobileRegEx); |
| | | Matcher matcher = pattern.matcher(str); |
| | | return matcher.find(); |
| | | } |
| | | |
| | | private static boolean isValidEmail(String str){ |
| | | Pattern pattern = Pattern.compile(emailRegEx); |
| | | Matcher matcher = pattern.matcher(str); |
| | | return matcher.find(); |
| | | } |
| | | |
| | | |
| | | |
| | | } |