wang-hao-jie
2021-10-19 e48043a2df9ca0c73fe18298bab3c4d42ca5c0c7
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
package cn.exrick.xboot.social.controller;
 
import cn.exrick.xboot.core.common.constant.CommonConstant;
import cn.exrick.xboot.core.common.redis.RedisTemplateHelper;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.User;
import cn.exrick.xboot.core.service.UserService;
import cn.exrick.xboot.social.entity.Social;
import cn.exrick.xboot.social.service.SocialService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.concurrent.TimeUnit;
 
/**
 * @author Exrickx
 */
@Slf4j
@Api(tags = "绑定第三方账号接口")
@RequestMapping("/xboot/social")
@RestController
@Transactional
public class RelateController {
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private SocialService socialService;
 
    @Autowired
    private SecurityUtil securityUtil;
 
    @Autowired
    private RedisTemplateHelper redisTemplate;
 
    @RequestMapping(value = "/relate", method = RequestMethod.POST)
    @ApiOperation(value = "绑定账号")
    public Result<Object> relate(@RequestParam Boolean isLogin,
                                 @RequestParam(required = false) String username,
                                 @RequestParam(required = false) String password,
                                 @RequestParam Integer socialType,
                                 @RequestParam String id) {
 
        if (isLogin) {
            // 用户已登录
            User user = securityUtil.getCurrUser();
            username = user.getUsername();
        } else {
            // 用户未登录
            if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
                return ResultUtil.error("用户名或密码不能为空");
            }
            User user = userService.findByUsername(username);
            if (user == null) {
                return ResultUtil.error("账号不存在");
            }
            if (!new BCryptPasswordEncoder().matches(password, user.getPassword())) {
                return ResultUtil.error("密码不正确");
            }
        }
 
        // 从redis中获取表id
        String ID = redisTemplate.get(id);
        if (StrUtil.isBlank(ID)) {
            return ResultUtil.error("无效的id");
        }
 
        String platform = "";
        if (CommonConstant.SOCIAL_TYPE_GITHUB.equals(socialType)) {
            platform = "Github";
        } else if (CommonConstant.SOCIAL_TYPE_WECHAT.equals(socialType)) {
            platform = "微信";
        } else if (CommonConstant.SOCIAL_TYPE_QQ.equals(socialType)) {
            platform = "QQ";
        } else if (CommonConstant.SOCIAL_TYPE_WEIBO.equals(socialType)) {
            platform = "微博";
        } else if (CommonConstant.SOCIAL_TYPE_DINGDING.equals(socialType)) {
            platform = "钉钉";
        } else if (CommonConstant.SOCIAL_TYPE_WORKWECHAT.equals(socialType)) {
            platform = "企业微信";
        }
        Social s = socialService.findByRelateUsernameAndPlatform(username, socialType);
        if (s != null) {
            return ResultUtil.error("该账户已绑定有" + platform + "账号,请先进行解绑操作");
        }
        Social social = socialService.get(ID);
        if (social == null) {
            return ResultUtil.error("绑定失败,请先进行第三方授权认证");
        }
        if (StrUtil.isNotBlank(social.getRelateUsername())) {
            return ResultUtil.error("该" + platform + "账号已绑定有用户,请先进行解绑操作");
        }
        social.setRelateUsername(username);
        socialService.update(social);
 
        if (!isLogin) {
            String JWT = securityUtil.getToken(username, true);
            // 存入redis
            String JWTKey = IdUtil.simpleUUID();
            redisTemplate.set(JWTKey, JWT, 2L, TimeUnit.MINUTES);
            return ResultUtil.data(JWTKey);
        } else {
            return ResultUtil.data("绑定成功");
        }
    }
 
    @RequestMapping(value = "/getJWT", method = RequestMethod.GET)
    @ApiOperation(value = "获取JWT")
    public Result<Object> getJWT(@RequestParam String JWTKey) {
 
        String JWT = redisTemplate.get(JWTKey);
        if (StrUtil.isBlank(JWT)) {
            return ResultUtil.error("获取JWT失败");
        }
        return ResultUtil.data(JWT);
    }
}