Commit e5ca5348 by 吕明尚

抖音验卷代码

parent e5b2b132
package share.common.enums;
public enum ErrorCodeEnum {
//0 成功
SUCCESS("0", "成功"),
//2100004 系统繁忙,此时请开发者稍候再试
SYSTEM_ERROR("2100004", "系统繁忙,此时请开发者稍候再试"),
//2100005 参数不合法
PARAM_ERROR("2100005", "参数不合法"),
//2100007 无权限操作
NO_PERMISSION("2100007", "无权限操作"),
;
private String code;
private String message;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
ErrorCodeEnum() {
}
ErrorCodeEnum(String code, String message) {
this.code = code;
this.message = message;
}
}
...@@ -7,6 +7,7 @@ public enum ReceiptRdeisEnum { ...@@ -7,6 +7,7 @@ public enum ReceiptRdeisEnum {
MT_SESSION_OBJECT_KEY(3, "MT_SESSION_OBJECT_KEY"), MT_SESSION_OBJECT_KEY(3, "MT_SESSION_OBJECT_KEY"),
ORDER_NO_KEY(4, "ORDER_NO_KEY."), ORDER_NO_KEY(4, "ORDER_NO_KEY."),
ACCESS_TOKEN_KEY(5, "ACCESS_TOKEN_KEY."), ACCESS_TOKEN_KEY(5, "ACCESS_TOKEN_KEY."),
TIKTOK_CLIENT_TOKEN(6, "TIKTOK_CLIENT_TOKEN."),
ORDER_NO(6, "ORDER_NO."); ORDER_NO(6, "ORDER_NO.");
......
package share.web.controller.system;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import share.system.service.TiktokService;
@RestController
@RequestMapping("/chessCards")
public class TiktokController {
@Autowired
private TiktokService tiktokService;
@GetMapping("/oauth/clientToken")
public void clientToken() {
tiktokService.clientToken();
}
@GetMapping("/certificate/prepare")
public void prepare(String code, String poiId) {
tiktokService.prepare(code, poiId);
}
}
package share.system.service;
public interface TiktokService {
void clientToken();
void prepare(String code, String poiId);
}
package share.system.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import share.common.core.redis.RedisUtil;
import share.common.enums.ErrorCodeEnum;
import share.common.enums.ReceiptRdeisEnum;
import share.system.service.TiktokService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class TiktokServiceImpl implements TiktokService {
private String CLIENT_SECRET;
private String CLIENT_KEY;
@Autowired
private RedisUtil redisUtil;
private static final Logger logger = LoggerFactory.getLogger(TiktokServiceImpl.class);
@Override
public void clientToken() {
String url = "https://open.douyin.com/oauth/client_token/";
HashMap<String, Object> tokenMap = new HashMap<>();
tokenMap.put("client_key", CLIENT_KEY);
tokenMap.put("client_secret", CLIENT_SECRET);
tokenMap.put("grant_type", "client_credentials");
String post = HttpUtil.post(url, tokenMap);
JSONObject sessionKey = new JSONObject(post);
String data = sessionKey.getStr("data");
JSONObject dataJson = new JSONObject(data);
String errorCode = dataJson.getStr("error_code");
if (!errorCode.equals(ErrorCodeEnum.SUCCESS.getCode())) {
logger.error("抖音授权失败:{}", dataJson.getStr("description"));
logger.error("抖音授权失败:{}", dataJson.getStr("error_code"));
throw new RuntimeException("获取token失败");
}
String accessToken = dataJson.getStr("access_token");
String expiresIn = dataJson.getStr("expires_in");
Map<String, String> map = new HashMap<>();
map.put("accessToken", accessToken);
map.put("expiresIn", expiresIn);
//当前时间加上过期时间(秒) 在转为年月日日时分秒
long time = System.currentTimeMillis() + Long.parseLong("7200") * 1000;
Date date = new Date(time);
String expiresTime = DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN);
map.put("expiresTime", expiresTime);
JSONObject jsonObject = new JSONObject(map);
redisUtil.set(ReceiptRdeisEnum.TIKTOK_CLIENT_TOKEN.getValue(), jsonObject);
}
@Override
public void prepare(String code, String poiId) {
String o = redisUtil.get(ReceiptRdeisEnum.TIKTOK_CLIENT_TOKEN.getValue());
JSONObject sessionKey = new JSONObject(o);
String accessToken = sessionKey.getStr("accessToken");
String url = "https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/";
String result = HttpRequest.get(url)
.contentType("application/json")
.header("access-token", accessToken)
.form("code", code).form("poi_id", poiId).execute().body();
}
}
package share.system.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class SignUtil {
/*
* appid/client_key对应的client_secret TODO 这里换成服务商的appsecret
*/
private static final String secret = "12345678901234566543210987654321";
private static final String key;
private static final String iv;
static {
key = parseSecret(secret);
iv = key.substring(16);
}
/**
* @param data base64后的密文
* @return 明文
* @Description AES解密
*/
public static String decryptAES(String data) throws Exception {
try {
byte[] encrypted1 = decode(data);//先用base64解密
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString.trim();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* base64编码
*/
public static String encode(byte[] byteArray) {
return new String(Base64.getEncoder().encode(byteArray));
}
/**
* base64解码
*/
public static byte[] decode(String base64EncodedString) {
return Base64.getDecoder().decode(base64EncodedString);
}
private static String parseSecret(String secret) {
secret = fillSecret(secret);
secret = cutSecret(secret);
return secret;
}
private static String cutSecret(String secret) {
if (secret.length() <= 32) {
return secret;
}
int rightCnt = (secret.length() - 32) / 2;
int leftCnt = secret.length() - 32 - rightCnt;
return secret.substring(leftCnt, 32 + leftCnt);
}
private static String fillSecret(String secret) {
if (secret.length() >= 32) {
return secret;
}
int rightCnt = (32 - secret.length()) / 2;
int leftCnt = 32 - secret.length() - rightCnt;
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < leftCnt; i++) {
sb.append('#');
}
sb.append(secret);
for (int i = 0; i < rightCnt; i++) {
sb.append('#');
}
return sb.toString();
}
}
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