Skip to content

Commit a2d4673

Browse files
committed
APIAuto: 参数注入 RANDOM_DB, ORDER_DB 支持传自定义请求体 body,以及表名扩展为 schema.table 方便传自定义数据库名
1 parent 243a51f commit a2d4673

4 files changed

Lines changed: 45 additions & 22 deletions

File tree

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson/CodeUtil.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6783,11 +6783,12 @@ res_data = rep.json()
67836783
var typeOfValue = CodeUtil.getType4Request(value);
67846784
var isValueNotArray = typeOfValue != 'array';
67856785
var isValueNotObject = typeOfValue != 'object';
6786-
6786+
var lastKey = pathKeys == null ? null : pathKeys[pathKeys.length - 1];
6787+
67876788
if (standardObj != null) {
67886789
var parentObj = pathKeys == null || pathKeys.length <= 0 ? null : JSONResponse.getStandardByPath(standardObj, pathKeys.slice(0, pathKeys.length - 1));
67896790
var targetValues = parentObj == null ? null : parentObj.values;
6790-
var lastKey = pathKeys == null ? null : pathKeys[pathKeys.length - 1]
6791+
67916792
var targetObj = targetValues == null ? null : (targetValues[0] || {})[lastKey] || {}; // JSONResponse.getStandardByPath(standardObj, pathKeys);
67926793

67936794
var t = targetObj == null ? null : targetObj.type;

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson/StringUtil.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var StringUtil = {
2424
* @return
2525
*/
2626
get: function(s) {
27-
return s == null ? '' : (JSONResponse.isString(s) ? s : JSON.stringify(s));
27+
return s == null ? '' : (typeof s == 'string' ? s : JSON.stringify(s));
2828
},
2929

3030
/**获取去掉前后空格后的string,为null则返回''
@@ -40,7 +40,7 @@ var StringUtil = {
4040
* @return
4141
*/
4242
noBlank: function(s) {
43-
return s == null ? '' : s.replace(/ /g, '');
43+
return StringUtil.get(s).replace(/ /g, '');
4444
},
4545

4646
/**判断字符是否为空

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,9 @@
806806
<use xlink:href="svg/icon.svg#close"></use>
807807
</svg>
808808

809-
<input type="text" placeholder="请输入" v-model="history.name" />
810-
<button @click="save()" >保存</button>
811-
</div>
809+
<input type="text" placeholder="请输入名称" v-model="history.name" />
810+
<button @click="save()" >保存</button>
811+
</div>
812812

813813
<!--弹出下载文本框-->
814814
<div class="save-box" v-show="isExportShow">

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/js/main.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5704,7 +5704,7 @@ https://github.com/Tencent/APIJSON/issues
57045704
if (show) {
57055705
var testCases = this.testCases
57065706
var allCount = testCases == null ? 0 : testCases.length
5707-
App.allCount = allCount
5707+
this.allCount = allCount
57085708
if (allCount > 0) {
57095709
if (! (this.isAllSummaryShow() || this.isCurrentSummaryShow())) {
57105710
this.showCompare4TestCaseList(show, this.isChainShow)
@@ -9033,7 +9033,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
90339033
'detail()': 'getFunctionDetail()',
90349034
'name$': search,
90359035
'detail$': search,
9036-
'@combine': search == null ? null : 'name$,detail$',
9036+
'@combine': StringUtil.isEmpty(search) ? null : 'name$,detail$',
90379037
}
90389038
},
90399039
'Request[]': {
@@ -10897,7 +10897,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1089710897
const start = value.indexOf('(');
1089810898
const end = value.lastIndexOf(')');
1089910899

10900-
var request4Db = function(tableName, which, p_k, pathKeys, key, lastKeyInPath, isRandom, isDesc, step) {
10900+
var request4Db = function(tableName, which, p_k, pathKeys, key, lastKeyInPath, isRandom, isDesc, step, body) {
1090110901
// const tableName = JSONResponse.getTableName(pathKeys[pathKeys.length - 2]);
1090210902
vOutput.value = 'requesting value for ' + tableName + '/' + key + ' from database...';
1090310903

@@ -10917,19 +10917,32 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1091710917
column = column.substring(1, column.length - 1);
1091810918
}
1091910919

10920-
const finalTableName = StringUtil.isEmpty(table, true) ? tableName : table;
10920+
var sch_tbl = StringUtil.isEmpty(table, true) ? tableName : table;
10921+
var ind = sch_tbl.indexOf('.');
10922+
var schema = ind < 0 ? App.schema : sch_tbl.substring(0, ind);
10923+
const finalTableName = StringUtil.firstCase(ind < 0 ? sch_tbl : sch_tbl.substring(ind + 1), true);
1092110924
const finalColumnName = StringUtil.isEmpty(column, true) ? lastKeyInPath : column;
1092210925

10923-
const tableReq = {
10926+
var tableReq = {
1092410927
'@column': isRandom ? finalColumnName : ('DISTINCT ' + finalColumnName),
10925-
'@order': isRandom ? 'rand()' : (finalColumnName + (isDesc ? '-' : '+'))
10928+
'@order': isRandom ? 'rand()' : (finalColumnName + (isDesc ? '-' : '+')),
10929+
'@schema': StringUtil.isEmpty(schema, true) ? undefined : schema
1092610930
};
1092710931
tableReq[finalColumnName + '>='] = min;
1092810932
tableReq[finalColumnName + '<='] = max;
10933+
try {
10934+
body = parseJSON(body);
10935+
} catch (e) {
10936+
console.log(e);
10937+
}
1092910938

10930-
const req = {};
10939+
var req = {};
1093110940
const listName = isRandom ? null : finalTableName + '-' + finalColumnName + '[]';
10932-
const orderIndex = isRandom ? null : getOrderIndex(randomId, line, null)
10941+
const orderIndex = isRandom ? null : getOrderIndex(randomId, line, null);
10942+
10943+
if (body != null && body[finalTableName] == null && (isRandom || body[listName] == null)) {
10944+
tableReq = Object.assign(tableReq, body);
10945+
}
1093310946

1093410947
if (isRandom) {
1093510948
req[finalTableName] = tableReq;
@@ -10941,11 +10954,19 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1094110954
page: (step*orderIndex) % 100 //暂时先这样,APIJSON 应该改为 count*page <= 10000 //FIXME 上限 100 怎么破,lastKeyInPath 未必是 id
1094210955
};
1094310956
listReq[finalTableName] = tableReq;
10957+
10958+
if (body != null && body[finalTableName] != null) {
10959+
listReq = Object.assign(listReq, body);
10960+
}
1094410961
req[listName] = listReq;
1094510962
}
1094610963

10964+
if (body != null && body[isRandom ? finalTableName : listName] != null) {
10965+
req = Object.assign(req, body);
10966+
}
10967+
1094710968
// reqCount ++;
10948-
App.request(true, REQUEST_TYPE_POST, REQUEST_TYPE_JSON, '/get', req, {}, function (url, res, err) {
10969+
App.adminRequest('/get', req, {}, function (url, res, err) {
1094910970
// respCount ++;
1095010971
try {
1095110972
App.onResponse(url, res, err)
@@ -10968,7 +10989,7 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1096810989
//越界,重新获取
1096910990
if (val == null && orderIndex > 0 && ORDER_MAP[randomId] != null && ORDER_MAP[randomId][line] != null) {
1097010991
ORDER_MAP[randomId][line] = null; //重置,避免还是在原来基础上叠加
10971-
request4Db(JSONResponse.getTableName(pathKeys[pathKeys.length - 2]), which, p_k, pathKeys, key, lastKeyInPath, false, isDesc, step);
10992+
request4Db(JSONResponse.getTableName(pathKeys[pathKeys.length - 2]), which, p_k, pathKeys, key, lastKeyInPath, false, isDesc, step, body);
1097210993
}
1097310994
else {
1097410995
invoke(val, which, p_k, pathKeys, key, lastKeyInPath);
@@ -11025,13 +11046,13 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1102511046
+ '\n+,-,step 前后都不能有空格等其它字符!');
1102611047
}
1102711048

11049+
var args = StringUtil.split(value.substring(start + 1, end), ', ') || []
11050+
1102811051
if (fun == ORDER_DB) {
11029-
request4Db(JSONResponse.getTableName(pathKeys[pathKeys.length - 2]), which, p_k, pathKeys, key, lastKeyInPath, false, isDesc, step); //request4Db(key + (isDesc ? '-' : '+'), step);
11052+
request4Db(JSONResponse.getTableName(pathKeys[pathKeys.length - 2]), which, p_k, pathKeys, key, lastKeyInPath, false, isDesc, step, args[4]); //request4Db(key + (isDesc ? '-' : '+'), step);
1103011053
continue;
1103111054
}
1103211055

11033-
var args = StringUtil.split(value.substring(start + 1, end))
11034-
1103511056
toEval = (fun == ORDER_IN ? 'orderIn' : (fun == ORDER_INT ? 'orderInt' : (fun == ORDER_BAD_BOOL ? 'orderBadBool' : (fun == ORDER_BAD_NUM
1103611057
? 'orderBadNum' : (fun == ORDER_BAD_STR ? 'orderBadStr' : (fun == ORDER_BAD_ARR ? 'orderBadArr' : (fun == ORDER_BAD_OBJ ? 'orderBadObj' : 'orderBad')))))))
1103711058
+ '(' + (fun == ORDER_BAD ? 'BADS, ' : '') + isDesc + ', ' + getOrderIndex(
@@ -11106,8 +11127,9 @@ Content-Type: ` + contentType) + (StringUtil.isEmpty(headerStr, true) ? '' : hea
1110611127
else {
1110711128
fun = funWithOrder; //还原,其它函数不支持 升降序和跨步!
1110811129

11109-
if (fun == RANDOM_DB) {
11110-
request4Db(JSONResponse.getTableName(pathKeys[pathKeys.length - 2]), which, p_k, pathKeys, key, lastKeyInPath, true); //'random()');
11130+
if (fun == RANDOM_DB) { // FIXME 改用 parseJSON('[' + value.substring(start + 1, end) + ']') 兼容复杂表达式
11131+
var args = StringUtil.split(value.substring(start + 1, end), ', ') || []
11132+
request4Db(JSONResponse.getTableName(pathKeys[pathKeys.length - 2]), which, p_k, pathKeys, key, lastKeyInPath, true, null, null, args[4]); //'random()');
1111111133
continue;
1111211134
}
1111311135

0 commit comments

Comments
 (0)