kaiyu
2021-03-15 2ddf3630707aa6515f8c193334f928d5255542cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.moral.api.service.impl;
 
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.MD5Utils;
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>
 * 后台账户表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class ManageAccountServiceImpl extends ServiceImpl<ManageAccountMapper, ManageAccount> implements ManageAccountService {
 
    @Value("${AES.KEY}")
    private String AESKey;
    @Resource
    ManageAccountMapper manageAccountMapper;
    @Resource
    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]
     * @return: java.util.Map<java.lang.String   ,   java.lang.Object>
     * @Author: 陈凯裕
     * @Date: 2021/3/11
     */
    @Override
    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 = manageAccountMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(manageAccount)) {
            loginDTO.setCode(LoginDTO.NOT_EXIST);
            return loginDTO;
        }
        //查询是否逻辑删除
        if (manageAccount.getIsDelete().equals("1")) {
            loginDTO.setCode(LoginDTO.IS_DELETE);
            return loginDTO;
        }
        //校验密码
        if (!MD5Utils.saltMD5Verify(password, manageAccount.getPassword())) {
            loginDTO.setCode(LoginDTO.PASSWORD_ERROR);
            return loginDTO;
        }
 
        //查询角色
        List<ManageRole> roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
        if (ObjectUtils.isEmpty(roles)) {//判断账号是否存在角色
            loginDTO.setCode(LoginDTO.ROLE_EMPTY);
            return loginDTO;
        }
        //查询菜单
        List<ManageMenu> menus = manageMenuMapper.getParentChildrenMenusByRoles(roles);
        if (ObjectUtils.isEmpty(menus)) {//判断账号是否存在菜单
            loginDTO.setCode(LoginDTO.MENU_EMPTY);
            return loginDTO;
        }
 
        //封装用户信息
        AccountInfoDTO accountInfoDTO = new AccountInfoDTO();
        accountInfoDTO.setAccount(manageAccount);
        accountInfoDTO.setMenus(menus);
        accountInfoDTO.setRoles(roles);
 
        //获取token 并且存入缓存
        String token = TokenUtils.getToken(String.valueOf(manageAccount.getId()), accountInfoDTO);
 
        //封装返回结果
        loginDTO.setCode(LoginDTO.SUCCESS);
        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(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();
    }
 
 
 
}