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
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
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.common.api.vo.UserVO;
import cn.mb.cloud.common.core.exception.ValidateCodeException;
import cn.mb.cloud.common.core.util.WebUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.api.gateway.demo.Client;
import com.aliyun.api.gateway.demo.Request;
import com.aliyun.api.gateway.demo.Response;
import com.aliyun.api.gateway.demo.constant.Constants;
import com.aliyun.api.gateway.demo.constant.ContentType;
import com.aliyun.api.gateway.demo.constant.HttpHeader;
import com.aliyun.api.gateway.demo.constant.HttpSchema;
import com.aliyun.api.gateway.demo.enums.Method;
import com.aliyun.api.gateway.demo.util.MessageDigestUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
@Slf4j
@Component("OT")
@AllArgsConstructor
public class OneTouchLoginHandler extends AbstractLoginHandler {
 
    private final IUserService userService;
 
    /**
     * 友盟苹果key
     */
    //private final String iOSKey = "5fe2e2181283d52b40b31b2f";
    private final String iOSKey = "600a7d656a2a470e8f86e6ef";
 
    /**
     * 友盟安卓key
     */
    private final String androidKey = "5e09c1a50cafb2153b000f33";
 
    /**
     * 阿里云认证服务Key
     */
    private final String aliyunKey = "24600523";
 
    /**
     * 阿里云认证服务Secret
     */
    private final String aliyunSecret = "710e0107b022277a6bf4deafe9d6d971";
 
    @Override
    public String identify(String code) {
        return code;
    }
 
    @Override
    @SneakyThrows
    public UserVO info(String identify, String threeAvatar, String threeName) {
        // 获取设备类型 0安卓 1苹果
        String deviceType = WebUtils.getRequest().getHeader("DeviceType");
        if (StringUtils.isEmpty(deviceType)) {
            log.info("未获取到设备类型");
            throw new ValidateCodeException("未获取到设备类型");
        }
        String mobile = null;
        JSONObject result = postString(identify, deviceType);
        if (result.containsKey("success")) {
            // 判断是否成功
            if (result.getBoolean("success")) {
                JSONObject data = result.getJSONObject("data");
                mobile = data.getString("mobile");
            } else {
                log.info(result.getString("message"));
                throw new ValidateCodeException(result.getString("message"));
            }
        }
        User user = null;
        user = userService.getOne(Wrappers.<User>query().lambda().
                eq(User::getUsername, mobile).
                eq(User::getDelFlag, 0));
        if (user == null) {
           /* log.info("一键登录未绑定:{}", identify);
            throw new ValidateCodeException("用户还未注册");*/
            //注册
 
            user = userService.getOne(Wrappers.<User>query().lambda()
                    .eq(User::getUsername, mobile)
                    .eq(User::getDelFlag, 0)
                    );
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        //登录更新设备号,设备类型,版本号
 
        return userVO;
    }
 
    public JSONObject postString(String token, String deviceType) throws Exception {
        String appKey = null;
        if (deviceType.equals("0")) {
            appKey = androidKey;
        } else {
            appKey = iOSKey;
        }
        //请求path
        String path = "/api/v1/mobile/info";
        //Body内容
        String body = "{\n" +
                "\t\"token\":\"" + token + "\"\n" +
                "}";
        Map<String, String> headers = new HashMap<String, String>();
        //(必填)根据期望的Response内容类型设置
        headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json");
        //(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
        //(POST/PUT请求必选)请求Body内容格式
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
 
        headers.put("X-Ca-Nonce", UUID.randomUUID().toString());
 
        Request request = new Request(Method.POST_STRING, HttpSchema.HTTPS + "verify5.market.alicloudapi.com", path, aliyunKey, aliyunSecret, Constants.DEFAULT_TIMEOUT);
        request.setHeaders(headers);
//        request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
 
        //请求的query
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("appkey", appKey);
        request.setQuerys(querys);
        request.setStringBody(body);
        //调用服务端
        Response response = Client.execute(request);
        String responseBody = response.getBody();
        log.info("一键登录返回日志:{}", JSON.toJSONString(responseBody));
        return JSONObject.parseObject(responseBody);
    }
}