Commit c8e3be05 by 吕明尚

抖音验卷接口

parent e5ca5348
...@@ -6,6 +6,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -6,6 +6,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import share.system.service.TiktokService; import share.system.service.TiktokService;
import java.util.List;
@RestController @RestController
@RequestMapping("/chessCards") @RequestMapping("/chessCards")
public class TiktokController { public class TiktokController {
...@@ -18,8 +20,12 @@ public class TiktokController { ...@@ -18,8 +20,12 @@ public class TiktokController {
} }
@GetMapping("/certificate/prepare") @GetMapping("/certificate/prepare")
public void prepare(String code, String poiId) { public String prepare(String code, String poiId) {
tiktokService.prepare(code, poiId); return tiktokService.prepare(code, poiId);
} }
@GetMapping("/certificate/verify")
public String verify(String verifyToken, String poiId, List<String> encryptedCodes) {
return tiktokService.verify(verifyToken, poiId, encryptedCodes);
}
} }
package share.system.service; package share.system.service;
import java.util.List;
public interface TiktokService { public interface TiktokService {
void clientToken(); void clientToken();
void prepare(String code, String poiId); String prepare(String code, String poiId);
String verify(String verifyToken, String poiId, List<String> encryptedCodes);
} }
...@@ -18,6 +18,7 @@ import share.system.service.TiktokService; ...@@ -18,6 +18,7 @@ import share.system.service.TiktokService;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@Service @Service
...@@ -41,8 +42,7 @@ public class TiktokServiceImpl implements TiktokService { ...@@ -41,8 +42,7 @@ public class TiktokServiceImpl implements TiktokService {
tokenMap.put("grant_type", "client_credentials"); tokenMap.put("grant_type", "client_credentials");
String post = HttpUtil.post(url, tokenMap); String post = HttpUtil.post(url, tokenMap);
JSONObject sessionKey = new JSONObject(post); JSONObject sessionKey = new JSONObject(post);
String data = sessionKey.getStr("data"); JSONObject dataJson = sessionKey.getJSONObject("data");
JSONObject dataJson = new JSONObject(data);
String errorCode = dataJson.getStr("error_code"); String errorCode = dataJson.getStr("error_code");
if (!errorCode.equals(ErrorCodeEnum.SUCCESS.getCode())) { if (!errorCode.equals(ErrorCodeEnum.SUCCESS.getCode())) {
logger.error("抖音授权失败:{}", dataJson.getStr("description")); logger.error("抖音授权失败:{}", dataJson.getStr("description"));
...@@ -64,15 +64,69 @@ public class TiktokServiceImpl implements TiktokService { ...@@ -64,15 +64,69 @@ public class TiktokServiceImpl implements TiktokService {
} }
@Override @Override
public void prepare(String code, String poiId) { public String prepare(String code, String poiId) {
String o = redisUtil.get(ReceiptRdeisEnum.TIKTOK_CLIENT_TOKEN.getValue()); String accessToken = obtain();
JSONObject sessionKey = new JSONObject(o);
String accessToken = sessionKey.getStr("accessToken");
String url = "https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/"; String url = "https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/";
String result = HttpRequest.get(url) String result = HttpRequest.get(url)
.contentType("application/json") .contentType("application/json")
.header("access-token", accessToken) .header("access-token", accessToken)
.form("code", code).form("poi_id", poiId).execute().body(); .form("code", code).form("poi_id", poiId).execute().body();
JSONObject entries = new JSONObject(result);
JSONObject data = entries.getJSONObject("data");
if (!data.getStr("error_code").equals(ErrorCodeEnum.SUCCESS.getCode())) {
logger.error("抖音验券准备失败:{}", data.getStr("description"));
throw new RuntimeException(data.getStr("description"));
}
return data.getStr("verify_token");
}
@Override
public String verify(String verifyToken, String poiId, List<String> encryptedCodes) {
String accessToken = obtain();
String url = "https://open.douyin.com/goodlife/v1/fulfilment/certificate/verify/";
Map<String, Object> map = new HashMap<>();
map.put("verify_token", verifyToken);
map.put("poi_id", poiId);
map.put("encrypted_codes", encryptedCodes);
JSONObject jsonObject = new JSONObject(map);
String result = HttpRequest.post(url)
.contentType("application/json")
.header("access-token", accessToken)
.body(jsonObject.toString()).execute().body();
JSONObject entries = new JSONObject(result);
JSONObject data = entries.getJSONObject("data");
JSONObject extra = entries.getJSONObject("extra");
if (data.getStr("error_code").equals(ErrorCodeEnum.SUCCESS.getCode()) && data.getJSONObject("verify_results").getStr("result").equals(ErrorCodeEnum.SUCCESS.getCode())) {
return "验卷成功";
} else if (data.getStr("error_code").equals("1208") || data.getJSONObject("verify_results").getStr("result").equals("2")) {
return "验卷成功";
} else if (!data.getStr("error_code").equals(ErrorCodeEnum.SUCCESS.getCode())) {
return "验卷失败";
}
return "验卷失败";
} }
public String obtain() {
String o;
o = redisUtil.get(ReceiptRdeisEnum.TIKTOK_CLIENT_TOKEN.getValue());
//过期时间小于当前时间 重新获取token
if (new JSONObject(o).getDate("expiresTime").getTime() < System.currentTimeMillis()) {
clientToken();
o = redisUtil.get(ReceiptRdeisEnum.TIKTOK_CLIENT_TOKEN.getValue());
}
JSONObject sessionKey = new JSONObject(o);
return sessionKey.getStr("accessToken");
}
public void compensate(String verifyToken, String poiId, List<String> encryptedCodes) {
//间隔5秒
try {
Thread.sleep(5000);
verify(verifyToken, poiId, encryptedCodes);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
} }
package share.system.util; package share.system.util;
import org.springframework.beans.factory.annotation.Value;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
...@@ -9,7 +11,9 @@ public class SignUtil { ...@@ -9,7 +11,9 @@ public class SignUtil {
/* /*
* appid/client_key对应的client_secret TODO 这里换成服务商的appsecret * appid/client_key对应的client_secret TODO 这里换成服务商的appsecret
*/ */
// @Value("${client_secret}")
private static final String secret = "12345678901234566543210987654321"; private static final String secret = "12345678901234566543210987654321";
// @Value("${client_key}")
private static final String key; private static final String key;
private static final String iv; private static final String iv;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment