kaiyu
2021-03-10 5097d13418e9a0bf605f5272f1b9e60fc62c80cb
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
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.ManageMenu;
import com.moral.api.entity.ManageRole;
import com.moral.api.mapper.ManageAccountMapper;
import com.moral.api.mapper.ManageMenuMapper;
import com.moral.api.mapper.ManageRoleMapper;
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.util.ObjectUtils;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * <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 accountMapper;
    @Resource
    ManageRoleMapper roleMapper;
    @Resource
    ManageMenuMapper manageMenuMapper;
 
 
    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");
        //解密
        String account = AESUtils.decrypt(cyrpAccount, AESKey);
        String password = AESUtils.decrypt(cyrpPassword, AESKey);
        //查询是否存在
        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;
        }
        //查询是否逻辑删除
        if(manageAccount.getIsDelete().equals("1")){
            result.put("accountId",-1);
            result.put("msg","用户已被封禁");
            return result;
        }
        //校验密码
        if(!MD5Utils.saltMD5Verify(password,manageAccount.getPassword())){
            result.put("accountId",-1);
            result.put("msg","用户名密码错误");
            return result;
        }
        //查询角色
        List<ManageRole> roles = roleMapper.getManageRoleByAccountId(manageAccount.getId());
        if(ObjectUtils.isEmpty(roles)){
            result.put("accountId",-1);
            result.put("msg","用户尚未分配角色");
            return result;
        }
        //查询菜单
        List<ManageMenu> menus = manageMenuMapper.getParentChildrenMenusByRoles(roles);
        if(ObjectUtils.isEmpty(menus)){
            result.put("accountId",-1);
            result.put("msg","用户尚未分配菜单");
            return result;
        }
 
        //获取用户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",-1);
            result.put("msg","生成token错误");
            return result;
        }
 
        //打包返回信息
        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;
    }
 
 
}