This commit is contained in:
六如
2024-11-04 14:37:54 +08:00
parent d9a257ff23
commit 53a40a1cb4
14 changed files with 285 additions and 65 deletions

View File

@@ -0,0 +1,43 @@
package com.gitee.sop.support.constants;
/**
* RPC附件name
*
* @author 六如
*/
public class AttachmentNames {
/**
* 分配给开发者的应用ID
*/
public static final String APP_ID = "client.app_id";
/**
* 接口名称
*/
public static final String API_NAME = "client.method";
/**
* 调用的接口版本
*/
public static final String VERSION = "client.version";
/**
* 开放平台主动通知商户服务器里指定的页面http/https路径
*/
public static final String NOTIFY_URL = "client.notify_url";
/**
* OAuth 2.0授权token
*/
public static final String APP_AUTH_TOKEN = "client.app_auth_token";
/**
* 请求id
*/
public static final String CLIENT_IP = "client.ip";
/**
* 请求traceId
*/
public static final String TRACE_ID = "client.trace_id";
/**
* 请求locale
*/
public static final String LOCALE = "client.locale";
}

View File

@@ -1,70 +1,77 @@
package com.gitee.sop.support.context;
import com.gitee.sop.support.constants.AttachmentNames;
import org.apache.dubbo.rpc.RpcContext;
import java.util.Locale;
/**
* @author 六如
*/
public class OpenContext {
/** 分配给开发者的应用ID */
private static final String APP_ID_NAME = "client.app_id";
/** 接口名称 */
private static final String API_NAME = "client.method";
/** 调用的接口版本 */
private static final String VERSION_NAME = "client.version";
/** 开放平台主动通知商户服务器里指定的页面http/https路径 */
private static final String NOTIFY_URL_NAME = "client.notify_url";
/** OAuth 2.0授权token */
private static final String APP_AUTH_TOKEN_NAME = "client.app_auth_token";
private static final String CLIENT_IP = "client.ip";
private static final String TRACE_ID = "client.trace_id";
/**
* 获取appId
*/
public static String getAppId() {
return RpcContext.getServerAttachment().getAttachment(APP_ID_NAME);
return getAttachment(AttachmentNames.APP_ID);
}
/**
* 获取apiName
*/
public static String getApiName() {
return RpcContext.getServerAttachment().getAttachment(API_NAME);
return getAttachment(AttachmentNames.API_NAME);
}
/**
* 获取version
*/
public static String getVersion() {
return RpcContext.getServerAttachment().getAttachment(VERSION_NAME);
return getAttachment(AttachmentNames.VERSION);
}
/**
* 获取token,没有返回null
*/
public static String getAppAuthToken() {
return RpcContext.getServerAttachment().getAttachment(APP_AUTH_TOKEN_NAME);
return getAttachment(AttachmentNames.APP_AUTH_TOKEN);
}
/**
* 获取客户端ip
*/
public static String getClientIp() {
return RpcContext.getServerAttachment().getAttachment(CLIENT_IP);
return getAttachment(AttachmentNames.CLIENT_IP);
}
/**
* 获取回调地址
*/
public static String getNotifyUrl() {
return RpcContext.getServerAttachment().getAttachment(NOTIFY_URL_NAME);
return getAttachment(AttachmentNames.NOTIFY_URL);
}
/**
* 获取唯一请求id
*/
public static String getTraceId() {
return RpcContext.getServerAttachment().getAttachment(TRACE_ID);
return getAttachment(AttachmentNames.TRACE_ID);
}
/**
* 获取locale
*/
public static Locale getLocale() {
String langTag = getAttachment(AttachmentNames.LOCALE);
if (langTag == null) {
return Locale.SIMPLIFIED_CHINESE;
}
return Locale.forLanguageTag(langTag);
}
private static String getAttachment(String key) {
return RpcContext.getServerAttachment().getAttachment(key);
}
}

View File

@@ -0,0 +1,54 @@
package com.gitee.sop.support.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.Locale;
/**
* @author 六如
*/
@Data
public class DefaultOpenRequest implements OpenRequest, Serializable {
private static final long serialVersionUID = -3218354527911979685L;
/**
* appId
*/
private String appId;
/**
* apiName
*/
private String apiName;
/**
* version
*/
private String version;
/**
* token,没有返回null
*/
private String appAuthToken;
/**
* 客户端ip
*/
private String clientIp;
/**
* 回调地址
*/
private String notifyUrl;
/**
* 唯一请求id
*/
private String traceId;
/**
* locale
*/
private Locale locale;
}

View File

@@ -0,0 +1,50 @@
package com.gitee.sop.support.dto;
import java.util.Locale;
/**
* @author 六如
*/
public interface OpenRequest {
/**
* 获取appId
*/
String getAppId();
/**
* 获取apiName
*/
String getApiName();
/**
* 获取version
*/
String getVersion();
/**
* 获取token,没有返回null
*/
String getAppAuthToken();
/**
* 获取客户端ip
*/
String getClientIp();
/**
* 获取回调地址
*/
String getNotifyUrl();
/**
* 获取唯一请求id
*/
String getTraceId();
/**
* 获取locale
*/
Locale getLocale();
}

View File

@@ -1,5 +1,6 @@
package com.gitee.sop.support.exception;
import com.gitee.sop.support.context.OpenContext;
import com.gitee.sop.support.message.OpenError;
import com.gitee.sop.support.message.OpenMessage;
import com.gitee.sop.support.message.OpenMessageFactory;
@@ -30,7 +31,7 @@ public class OpenException extends RuntimeException {
}
public OpenException(OpenError openError, Object... params) {
this(openError, Locale.SIMPLIFIED_CHINESE, params);
this(openError, OpenContext.getLocale(), params);
}
@Override

View File

@@ -1 +1,2 @@
com.gitee.sop.support.dto.CommonFileData
com.gitee.sop.support.dto.DefaultOpenRequest