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; 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.PaganiException;
import awesome.group.game.service.common.exception.PaganiExceptionCode; import awesome.group.game.service.common.exception.PaganiExceptionCode;
import awesome.group.game.service.common.log.L; 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(); 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<>(); Map<String, Object> data = new HashMap<>();
data.put("token", token); data.put("token", token);
String api = "/api/v1/mobile/info?appkey=" + APP_KEY_UMENT; String api = "/api/v1/mobile/info?appkey=" + umengConfigBo.umengAppKey;
String res = doHttpPost(api, data); String res = doHttpPost(api, data, umengConfigBo);
System.out.println(res); UMengRes uMengRes = gson.fromJson(res, UMengRes.class);
if (uMengRes.success) {
return res; 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<>(); Map<String, String> header = new HashMap<>();
String body = gson.toJson(jsonBody); String body = gson.toJson(jsonBody);
@ -55,7 +59,7 @@ public class UMengService {
header.put("X-Ca-Version", "1"); 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-Signature-Headers", "X-Ca-Version,X-Ca-Stage,X-Ca-Key,X-Ca-Timestamp");
header.put("X-Ca-Stage", "RELEASE"); 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-Timestamp", String.valueOf(System.currentTimeMillis()));
header.put("X-Ca-Nonce", UUID.randomUUID().toString()); header.put("X-Ca-Nonce", UUID.randomUUID().toString());
header.put("Content-MD5", Base64.encodeBase64String(DigestUtils.md5(body.getBytes()))); header.put("Content-MD5", Base64.encodeBase64String(DigestUtils.md5(body.getBytes())));
@ -66,7 +70,7 @@ public class UMengService {
try { try {
Mac hmacSha256 = Mac.getInstance("HmacSHA256"); 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")); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
String sign = new String(Base64.encodeBase64(hmacSha256.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)))); String sign = new String(Base64.encodeBase64(hmacSha256.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8))));
header.put("X-Ca-Signature", sign); 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.MatrixMoneyRecordMapper;
import awesome.group.game.dao.mapper.MatrixUserMapper; import awesome.group.game.dao.mapper.MatrixUserMapper;
import awesome.group.game.service.SmsService; 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.AppCashConfig;
import awesome.group.game.service.bo.citrus.CashRecord; import awesome.group.game.service.bo.citrus.CashRecord;
import awesome.group.game.service.bo.citrus.LoginReq; import awesome.group.game.service.bo.citrus.LoginReq;
import awesome.group.game.service.bo.citrus.UserBo; 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.PaganiException;
import awesome.group.game.service.common.exception.PaganiExceptionCode; import awesome.group.game.service.common.exception.PaganiExceptionCode;
import awesome.group.game.service.util.DateUtil; import awesome.group.game.service.util.DateUtil;
import awesome.group.game.service.util.EncryptUtil; import awesome.group.game.service.util.EncryptUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.google.gson.Gson;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -45,6 +46,9 @@ public class UserService {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private UMengService uMengService;
public static final int STATUS_WAITING = 0;//等审核 public static final int STATUS_WAITING = 0;//等审核
public static final int STATUS_AUDIT_PASS = 1;//已审核通过 public static final int STATUS_AUDIT_PASS = 1;//已审核通过
@ -89,9 +93,17 @@ public class UserService {
Assert.isTrue(StringUtils.hasText(token), "非法请求"); Assert.isTrue(StringUtils.hasText(token), "非法请求");
MatrixApp app = appMapper.queryByCode(appCode); MatrixApp app = appMapper.queryByCode(appCode);
Assert.isTrue(app != null, "非法请求"); Assert.isTrue(app != null, "非法请求");
LambdaQueryWrapper<MatrixUser> wrapper = Wrappers.lambdaQuery(); Gson gson = new Gson();
wrapper.eq(MatrixUser::getAppId, app.getId()); UmengConfigBo configBo = gson.fromJson(app.getUmeng(), UmengConfigBo.class);
return userMapper.selectOne(wrapper); 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) { 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; 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.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.jupiter.api.Assertions.*;
class UMengServiceTest extends BaseTest{ class UMengServiceTest extends BaseTest{
@Autowired @Autowired
@ -13,6 +13,13 @@ class UMengServiceTest extends BaseTest{
@Test @Test
void getMobile() { void getMobile() {
String token = "eyJjIjoiNmk5amtxSVwvNXhXRElTRkNIeUpcL1RTemNjTEJsOEtiWndkT0NEYlhkUFJQREUzZ3g1bWx1bmNYdzRtclBWODVYOEludnJzS1pTOTRRXG53ejRudVlSaUs4cTFtRUdcL2FPQ0tIRktiUUtuS2Iya0ZFWjlyNWpPV044R0w3Q2I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zh4R1lCYW04T093TVpoTElWSER5VVpvSThySFBOejRcL1ZKdm9XWXhwMEVNcWpyRkF0anV1cENDb1RKWUMzVWtXTmpMUkRLcG1za3Y2RHBjc2lBeHRvZnBPdVwvak04ZnR2dFhJOFBDQVwvU0Q4cHFENytKZDdqXC81Z0VQTUFndGgrdnZOYnp4VThrbU5cL01SVmRXK2FpUlFZSGswVFRZUnZ6eEJONDYwMCtDRjd2Vlh3djVcL1RUTmNBUmxCZkpkTFZnUnpnSFBjSE03Qk1NRzFRRW5PV3BGcjAyd29NS1dUdHZ6NndxYWdnQU9WeWFkdmgrRzg4eU9TRGNaTXBxbG5xOGtXQlFoY2QrampTZjRZaDdiOGNpdUZubWlzYmlXUmpmdz09IiwibyI6IkFuZHJvaWQifQ==";
uMengService.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