86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
package cn.mb.cloud.auth.security.social.handler;
 
import cn.mb.cloud.auth.security.entity.User;
import cn.mb.cloud.auth.security.service.IUserService;
import cn.mb.cloud.auth.security.util.AppleUtil;
import cn.mb.cloud.common.api.vo.UserVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
 
/**
 * @author jason
 * @date 2018/11/18
 */
@Slf4j
@Component("APPLE")
@AllArgsConstructor
public class AppleLoginHandler extends AbstractLoginHandler {
 
    private final IUserService userService;
 
    /**
     * 苹果登录传入identityToken
     * <p>
     *
     * @param identityToken
     * @return
     */
    @Override
    public String identify(String identityToken) {
        String email = null;
        if (!StringUtils.isEmpty(identityToken)) {
            JSONObject json = null;
            try {
                System.out.println("identityToken:" + identityToken);
                //验证identityToken
                if (!AppleUtil.verify(identityToken)) {
                    return null;
                }
                //对identityToken解码
                json = AppleUtil.parserIdentityToken(identityToken);
                if (json == null) {
                    return null;
                }
                System.out.println(json.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            email = json.getString("email");
        }
        return email;
    }
 
    /**
     * openId 获取用户信息
     *
     * @param openId
     * @return
     */
    @Override
    public UserVO info(String openId, String threeAvatar, String threeName) {
        User user = userService.getOne(Wrappers.<User>query().lambda().eq(User::getOpenId, openId));
        if (user == null) {
           /* log.info("苹果未绑定:{}", openId);
            return null;*/
            user = userService.getOne(Wrappers.<User>query().lambda()
                    .eq(User::getOpenId, openId)
                    .eq(User::getDelFlag, 0));
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        if (StringUtils.isEmpty(user.getUsername())) {
            userVO.setUsername(user.getOpenId().toString());
        }
        //登录更新设备号,设备类型,版本号
        return userVO;
    }
 
}