pull/3/head
tanghc 4 years ago
parent 994960da31
commit 87a2a3a629
  1. 95
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java
  2. 82
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java

@ -1,8 +1,23 @@
package com.gitee.sop.servercommon.bean; package com.gitee.sop.servercommon.bean;
import org.apache.commons.lang3.time.DateUtils;
import java.text.ParseException;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import static com.gitee.sop.servercommon.bean.ParamNames.API_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.APP_AUTH_TOKEN_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.APP_KEY_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.BIZ_CONTENT_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.CHARSET_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.FORMAT_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.NOTIFY_URL_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.SIGN_TYPE_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.TIMESTAMP_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.TIMESTAMP_PATTERN;
import static com.gitee.sop.servercommon.bean.ParamNames.VERSION_NAME;
/** /**
* 获取开放平台请求参数 * 获取开放平台请求参数
* *
@ -10,81 +25,109 @@ import java.util.Map;
*/ */
public interface OpenContext { public interface OpenContext {
/**
* 获取某个参数值
* @param name 参数名称
* @return 没有返回null
*/
String getParameter(String name);
/** /**
* 返回所有的请求参数 * 返回所有的请求参数
*
* @return 返回所有的请求参数 * @return 返回所有的请求参数
*/ */
Map<String, Object> getParameterMap(); Map<String, Object> getParameterMap();
/** /**
* 返回appid * 返回业务对象
* *
* @return 返回appid * @return 业务对象
*/ */
String getAppId(); Object getBizObject();
/** /**
* 返回业务参数json字符串 * 获取某个参数值
* *
* @return 返回字符串业务参数 * @param name 参数名称
* @return 没有返回null
*/ */
String getBizContent(); default String getParameter(String name) {
Object value = getParameterMap().get(name);
return value == null ? null : value.toString();
}
/** /**
* 返回业务对象 * 返回appid
* *
* @return 业务对象 * @return 返回appId
*/ */
Object getBizObject(); default String getAppId() {
return getParameter(APP_KEY_NAME);
}
/**
* 返回业务参数json字符串
*
* @return 返回字符串业务参数
*/
default String getBizContent() {
return getParameter(BIZ_CONTENT_NAME);
}
/** /**
* 返回字符编码 * 返回字符编码
* *
* @return 如UTF-8 * @return 如UTF-8
*/ */
String getCharset(); default String getCharset() {
return getParameter(CHARSET_NAME);
}
/** /**
* 返回接口名 * 返回接口名
* *
* @return alipay.goods.get * @return alipay.goods.get
*/ */
String getMethod(); default String getMethod() {
return getParameter(API_NAME);
}
/** /**
* 返回版本号 * 返回版本号
* *
* @return 1.0 * @return 1.0
*/ */
String getVersion(); default String getVersion() {
return getParameter(VERSION_NAME);
}
/** /**
* 返回参数格式化 * 返回参数格式化
* *
* @return json * @return json
*/ */
String getFormat(); default String getFormat() {
return getParameter(FORMAT_NAME);
}
/** /**
* 返回签名类型 * 返回签名类型
* *
* @return RSA2 * @return RSA2
*/ */
String getSignType(); default String getSignType() {
return getParameter(SIGN_TYPE_NAME);
}
/** /**
* 返回时间戳 * 返回时间戳
* *
* @return * @return
*/ */
Date getTimestamp(); default Date getTimestamp() {
String timestampStr = getParameter(TIMESTAMP_NAME);
try {
return DateUtils.parseDate(timestampStr, TIMESTAMP_PATTERN);
} catch (ParseException e) {
return null;
}
}
/** /**
@ -92,13 +135,17 @@ public interface OpenContext {
* *
* @return 返回token * @return 返回token
*/ */
String getAppAuthToken(); default String getAppAuthToken() {
return getParameter(APP_AUTH_TOKEN_NAME);
}
/** /**
* 返回回调地址 * 返回回调地址
* *
* @return 返回回调地址 * @return 返回回调地址
*/ */
String getNotifyUrl(); default String getNotifyUrl() {
return getParameter(NOTIFY_URL_NAME);
}
} }

@ -1,32 +1,15 @@
package com.gitee.sop.servercommon.bean; package com.gitee.sop.servercommon.bean;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateUtils;
import java.text.ParseException;
import java.util.Date;
import java.util.Map; import java.util.Map;
import static com.gitee.sop.servercommon.bean.ParamNames.API_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.APP_AUTH_TOKEN_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.APP_KEY_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.BIZ_CONTENT_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.CHARSET_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.FORMAT_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.NOTIFY_URL_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.SIGN_TYPE_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.TIMESTAMP_NAME;
import static com.gitee.sop.servercommon.bean.ParamNames.TIMESTAMP_PATTERN;
import static com.gitee.sop.servercommon.bean.ParamNames.VERSION_NAME;
/** /**
* @author tanghc * @author tanghc
*/ */
public class OpenContextImpl implements OpenContext { public class OpenContextImpl implements OpenContext {
private final JSONObject requestParams; private final Map<String, Object> requestParams;
private Object bizObject; private Object bizObject;
public OpenContextImpl(JSONObject requestParams) { public OpenContextImpl(Map<String, Object> requestParams) {
this.requestParams = requestParams; this.requestParams = requestParams;
} }
@ -34,77 +17,16 @@ public class OpenContextImpl implements OpenContext {
this.bizObject = bizObject; this.bizObject = bizObject;
} }
@Override
public String getParameter(String name) {
return requestParams.getString(name);
}
@Override @Override
public Map<String, Object> getParameterMap() { public Map<String, Object> getParameterMap() {
return requestParams; return requestParams;
} }
@Override
public String getAppId() {
return requestParams.getString(APP_KEY_NAME);
}
@Override @Override
public Object getBizObject() { public Object getBizObject() {
return bizObject; return bizObject;
} }
@Override
public String getBizContent() {
return requestParams.getString(BIZ_CONTENT_NAME);
}
@Override
public String getCharset() {
return requestParams.getString(CHARSET_NAME);
}
@Override
public String getMethod() {
return requestParams.getString(API_NAME);
}
@Override
public String getVersion() {
return requestParams.getString(VERSION_NAME);
}
@Override
public String getFormat() {
return requestParams.getString(FORMAT_NAME);
}
@Override
public String getSignType() {
return requestParams.getString(SIGN_TYPE_NAME);
}
@Override
public Date getTimestamp() {
String timestampStr = requestParams.getString(TIMESTAMP_NAME);
try {
return DateUtils.parseDate(timestampStr, TIMESTAMP_PATTERN);
} catch (ParseException e) {
return null;
}
}
@Override
public String getAppAuthToken() {
return requestParams.getString(APP_AUTH_TOKEN_NAME);
}
@Override
public String getNotifyUrl() {
return requestParams.getString(NOTIFY_URL_NAME);
}
@Override @Override
public String toString() { public String toString() {
return requestParams.toString(); return requestParams.toString();

Loading…
Cancel
Save