leutu
2024-06-03 3ef35e6cd16bbfa206b26bb3271eac40ad020bcb
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
package com.fastbee.iot.service.impl;
 
import com.fastbee.common.enums.SocialPlatformType;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.iot.domain.SocialPlatform;
import com.fastbee.iot.model.login.AuthRequestWrap;
import com.fastbee.iot.service.IAuthRequestFactory;
import com.fastbee.iot.service.ISocialPlatformService;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthQqRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWeChatOpenRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Locale;
 
@Service
public class AuthRequestFactoryImpl implements IAuthRequestFactory {
 
    private static final Logger log = LoggerFactory.getLogger(AuthRequestFactoryImpl.class);
 
    @Autowired
    private ISocialPlatformService iSocialPlatformService;
 
    @Autowired
    private AuthStateRedisCache authStateRedisCache;
 
    /**
     * 获得对于AUthRequest
     *
     * @param source 登录方式
     * @return 对应AuthRequest
     */
    @Override
    public AuthRequestWrap getAuthRequest(String source) {
        AuthRequestWrap authRequestWrap = new AuthRequestWrap();
        AuthRequest authRequest;
        try {
            String lowerSource = source.toLowerCase(Locale.ROOT);
            SocialPlatformType socialPlatformType = SocialPlatformType.getSocialPlatformType(lowerSource);
            if (socialPlatformType == null) {
                throw new ServiceException("未获取到第三方平台来源");
            }
            SocialPlatform socialPlatform = iSocialPlatformService.selectSocialPlatformByPlatform(lowerSource);
            authRequestWrap.setSocialPlatform(socialPlatform);
            AuthConfig authConfig = AuthConfig.builder()
                    .clientId(socialPlatform.getClientId())
                    .clientSecret(socialPlatform.getSecretKey())
                    .redirectUri(socialPlatform.getRedirectUri())
                    .build();
            switch (socialPlatformType) {
                case WECHAT_OPEN_WEB: {
                    authRequest = new AuthWeChatOpenRequest(authConfig, authStateRedisCache);
                    break;
                }
                case QQ_OPEN_WEB: {
                    authRequest = new AuthQqRequest(authConfig, authStateRedisCache);
                    break;
                }
                default: {
                    throw new ServiceException("source: " + source + ",暂不支持");
                }
            }
            authRequestWrap.setAuthRequest(authRequest);
            return authRequestWrap;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
 
}