Browse Source

友盟一键登录

master
nili 7 months ago
parent
commit
cb67c18371
  1. 22
      game-service/src/main/java/awesome/group/game/service/UMengService.java
  2. 13
      game-service/src/main/java/awesome/group/game/service/bo/citrus/UMengRes.java
  3. 22
      game-service/src/main/java/awesome/group/game/service/citrus/UserService.java
  4. 13
      game-service/src/test/java/awesome/group/game/service/UMengServiceTest.java

22
game-service/src/main/java/awesome/group/game/service/UMengService.java

@ -1,5 +1,7 @@
package awesome.group.game.service;
import awesome.group.game.service.bo.citrus.UMengRes;
import awesome.group.game.service.bo.matrix.UmengConfigBo;
import awesome.group.game.service.common.exception.PaganiException;
import awesome.group.game.service.common.exception.PaganiExceptionCode;
import awesome.group.game.service.common.log.L;
@ -36,17 +38,19 @@ public class UMengService {
private final OkHttpClient httpClient = new OkHttpClient().newBuilder().connectTimeout(2, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).connectionPool(new ConnectionPool(8, 5, TimeUnit.MINUTES)).build();
public String getMobile(String token) {
public String getMobile(String token, UmengConfigBo umengConfigBo) {
Map<String, Object> data = new HashMap<>();
data.put("token", token);
String api = "/api/v1/mobile/info?appkey=" + APP_KEY_UMENT;
String res = doHttpPost(api, data);
System.out.println(res);
return res;
String api = "/api/v1/mobile/info?appkey=" + umengConfigBo.umengAppKey;
String res = doHttpPost(api, data, umengConfigBo);
UMengRes uMengRes = gson.fromJson(res, UMengRes.class);
if (uMengRes.success) {
return uMengRes.data.mobile;
}
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "获取手机号失败");
}
public String doHttpPost(String url, Map<String, Object> jsonBody) {
public String doHttpPost(String url, Map<String, Object> jsonBody, UmengConfigBo configBo) {
Map<String, String> header = new HashMap<>();
String body = gson.toJson(jsonBody);
@ -55,7 +59,7 @@ public class UMengService {
header.put("X-Ca-Version", "1");
header.put("X-Ca-Signature-Headers", "X-Ca-Version,X-Ca-Stage,X-Ca-Key,X-Ca-Timestamp");
header.put("X-Ca-Stage", "RELEASE");
header.put("X-Ca-Key", APP_KEY);
header.put("X-Ca-Key", configBo.umengAppKeyAli);
header.put("X-Ca-Timestamp", String.valueOf(System.currentTimeMillis()));
header.put("X-Ca-Nonce", UUID.randomUUID().toString());
header.put("Content-MD5", Base64.encodeBase64String(DigestUtils.md5(body.getBytes())));
@ -66,7 +70,7 @@ public class UMengService {
try {
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
byte[] keyBytes = APP_SECRET.getBytes(StandardCharsets.UTF_8);
byte[] keyBytes = configBo.umengAppSecret.getBytes(StandardCharsets.UTF_8);
hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
String sign = new String(Base64.encodeBase64(hmacSha256.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8))));
header.put("X-Ca-Signature", sign);

13
game-service/src/main/java/awesome/group/game/service/bo/citrus/UMengRes.java

@ -0,0 +1,13 @@
package awesome.group.game.service.bo.citrus;
public class UMengRes {
public boolean success;
public Integer code;
public String message;
public Data data;
public static class Data {
public String mobile;
}
}

22
game-service/src/main/java/awesome/group/game/service/citrus/UserService.java

@ -7,16 +7,17 @@ import awesome.group.game.dao.mapper.MatrixAppMapper;
import awesome.group.game.dao.mapper.MatrixMoneyRecordMapper;
import awesome.group.game.dao.mapper.MatrixUserMapper;
import awesome.group.game.service.SmsService;
import awesome.group.game.service.UMengService;
import awesome.group.game.service.bo.citrus.AppCashConfig;
import awesome.group.game.service.bo.citrus.CashRecord;
import awesome.group.game.service.bo.citrus.LoginReq;
import awesome.group.game.service.bo.citrus.UserBo;
import awesome.group.game.service.bo.matrix.UmengConfigBo;
import awesome.group.game.service.common.exception.PaganiException;
import awesome.group.game.service.common.exception.PaganiExceptionCode;
import awesome.group.game.service.util.DateUtil;
import awesome.group.game.service.util.EncryptUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@ -45,6 +46,9 @@ public class UserService {
@Autowired
private UserService userService;
@Autowired
private UMengService uMengService;
public static final int STATUS_WAITING = 0;//等审核
public static final int STATUS_AUDIT_PASS = 1;//已审核通过
@ -89,9 +93,17 @@ public class UserService {
Assert.isTrue(StringUtils.hasText(token), "非法请求");
MatrixApp app = appMapper.queryByCode(appCode);
Assert.isTrue(app != null, "非法请求");
LambdaQueryWrapper<MatrixUser> wrapper = Wrappers.lambdaQuery();
wrapper.eq(MatrixUser::getAppId, app.getId());
return userMapper.selectOne(wrapper);
Gson gson = new Gson();
UmengConfigBo configBo = gson.fromJson(app.getUmeng(), UmengConfigBo.class);
String mobile = uMengService.getMobile(token, configBo);
MatrixUser user = userMapper.selectByAppIdAndMobile(app.getId(), mobile);
if (user == null) {
user = new MatrixUser();
user.setAppId(app.getId());
user.setMobile(mobile);
userMapper.insert(user);
}
return user;
}
public UserBo getUser(Integer uid) {

13
game-service/src/test/java/awesome/group/game/service/UMengServiceTest.java

@ -1,10 +1,10 @@
package awesome.group.game.service;
import awesome.group.game.service.bo.matrix.UmengConfigBo;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.jupiter.api.Assertions.*;
class UMengServiceTest extends BaseTest{
@Autowired
@ -13,6 +13,13 @@ class UMengServiceTest extends BaseTest{
@Test
void getMobile() {
String token = "eyJjIjoiNmk5amtxSVwvNXhXRElTRkNIeUpcL1RTemNjTEJsOEtiWndkT0NEYlhkUFJQREUzZ3g1bWx1bmNYdzRtclBWODVYOEludnJzS1pTOTRRXG53ejRudVlSaUs4cTFtRUdcL2FPQ0tIRktiUUtuS2Iya0ZFWjlyNWpPV044R0w3Q2I3V1g5dDA0ays2dmJwUXpaNGVTMklyOTJJcGtqaVxudm0rVG5vWVVVZWl4ZHhNNDArZ0hlaXBFRUpnUjlQNjhyZzY1VitrU1wvaWFQbXMxM3h4dEtGd2c3OVA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.getMobile(token);
UmengConfigBo bo = new UmengConfigBo();
bo.umengAppSecret = "7e3a9e8ae85cd8c1ff58343e241ec154";
bo.umengAppKey = "66403e5dcac2a664de3234d4";
bo.umengAppKeyAli = "24785365";
bo.umengAppCode = "2c62d2208aa640f09a55449efa634424";
Gson gson = new Gson();
String str = gson.toJson(bo);
uMengService.getMobile(token, bo);
}
}
Loading…
Cancel
Save