mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-12 07:02:14 +08:00
4.0.3
This commit is contained in:
@@ -10,7 +10,9 @@ ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange);
|
|||||||
## 微服务端如何获取appId等参数
|
## 微服务端如何获取appId等参数
|
||||||
|
|
||||||
```java
|
```java
|
||||||
String app_id = request.getParameter("app_id");
|
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
|
||||||
|
System.out.println("app_id:" + openContext.getAppId());
|
||||||
|
System.out.println("token:" + openContext.getAppAuthToken());
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
package com.gitee.sop.servercommon.bean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
public interface OpenBeanFactory {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回业务参数对象
|
|
||||||
*
|
|
||||||
* @param clazz 业务参数类class
|
|
||||||
* @param <T> 业务参数对象
|
|
||||||
* @return 返回业务参数对象
|
|
||||||
*/
|
|
||||||
<T> T getBizObject(Class<T> clazz);
|
|
||||||
}
|
|
@@ -0,0 +1,90 @@
|
|||||||
|
package com.gitee.sop.servercommon.bean;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取开放平台请求参数。
|
||||||
|
*
|
||||||
|
* @author tanghc
|
||||||
|
*/
|
||||||
|
public interface OpenContext {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回appid
|
||||||
|
*
|
||||||
|
* @return 返回appid
|
||||||
|
*/
|
||||||
|
String getAppId();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回业务参数json字符串
|
||||||
|
*
|
||||||
|
* @return 返回字符串业务参数
|
||||||
|
*/
|
||||||
|
String getBizContent();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回业务对象
|
||||||
|
*
|
||||||
|
* @return 业务对象
|
||||||
|
*/
|
||||||
|
Object getBizObject();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回字符编码
|
||||||
|
*
|
||||||
|
* @return 如UTF-8
|
||||||
|
*/
|
||||||
|
String getCharset();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回接口名
|
||||||
|
*
|
||||||
|
* @return 如:alipay.goods.get
|
||||||
|
*/
|
||||||
|
String getMethod();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回版本号
|
||||||
|
*
|
||||||
|
* @return 如:1.0
|
||||||
|
*/
|
||||||
|
String getVersion();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回参数格式化
|
||||||
|
*
|
||||||
|
* @return 如:json
|
||||||
|
*/
|
||||||
|
String getFormat();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回签名类型
|
||||||
|
*
|
||||||
|
* @return 如:RSA2
|
||||||
|
*/
|
||||||
|
String getSignType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回时间戳
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Date getTimestamp();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回token
|
||||||
|
*
|
||||||
|
* @return 返回token
|
||||||
|
*/
|
||||||
|
String getAppAuthToken();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回回调地址
|
||||||
|
*
|
||||||
|
* @return 返回回调地址
|
||||||
|
*/
|
||||||
|
String getNotifyUrl();
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,101 @@
|
|||||||
|
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 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
|
||||||
|
*/
|
||||||
|
public class OpenContextImpl implements OpenContext {
|
||||||
|
private JSONObject requestParams;
|
||||||
|
private Object bizObject;
|
||||||
|
|
||||||
|
public OpenContextImpl(JSONObject requestParams) {
|
||||||
|
this.requestParams = requestParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBizObject(Object bizObject) {
|
||||||
|
this.bizObject = bizObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAppId() {
|
||||||
|
return requestParams.getString(APP_KEY_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getBizObject() {
|
||||||
|
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
|
||||||
|
public String toString() {
|
||||||
|
return requestParams.toString();
|
||||||
|
}
|
||||||
|
}
|
@@ -10,8 +10,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public class ServiceContext extends ConcurrentHashMap<String, Object> {
|
public class ServiceContext extends ConcurrentHashMap<String, Object> {
|
||||||
|
|
||||||
public static final String REQUEST_KEY = "request";
|
public static final String REQUEST_KEY = "sop-request";
|
||||||
public static final String RESPONSE_KEY = "response";
|
public static final String RESPONSE_KEY = "sop-response";
|
||||||
|
public static final String OPEN_CONTEXT_KEY = "sop-open-context";
|
||||||
|
|
||||||
protected static Class<? extends ServiceContext> contextClass = ServiceContext.class;
|
protected static Class<? extends ServiceContext> contextClass = ServiceContext.class;
|
||||||
|
|
||||||
@@ -31,6 +32,14 @@ public class ServiceContext extends ConcurrentHashMap<String, Object> {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setOpenContext(OpenContext openContext) {
|
||||||
|
set(OPEN_CONTEXT_KEY, openContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OpenContext getOpenContext() {
|
||||||
|
return (OpenContext) get(OPEN_CONTEXT_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
public Locale getLocale() {
|
public Locale getLocale() {
|
||||||
HttpServletRequest request = getRequest();
|
HttpServletRequest request = getRequest();
|
||||||
if (request == null) {
|
if (request == null) {
|
||||||
|
@@ -4,7 +4,11 @@ import com.alibaba.fastjson.JSON;
|
|||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.alibaba.fastjson.JSONValidator;
|
import com.alibaba.fastjson.JSONValidator;
|
||||||
import com.gitee.sop.servercommon.annotation.Open;
|
import com.gitee.sop.servercommon.annotation.Open;
|
||||||
|
import com.gitee.sop.servercommon.bean.OpenContext;
|
||||||
|
import com.gitee.sop.servercommon.bean.OpenContextImpl;
|
||||||
import com.gitee.sop.servercommon.bean.ParamNames;
|
import com.gitee.sop.servercommon.bean.ParamNames;
|
||||||
|
import com.gitee.sop.servercommon.bean.ServiceConfig;
|
||||||
|
import com.gitee.sop.servercommon.bean.ServiceContext;
|
||||||
import com.gitee.sop.servercommon.util.OpenUtil;
|
import com.gitee.sop.servercommon.util.OpenUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
@@ -133,12 +137,18 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver {
|
|||||||
}
|
}
|
||||||
HandlerMethodArgumentResolver resolver = getOtherArgumentResolver(methodParameter);
|
HandlerMethodArgumentResolver resolver = getOtherArgumentResolver(methodParameter);
|
||||||
if (resolver != null) {
|
if (resolver != null) {
|
||||||
return resolver.resolveArgument(
|
Object param = resolver.resolveArgument(
|
||||||
methodParameter
|
methodParameter
|
||||||
, modelAndViewContainer
|
, modelAndViewContainer
|
||||||
, nativeWebRequest
|
, nativeWebRequest
|
||||||
, webDataBinderFactory
|
, webDataBinderFactory
|
||||||
);
|
);
|
||||||
|
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
|
||||||
|
if (openContext instanceof OpenContextImpl) {
|
||||||
|
OpenContextImpl openContextImpl = (OpenContextImpl) openContext;
|
||||||
|
openContextImpl.setBizObject(param);
|
||||||
|
}
|
||||||
|
return param;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -153,7 +163,10 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver {
|
|||||||
*/
|
*/
|
||||||
protected Object getParamObject(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) {
|
protected Object getParamObject(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) {
|
||||||
HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();
|
HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();
|
||||||
|
ServiceContext currentContext = ServiceContext.getCurrentContext();
|
||||||
JSONObject requestParams = OpenUtil.getRequestParams(request);
|
JSONObject requestParams = OpenUtil.getRequestParams(request);
|
||||||
|
OpenContextImpl openContext = new OpenContextImpl(requestParams);
|
||||||
|
currentContext.setOpenContext(openContext);
|
||||||
String bizContent = requestParams.getString(ParamNames.BIZ_CONTENT_NAME);
|
String bizContent = requestParams.getString(ParamNames.BIZ_CONTENT_NAME);
|
||||||
if (bizContent == null) {
|
if (bizContent == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -169,6 +182,7 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver {
|
|||||||
Map<String, Object> query = OpenUtil.parseQueryToMap(bizContent);
|
Map<String, Object> query = OpenUtil.parseQueryToMap(bizContent);
|
||||||
param = new JSONObject(query).toJavaObject(parameterType);
|
param = new JSONObject(query).toJavaObject(parameterType);
|
||||||
}
|
}
|
||||||
|
openContext.setBizObject(param);
|
||||||
this.bindUploadFile(param, request);
|
this.bindUploadFile(param, request);
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
|
|||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.gitee.sop.servercommon.annotation.BizCode;
|
import com.gitee.sop.servercommon.annotation.BizCode;
|
||||||
import com.gitee.sop.servercommon.annotation.Open;
|
import com.gitee.sop.servercommon.annotation.Open;
|
||||||
|
import com.gitee.sop.servercommon.bean.OpenContext;
|
||||||
|
import com.gitee.sop.servercommon.bean.ServiceContext;
|
||||||
import com.gitee.sop.servercommon.exception.ServiceException;
|
import com.gitee.sop.servercommon.exception.ServiceException;
|
||||||
import com.gitee.sop.storyweb.controller.param.CategoryParam;
|
import com.gitee.sop.storyweb.controller.param.CategoryParam;
|
||||||
import com.gitee.sop.storyweb.controller.param.LargeTextParam;
|
import com.gitee.sop.storyweb.controller.param.LargeTextParam;
|
||||||
@@ -16,6 +18,7 @@ import io.swagger.annotations.Api;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -81,6 +84,18 @@ public class Example1001_BaseController {
|
|||||||
return story;
|
return story;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Open(value = "story.system.param.get")
|
||||||
|
@GetMapping("/get/system/param/v1")
|
||||||
|
public StoryResult systemParam(StoryParam param) {
|
||||||
|
StoryResult result = new StoryResult();
|
||||||
|
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
|
||||||
|
System.out.println(param == openContext.getBizObject());
|
||||||
|
System.out.println("app_id:" + openContext.getAppId());
|
||||||
|
System.out.println("token:" + openContext.getAppAuthToken());
|
||||||
|
result.setName("系统参数:" + openContext);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// 参数绑定
|
// 参数绑定
|
||||||
@Open(value = "story.param.bind", mergeResult = false)
|
@Open(value = "story.param.bind", mergeResult = false)
|
||||||
@RequestMapping("/get/param/v1")
|
@RequestMapping("/get/param/v1")
|
||||||
|
@@ -55,6 +55,17 @@ public class AllInOneTest extends TestBase {
|
|||||||
client.execute(requestBuilder);
|
client.execute(requestBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSystemParam() {
|
||||||
|
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||||
|
.method("story.system.param.get")
|
||||||
|
.version("1.0")
|
||||||
|
.appAuthToken("123123")
|
||||||
|
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
|
||||||
|
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||||
|
|
||||||
|
client.execute(requestBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以json方式提交(application/json)
|
* 以json方式提交(application/json)
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user