zjh
2023-12-14 448a0c22ec89d5962cbf78d737517aaf176c0240
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
package com.ltkj.web.controller.system;
 
import java.awt.peer.LabelPeer;
import java.util.*;
 
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltkj.common.annotation.RepeatSubmit;
import com.ltkj.common.core.domain.entity.SysDept;
import com.ltkj.common.core.domain.entity.SysRole;
import com.ltkj.common.core.redis.RedisCache;
import com.ltkj.common.exception.CustomException;
import com.ltkj.common.exception.user.SecretKeyException;
import com.ltkj.common.utils.StringUtils;
import com.ltkj.framework.config.UserHoder;
import com.ltkj.hosp.domain.DictHosp;
import com.ltkj.hosp.domain.TjCustomer;
import com.ltkj.hosp.service.IDictHospService;
import com.ltkj.hosp.service.ITjCustomerService;
import com.ltkj.system.domain.SysPost;
import com.ltkj.system.domain.SysRoleMenu;
import com.ltkj.system.domain.SysUserPost;
import com.ltkj.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.ltkj.common.constant.Constants;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.common.core.domain.entity.SysMenu;
import com.ltkj.common.core.domain.entity.SysUser;
import com.ltkj.common.core.domain.model.LoginBody;
import com.ltkj.common.utils.SecurityUtils;
import com.ltkj.framework.web.service.SysLoginService;
import com.ltkj.framework.web.service.SysPermissionService;
 
import javax.annotation.Resource;
 
import static com.ltkj.common.core.domain.AjaxResult.success;
 
/**
 * 登录验证
 *
 * @author ltkj
 */
@RestController
@Api(tags = "项目管理端登录接口")
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;
 
    @Autowired
    private ISysMenuService menuService;
 
    @Autowired
    private SysPermissionService permissionService;
 
    @Resource
    private ITjCustomerService customerService;
    @Resource
    private ISysRoleService roleService;
    @Resource
    private ISysRoleMenuService roleMenuService;
    @Autowired
    private ISysConfigService configService;
    @Resource
    private IDictHospService hospService;
    @Autowired
    private ISysDeptService deptService;
    @Value("${token.secret_key}")
    private String secret;
 
    @Value("${token.secret_key_login}")
    private boolean secretKeyLogin;
    @Autowired
    private RedisCache redisCache;
 
    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation("登录接口")
    @RepeatSubmit
    public AjaxResult login(@RequestBody @ApiParam(value = "登录对象") LoginBody loginBody) {
        AjaxResult ajax = success();
        // 生成令牌
        String token;
        if (loginBody.getMobile()) {
            token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getType(), loginBody.getCode(), loginBody.getUuid());
        } else {
            token = loginService.Cuslogin(loginBody.getUsername(), loginBody.getPassword(), loginBody.getType(), loginBody.getCode(), loginBody.getUuid());
        }
        if (null == token) {
            return success("该账号正在使用中");
        }
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
 
    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        AjaxResult ajax = success();
        SysUser user = UserHoder.getLoginUser().getUser();
        if (null != user.getUserId()) {
            if (secretKeyLogin) {
                if (null != user.getDeptId()) {
                    SysDept sysDept = deptService.getById(user.getDeptId());
                    if (null != sysDept) {
                        DictHosp dictHosp = hospService.getById(sysDept.getHospId());
                        if (null != dictHosp) {
                            user.setHospName(dictHosp.getHospAreaName());
                            user.setHospId(dictHosp.getHospAreaId());
                        }
                        if (!SecurityUtils.isAdmin(user.getUserId())) {
                            if (null != dictHosp && dictHosp.getSecretKey() != null && dictHosp.getExpirationTime() != null) {
                                if (!SecurityUtils.matchesMallMerchantSecretKey(secret, dictHosp.getHospAreaId(), dictHosp.getHospAreaName(),
                                        dictHosp.getExpirationTime(), dictHosp.getSecretKey())) {
 
                                    String userIdKey = Constants.LOGIN_USERID_KEY + user.getUserId();
                                    String userKey = redisCache.getCacheObject(userIdKey);
                                    if (org.apache.commons.lang3.StringUtils.isNotEmpty(userKey)) {
                                        redisCache.deleteObject(userIdKey);
                                        redisCache.deleteObject(userKey);
                                    }
                                    throw new SecretKeyException("商家已过期请续费使用");
                                }
                                long between = DateUtil.between(new Date(), DateUtil.endOfDay(dictHosp.getExpirationTime()), DateUnit.DAY);
                                if (between <= 30) {
                                    UserHoder.getLoginUser().setMessage("还有 " + between + " 天到期 请注意续费! 以免影响正常使用!");
                                }
                            } else {
                                throw new SecretKeyException("请缴费使用");
                            }
                        }
                    }
 
                }
            }
            Set<String> roles = permissionService.getRolePermission(user);
            Set<String> permissions = permissionService.getMenuPermission(user);
            ajax.put("user", user);
            ajax.put("roles", roles);
            ajax.put("permissions", permissions);
            ajax.put("securitMessage", UserHoder.getLoginUser().getMessage());
            ajax.put("hospName", UserHoder.getLoginUser().getHospName());
        } else {
            Set<String> roles = new HashSet<>();
            LambdaQueryWrapper<SysRole> wq = new LambdaQueryWrapper<>();
            String userId = SecurityUtils.getLoginUser().getUserId();
            TjCustomer customer = customerService.getById(userId.substring(3));
            Set<String> permissions = menuService.selectMenuPermsByRoleId(customer.getRole());
            wq.eq(SysRole::getRoleId, customer.getRole());
            List<SysRole> list = roleService.list(wq);
            for (SysRole perm : list) {
                if (StringUtils.isNotNull(perm)) {
                    roles.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
                }
            }
            SysUser user1 = new SysUser();
            user1.setNickName(customer.getCusName());
            ajax.put("user", user1);
            ajax.put("roles", roles);
            ajax.put("permissions", permissions);
            ajax.put("post", null);
            ajax.put("securitMessage", null);
            ajax.put("hospName", UserHoder.getLoginUser().getHospName());
        }
        return ajax;
    }
 
    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        String userId = String.valueOf(SecurityUtils.getLoginUser().getUserId());
        List<SysMenu> menus = null;
        if (!userId.contains("cus")) {
            menus = menuService.selectMenuTreeByUserId(Long.valueOf(userId));
        } else {
            userId = userId.substring(3);
            TjCustomer tjCustomer = customerService.getById(userId);
            LambdaQueryWrapper<SysRoleMenu> wq = new LambdaQueryWrapper<>();
            wq.eq(SysRoleMenu::getRoleId, tjCustomer.getRole());
            List<SysRoleMenu> list = roleMenuService.list(wq);
            if (null != list && list.size() > 0) {
                menus = menuService.getTreeByUserId(list);
            }
        }
        return success(menuService.buildMenus(menus));
    }
 
 
    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/getCaptchaConfigKey")
    @ApiOperation(value = "查询验证码开关 Y开N关")
    @RepeatSubmit
    public AjaxResult getCaptchaConfigKey() {
        return success(configService.selectConfigByKey("captcha_switch"));
    }
}