API高级列表
配置过程
1、需要配置查询 URL
2、SQL语句也是必须要的,如果没有,需要自行构建一个临时表,用于字段配置
3、其余配置同普通高级列表
查询接口要求
接口请求方式:POST
平台发起请求时: **Content-Type 为 application/x-www-form-urlencoded**
接口需要支持 jwt 或 iac 校验,平台发起请求时,在请求头上加上 x-auth-token 和 x-iac-token
入参格式
字段 | 类型 | 说明 |
---|---|---|
userQuerySql | String | 查询参数 sql |
userDataSql | String | 数据权限 sql |
orderSql | String | 排序 sql |
pageNum | String | 当前页码 |
pageSize | String | 分页大小 |
//参考样例
@Data
public class ViewQueryReq {
@ApiModelProperty("查询参数 sql")
private String userQuerySql;
@ApiModelProperty("数据权限 sql")
private String userDataSql;
@ApiModelProperty("排序 sql")
private String orderSql;
@ApiModelProperty("分页大小")
private Integer pageSize;
@ApiModelProperty("当前页码")
private Integer pageNum;
}
返参格式
{
"status":"0",
"message":"",
"data":{
"pageNum":1,
"pageSize":20,
"total":1000,
"pages":50,
"list":[
{
}
]
}
}
字段 | 类型 | 说明 |
---|---|---|
total | Long | 总条数 |
pageSize | Integer | 分页大小 |
pageNum | Integer | 当前页码 |
pages | String | 页数 |
list | Object | 返回内容 |
@Data
public class PageResultDTO {
private Long total;
private Integer pageSize;
private Integer pageNum;
private Integer pages;
private Object list;
}
参考代码
package com.cvte.saas.plm.pdm.sy.api.rest.utils;
import com.alibaba.fastjson.JSONObject;
import com.cvte.csb.toolkit.StringUtils;
import com.cvte.csb.web.wrapper.CommRequestWrapper;
import com.google.common.collect.Maps;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class RequestUtils {
/**
* 格式化参数
* @param request
* @param clasz
* @return
* @param <T>
* @throws UnsupportedEncodingException
*/
public static <T> T parserRequestParamsObject(HttpServletRequest request,Class<T> clasz) throws UnsupportedEncodingException {
Map<String,String> paramsMaps = parserRequestParams(request);
return JSONObject.parseObject(JSONObject.toJSONString(paramsMaps),clasz);
}
/**
* 解析 body中的参数
* @param request
* @return
* @throws UnsupportedEncodingException
*/
public static Map<String,String> parserRequestParams(HttpServletRequest request) throws UnsupportedEncodingException {
Map<String, String> parameters = new HashMap<>();
String content = ((CommRequestWrapper)request).getBody();
if(StringUtils.isBlank(content)) {
return Maps.newHashMap();
}
String[] pairs = content.toString().split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
if (idx != -1) {
String name = pair.substring(0, idx);
String value = pair.substring(idx + 1);
parameters.put(decodeParam(name).toUpperCase(), decodeParam(value));
}
}
return parameters;
}
private static String decodeParam(String value) throws UnsupportedEncodingException {
return new String(URLDecoder.decode(URLDecoder.decode(value, "UTF-8"),"UTF-8"));
}
}
package com.cvte.saas.plm.pdm.sy.application.utils;
import com.cvte.csb.toolkit.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelNode;
import org.springframework.expression.spel.ast.PropertyOrFieldReference;
import org.springframework.expression.spel.ast.SpelNodeImpl;
import org.springframework.expression.spel.ast.StringLiteral;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import java.util.*;
public class ViewQueryParseUtils {
public static Map<String,String> parseObject(String sqlParams) {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(sqlParams);
Map<String,String> results = getParameterNames(exp);
return results;
}
private static Map<String,String> getParameterNames(Expression expression) {
List<String> parameterNames = new ArrayList<>();
List<String> values = new ArrayList<>();
SpelNode spelNode = ((SpelExpression)expression).getAST();
visitSpelNode(spelNode, parameterNames);
visitSpelNode2(spelNode, values);
Map<String,String> results = Maps.newHashMap();
for(int i=0;i<parameterNames.size();i++) {
String value = values.get(i);
if(StringUtils.isNotBlank(value)){
value = value.replaceAll("\'","");
}
results.put(parameterNames.get(i),value);
}
return results;
}
private static void visitSpelNode(SpelNode node, List<String> parameterNames) {
if (node instanceof PropertyOrFieldReference) {
PropertyOrFieldReference variableReference = (PropertyOrFieldReference) node;
parameterNames.add(variableReference.toStringAST().replaceAll("#","").toUpperCase());
} else if (node instanceof SpelNodeImpl) {
SpelNodeImpl spelNode = (SpelNodeImpl) node;
for (int i = 0; i < spelNode.getChildCount(); i++) {
visitSpelNode(spelNode.getChild(i), parameterNames);
}
}
}
private static void visitSpelNode2(SpelNode node, List<String> values) {
if (node instanceof StringLiteral) {
StringLiteral variableReference = (StringLiteral) node;
values.add(variableReference.toStringAST().replaceAll("#",""));
} else if (node instanceof SpelNodeImpl) {
SpelNodeImpl spelNode = (SpelNodeImpl) node;
for (int i = 0; i < spelNode.getChildCount(); i++) {
visitSpelNode2(spelNode.getChild(i), values);
}
}
}
public static void main(String[] args) {
String sql = "((column =='ss' and columnC=='aa') or columnB=='cc')";
parseObject(sql);
}
}
踩坑经验(必读)
https://kb.cvte.com/pages/viewpage.action?pageId=423858653
作者:聂维 创建时间:2024-06-13 10:06
最后编辑:柯立明 更新时间:2025-05-12 18:04
最后编辑:柯立明 更新时间:2025-05-12 18:04