123
无关风月
2024-12-27 c0d6d27f6b93b60b83b046900289c831c8092126
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
package com.jilongda.optometrist.authority.controller;
 
import com.jilongda.common.basic.ApiResult;
import com.jilongda.common.basic.Constant;
import com.jilongda.common.exception.ServiceException;
import com.jilongda.common.log.OperLoginLog;
import com.jilongda.common.redis.RedisAutoTemplate;
import com.jilongda.common.security.SecurityUtils;
import com.jilongda.common.utils.WebUtils;
import com.jilongda.optometrist.authority.dto.LoginCodeDTO;
import com.jilongda.optometrist.authority.dto.LoginDTO;
import com.jilongda.optometrist.authority.dto.VerificationCodeDTO;
import com.jilongda.optometrist.authority.model.SecUser;
import com.jilongda.optometrist.authority.service.SecUserService;
import com.jilongda.optometrist.model.TOptometrist;
import com.jilongda.optometrist.security.SecurityUserDetails;
import com.jilongda.optometrist.service.TOptometristService;
import com.jilongda.optometrist.utils.MsgUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author xiaochen
 * @ClassName LoginController
 * @Description
 * @date 2022-05-23 17:24
 */
@Slf4j
@Api(tags = "验光师登录 相关接口")
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping("/")
public class LoginController {
 
    private final AuthenticationManager authenticationManager;
    private final SecurityUtils securityUtils;
    private final PasswordEncoder passwordEncoder;
    private final RedisAutoTemplate redisAutoTemplate;
    private final MsgUtils msgUtils;
    private final SecUserService secUserService;
    @Autowired
    private TOptometristService optometristService;
 
    @Autowired
    public LoginController(AuthenticationManager authenticationManager, SecurityUtils securityUtils, PasswordEncoder passwordEncoder, RedisAutoTemplate redisAutoTemplate, MsgUtils msgUtils, SecUserService secUserService) {
        this.authenticationManager = authenticationManager;
        this.securityUtils = securityUtils;
        this.passwordEncoder = passwordEncoder;
        this.redisAutoTemplate = redisAutoTemplate;
        this.msgUtils = msgUtils;
        this.secUserService = secUserService;
    }
    /**
     * 登录接口
     */
    @ApiOperation("验光师短信登录")
    @PostMapping(value = "code/login")
    public ApiResult<Map<String, Object>> loginByCode(@Validated @RequestBody LoginCodeDTO dto) {
        TOptometrist one = optometristService.lambdaQuery().eq(TOptometrist::getPhone, dto.getPhone()).one();
        if (one==null){
            return ApiResult.failed(500, "手机号未注册");
        }
        TOptometrist two = optometristService.lambdaQuery().eq(TOptometrist::getPhone, dto.getPhone())
                .eq(TOptometrist::getStatus, 2)
                .one();
        if (two!=null){
            return ApiResult.failed(500, "账号已被禁用");
        }
        // 先检验是否登录
        String phone = dto.getPhone();
        String code = dto.getCode();
        // 校验验证码是否正确
        String redisCode = redisAutoTemplate.getStr(dto.getPhone());
        Assert.isTrue(StringUtils.hasLength(redisCode), "验证码已过期");
        if (!code.equals(redisCode)) {
            throw new ServiceException(500, "验证码错误,请重新输入验证码!");
        }
        try {
            Map<String, Object> token = securityUtils.login(phone, code, authenticationManager, SecurityUserDetails.class, 2);
            return ApiResult.success(token);
        } catch (Exception e) {
            ApiResult<Map<String, Object>> failed = ApiResult.failed(new HashMap<>(1));
            failed.setCode(0);
            failed.setSuccess(false);
            failed.setMsg(e.getMessage());
            return failed;
        }
    }
 
 
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    @PostMapping(value = "sendMsg")
    public ApiResult<String> sendMsg(@Validated @RequestBody VerificationCodeDTO dto) throws Exception {
        // 发送验证码并存储到redis
        if (StringUtils.hasLength(dto.getPhone())) {
            SecUser one = secUserService.lambdaQuery().eq(SecUser::getPhone, dto.getPhone())
                    .eq(SecUser::getUserType, 2).one();
            if (one==null){
                return ApiResult.failed(500, "手机号未注册");
            }
            SecUser two = secUserService.lambdaQuery().eq(SecUser::getPhone, dto.getPhone())
                    .eq(SecUser::getUserType, 2)
                    .eq(SecUser::getState,0)
                    .one();
            if (two==null){
                return ApiResult.failed(500, "账号已被禁用");
            }
 
            String code = String.valueOf((int) (Math.random() * 1000000));
            redisAutoTemplate.setStr(dto.getPhone(), code);
            redisAutoTemplate.expire(dto.getPhone(), Constant.REDIS_EXPIRE);
            msgUtils.sendMsg(dto.getPhone(), code);
            return ApiResult.success("发送短信验证码成功!10分钟内有效");
        }
        return ApiResult.failed(500, "发送短信验证码失败,请确认手机号码!");
 
    }
 
 
    /**
     * 退出
     *
     * @return 退出登录
     */
    @ApiOperation("验光师退出登录")
    @GetMapping("logout")
    public ApiResult<String> logout() {
        boolean flag = securityUtils.invalidateToken(WebUtils.request());
        if (flag) {
            return ApiResult.success("退出成功");
        }
        return ApiResult.success("退出失败");
    }
 
    /**
     * h5登录
     */
    /*@ApiOperation("h5登录")
    @PostMapping(value = "h5/login")
    public ApiResult<Map<String, Object>> webLogin(@Validated @RequestBody LoginDTO loginDto) {
        // 先检验是否登录
        String username = loginDto.getAccount();
        String password = loginDto.getPassword();
        EvaluatePerson evaluatePerson = evaluatePersonService.getOne(Wrappers.lambdaQuery(EvaluatePerson.class)
                .eq(EvaluatePerson::getPersonAccount, username).last(" LIMIT 1"));
        if (Objects.isNull(evaluatePerson)) {
            throw new UsernameNotFoundException("该用户不存在");
        }
        // 根据加密算法加密用户输入的密码,然后和数据库中保存的密码进行比较
        if (!passwordEncoder.matches(password, evaluatePerson.getPersonPassword())) {
            throw new BadCredentialsException("输入账号或密码不正确");
        }
        try {
            Map<String, Object> token = securityUtils.login(username, evaluatePerson, authenticationManager, EvaluatePerson.class,2);
            String account = JwtTokenUtils.getUsername();
            log.info("登录账号=============:{}",account);
            return ApiResult.success(token);
        } catch (Exception e) {
            ApiResult<Map<String, Object>> failed = ApiResult.failed(new HashMap<>(1));
            failed.setCode(0);
            failed.setSuccess(false);
            failed.setMsg(e.getMessage());
            return failed;
        }
    }*/
 
}