cl
2024-01-22 8de0bed14d364e3d3adae6b6e2260b838d180c23
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
package cn.stylefeng.guns.oauth2.service.impl;
 
import cn.stylefeng.guns.base.auth.service.AuthService;
import cn.stylefeng.guns.base.oauth2.service.OauthUserInfoService;
import cn.stylefeng.guns.oauth2.service.LoginService;
import cn.stylefeng.guns.sys.core.exception.oauth.OAuthExceptionEnum;
import cn.stylefeng.guns.sys.core.exception.oauth.OAuthLoginException;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.guns.base.oauth2.entity.OauthUserInfo;
import cn.stylefeng.guns.oauth2.factory.OAuthUserInfoFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 默认第三方登录逻辑
 *
 * @author fengshuonan
 * @Date 2019/6/9 18:16
 */
@Service
public class DefaultLoginService implements LoginService {
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private OauthUserInfoService oauthUserInfoService;
 
    @Autowired
    private AuthService authService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String oauthLogin(AuthUser oauthUser) {
 
        if (oauthUser == null) {
            throw new OAuthLoginException(OAuthExceptionEnum.OAUTH_RESPONSE_ERROR);
        }
 
        //当前无登录用户,创建用户或根据已有绑定用户的账号登录
        String account = getOauthUserAccount(oauthUser);
 
        //执行原有系统登录逻辑
        return authService.login(account);
    }
 
 
    /**
     * 绑定当前用户的source和openId
     *
     * @author fengshuonan
     * @Date 2019/6/9 18:51
     */
    private void bindOAuthUser(Long userId, AuthUser oauthUser) {
 
        //先判断当前系统这个openId有没有人用
        QueryWrapper<OauthUserInfo> queryWrapper = new QueryWrapper<OauthUserInfo>()
                .eq("source", oauthUser.getSource().name())
                .and(i -> i.eq("uuid", oauthUser.getUuid()))
                .and(i -> i.ne("user_id", userId));
        List<OauthUserInfo> oauthUserInfos = this.oauthUserInfoService.list(queryWrapper);
 
        //已有人绑定,抛出异常
        if (oauthUserInfos != null && oauthUserInfos.size() > 0) {
            throw new OAuthLoginException(OAuthExceptionEnum.OPEN_ID_ALREADY_BIND);
        } else {
            //新建一条绑定记录
            OauthUserInfo oAuthUserInfo = OAuthUserInfoFactory.createOAuthUserInfo(userId, oauthUser);
            this.oauthUserInfoService.save(oAuthUserInfo);
        }
 
    }
 
    /**
     * 通过第三方登录的信息创建本系统用户
     *
     * @author fengshuonan
     * @Date 2019/6/9 19:07
     */
    private String getOauthUserAccount(AuthUser oauthUser) {
 
        //先判断当前系统这个openId有没有人用
        QueryWrapper<OauthUserInfo> queryWrapper = new QueryWrapper<OauthUserInfo>()
                .eq("source", oauthUser.getSource().name())
                .and(i -> i.eq("uuid", oauthUser.getUuid()));
        OauthUserInfo oauthUserInfos = this.oauthUserInfoService.getOne(queryWrapper);
 
        //已有人绑定,直接返回这个人的账号,进行登录
        if (oauthUserInfos != null) {
            Long userId = oauthUserInfos.getUserId();
            return this.userService.getById(userId).getAccount();
        } else {
 
            //没有人绑定的创建这个人的本系统用户
            User user = OAuthUserInfoFactory.createOAuthUser(oauthUser);
            this.userService.save(user);
 
            //新建一条oauth2绑定记录
            OauthUserInfo oAuthUserInfo = OAuthUserInfoFactory.createOAuthUserInfo(user.getUserId(), oauthUser);
            this.oauthUserInfoService.save(oAuthUserInfo);
 
            return user.getAccount();
        }
    }
 
}