Skip to content

Commit 5634899

Browse files
committed
Java:MultiDataSource 新增当期用户相关远程函数,优化数组内元素校验相关远程函数
1 parent dac86df commit 5634899

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoFunctionParser.java

+44-18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.ArrayList;
1818
import java.util.Arrays;
1919
import java.util.Collection;
20+
import java.util.Iterator;
21+
import java.util.List;
2022

2123
import javax.servlet.http.HttpSession;
2224

@@ -30,6 +32,7 @@
3032
import apijson.framework.APIJSONFunctionParser;
3133
import apijson.orm.AbstractVerifier;
3234
import apijson.orm.JSONRequest;
35+
import apijson.orm.Visitor;
3336

3437

3538
/**可远程调用的函数类,用于自定义业务逻辑处理
@@ -45,31 +48,51 @@ public DemoFunctionParser() {
4548
public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) {
4649
super(method, tag, version, request, session);
4750
}
51+
52+
public Visitor<Long> getCurrentUser(@NotNull JSONObject current) {
53+
return DemoVerifier.getVisitor(getSession());
54+
}
55+
56+
public Long getCurrentUserId(@NotNull JSONObject current) {
57+
return DemoVerifier.getVisitorId(getSession());
58+
}
59+
60+
public List<Long> getCurrentUserIdAsList(@NotNull JSONObject current) {
61+
return Arrays.asList(DemoVerifier.getVisitorId(getSession()));
62+
}
63+
64+
public List<Long> getCurrentContactIdList(@NotNull JSONObject current) {
65+
Visitor<Long> user = getCurrentUser(current);
66+
return user == null ? null : user.getContactIdList();
67+
}
68+
4869

4970
/**
5071
* @param current
5172
* @param idList
5273
* @return
5374
* @throws Exception
5475
*/
55-
public Object verifyIdList(@NotNull JSONObject current, @NotNull String idList) throws Exception {
76+
public void verifyIdList(@NotNull JSONObject current, @NotNull String idList) throws Exception {
5677
Object obj = current.get(idList);
5778
if (obj == null) {
58-
return null;
79+
return;
5980
}
6081

6182
if (obj instanceof Collection == false) {
62-
throw new IllegalArgumentException(idList + " 不符合 Array 类型! 结构必须是 [] !");
83+
throw new IllegalArgumentException(idList + " 不符合 Array 数组类型! 结构必须是 [] !");
6384
}
64-
JSONArray array = (JSONArray) obj;
65-
if (array != null) {
66-
for (int i = 0; i < array.size(); i++) {
67-
if (array.get(i) instanceof Long == false && array.get(i) instanceof Integer == false) {
68-
throw new IllegalArgumentException(idList + " 内字符 " + array.getString(i) + " 不符合 Long 类型!");
85+
86+
Collection<?> collection = (Collection<?>) obj;
87+
if (collection != null) {
88+
int i = -1;
89+
for (Object item : collection) {
90+
i ++;
91+
if (item instanceof Long == false && item instanceof Integer == false) {
92+
throw new IllegalArgumentException(idList + "/" + i + ": " + item + " 不符合 Long 数字类型!");
6993
}
7094
}
7195
}
72-
return null;
7396
}
7497

7598

@@ -79,24 +102,26 @@ public Object verifyIdList(@NotNull JSONObject current, @NotNull String idList)
79102
* @return
80103
* @throws Exception
81104
*/
82-
public Object verifyURLList(@NotNull JSONObject current, @NotNull String urlList) throws Exception {
105+
public void verifyURLList(@NotNull JSONObject current, @NotNull String urlList) throws Exception {
83106
Object obj = current.get(urlList);
84107
if (obj == null) {
85-
return null;
108+
return;
86109
}
87110

88111
if (obj instanceof Collection == false) {
89-
throw new IllegalArgumentException(urlList + " 不符合 Array 类型! 结构必须是 [] !");
112+
throw new IllegalArgumentException(urlList + " 不符合 Array 数组类型! 结构必须是 [] !");
90113
}
91-
JSONArray array = (JSONArray) obj;
92-
if (array != null) {
93-
for (int i = 0; i < array.size(); i++) {
94-
if (StringUtil.isUrl(array.getString(i)) == false) {
95-
throw new IllegalArgumentException(urlList + " 内字符 " + array.getString(i) + " 不符合 URL 格式!");
114+
115+
Collection<?> collection = (Collection<?>) obj;
116+
if (collection != null) {
117+
int i = -1;
118+
for (Object item : collection) {
119+
i ++;
120+
if (item instanceof String == false || StringUtil.isUrl((String) item) == false) {
121+
throw new IllegalArgumentException(urlList + "/" + i + ": " + item + " 不符合 URL 字符串格式!");
96122
}
97123
}
98124
}
99-
return null;
100125
}
101126

102127

@@ -211,6 +236,7 @@ public JSONArray getIdList(@NotNull JSONObject current) {
211236
* @return
212237
* @throws Exception
213238
*/
239+
@Override
214240
public Object verifyAccess(@NotNull JSONObject current) throws Exception {
215241
long userId = current.getLongValue(JSONRequest.KEY_USER_ID);
216242
String role = current.getString(JSONRequest.KEY_ROLE);

0 commit comments

Comments
 (0)