init
This commit is contained in:
30
src/main/java/com/newyetai/sdk/bytedance/ByteDanceUtils.java
Normal file
30
src/main/java/com/newyetai/sdk/bytedance/ByteDanceUtils.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.newyetai.sdk.bytedance;
|
||||
|
||||
import com.newyetai.common.AlitaResult;
|
||||
import com.newyetai.sdk.bytedance.constant.ByteDanceConstants;
|
||||
import com.newyetai.sdk.bytedance.model.BaseByteDanceDTO;
|
||||
import com.newyetai.sdk.common.constant.SdkConstants;
|
||||
import com.newyetai.sdk.common.dto.ISdkRequest;
|
||||
import com.newyetai.sdk.common.exception.SdkException;
|
||||
import com.newyetai.sdk.common.util.HttpUtils;
|
||||
|
||||
/**
|
||||
* @author sr on 2021-11-25
|
||||
*/
|
||||
public class ByteDanceUtils {
|
||||
|
||||
public static <T> AlitaResult<T> execute(ISdkRequest<BaseByteDanceDTO<T>> req) {
|
||||
try {
|
||||
BaseByteDanceDTO<T> dto = HttpUtils.post(req);
|
||||
Long errNo = dto.getErrNo();
|
||||
if (ByteDanceConstants.SUCCESS_CODE.equals(errNo)) {
|
||||
return AlitaResult.success(dto.getData());
|
||||
}
|
||||
return AlitaResult.error(SdkConstants.SDK_HTTP_INVOKE_RESPONSE_FAIL_CODE,
|
||||
dto.getErrNo() + dto.getErrTips());
|
||||
} catch (SdkException e) {
|
||||
return AlitaResult.error("字节接口调用失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.newyetai.sdk.bytedance.constant;
|
||||
|
||||
/**
|
||||
* 字节常量
|
||||
* @author sr on 2021-11-25
|
||||
*/
|
||||
public interface ByteDanceConstants {
|
||||
|
||||
Long SUCCESS_CODE = 0L;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.newyetai.sdk.bytedance.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AccessTokenDataDTO {
|
||||
|
||||
/** **/
|
||||
private static final long serialVersionUID = 814049874223007628L;
|
||||
|
||||
/** 接口调用凭证 **/
|
||||
@JsonProperty("access_token")
|
||||
private String accessToken;
|
||||
|
||||
/** access_token接口调用凭证超时时间,单位(秒) **/
|
||||
@JsonProperty("expires_in")
|
||||
private String expiresIn;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.newyetai.sdk.bytedance.model;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.newyetai.common.annotation.Mask;
|
||||
import com.newyetai.sdk.common.dto.ISdkRequest;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Builder.Default;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class AccessTokenReqDTO implements ISdkRequest<BaseByteDanceDTO<AccessTokenDataDTO>>{
|
||||
|
||||
/** **/
|
||||
private static final long serialVersionUID = 197693764614332172L;
|
||||
private static ParameterizedTypeReference<BaseByteDanceDTO<AccessTokenDataDTO>> type =
|
||||
new ParameterizedTypeReference<BaseByteDanceDTO<AccessTokenDataDTO>>() {};
|
||||
|
||||
/** 第三方用户唯一凭证 **/
|
||||
@JsonProperty("appid")
|
||||
private String appId;
|
||||
@Mask
|
||||
/** 第三方用户唯一凭证密钥,即appsecret **/
|
||||
private String secret;
|
||||
@Default
|
||||
@JsonProperty("grant_type")
|
||||
private String grantType = "client_credential";
|
||||
|
||||
@Override
|
||||
public String getUri() {
|
||||
return "https://developer.toutiao.com/api/apps/v2/token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterizedTypeReference<BaseByteDanceDTO<AccessTokenDataDTO>> getResponseType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.newyetai.sdk.bytedance.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.newyetai.sdk.common.dto.ISdkResponse;
|
||||
|
||||
@Data
|
||||
public class BaseByteDanceDTO<T> implements ISdkResponse, Serializable {
|
||||
|
||||
/** 错误编码 **/
|
||||
@JsonProperty("err_no")
|
||||
private Long errNo;
|
||||
|
||||
/** 错误信息 **/
|
||||
@JsonProperty("err_tips")
|
||||
private String errTips;
|
||||
|
||||
private T data;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.newyetai.sdk.bytedance.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author sr on 2021-11-25
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Code2SessionDataDTO implements Serializable {
|
||||
|
||||
/** **/
|
||||
private static final long serialVersionUID = -5908915158294552716L;
|
||||
|
||||
/** 会话密钥,如果请求时有 code 参数才会返回 **/
|
||||
@JsonProperty("session_key")
|
||||
private String sessionKey;
|
||||
|
||||
/** 用户在当前小程序的 ID,如果请求时有 code 参数才会返回 **/
|
||||
@JsonProperty("openid")
|
||||
private String openId;
|
||||
|
||||
/** 匿名用户在当前小程序的 ID,如果请求时有 anonymous_code 参数才会返回 **/
|
||||
@JsonProperty("anonymous_openid")
|
||||
private String anonymousOpenId;
|
||||
|
||||
|
||||
/** 用户在小程序平台的唯一标识符,请求时有 code 参数才会返回。如果开发者拥有多个小程序,可通过 unionid 来区分用户的唯一性 **/
|
||||
@JsonProperty("unionid")
|
||||
private String unionId;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.newyetai.sdk.bytedance.model;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.newyetai.common.annotation.Mask;
|
||||
import com.newyetai.sdk.common.dto.ISdkRequest;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class Code2SessionReqDTO implements ISdkRequest<BaseByteDanceDTO<Code2SessionDataDTO>> {
|
||||
|
||||
/** **/
|
||||
private static final long serialVersionUID = 197693764614332172L;
|
||||
private static ParameterizedTypeReference<BaseByteDanceDTO<Code2SessionDataDTO>> type =
|
||||
new ParameterizedTypeReference<BaseByteDanceDTO<Code2SessionDataDTO>>() {};
|
||||
|
||||
/** 第三方用户唯一凭证 **/
|
||||
@JsonProperty("appid")
|
||||
private String appId;
|
||||
@Mask
|
||||
/** 第三方用户唯一凭证密钥,即appsecret **/
|
||||
private String secret;
|
||||
/** login 接口返回的登录凭证 **/
|
||||
private String code;
|
||||
/** login 接口返回的匿名登录凭证 **/
|
||||
@JsonProperty("anonymous_code")
|
||||
private String anonymousCode;
|
||||
|
||||
@Override
|
||||
public String getUri() {
|
||||
return "https://developer.toutiao.com/api/apps/v2/jscode2session";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterizedTypeReference<BaseByteDanceDTO<Code2SessionDataDTO>> getResponseType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user