17
17
import java .util .ArrayList ;
18
18
import java .util .Arrays ;
19
19
import java .util .Collection ;
20
+ import java .util .List ;
20
21
21
22
import javax .servlet .http .HttpSession ;
22
23
30
31
import apijson .framework .APIJSONFunctionParser ;
31
32
import apijson .orm .AbstractVerifier ;
32
33
import apijson .orm .JSONRequest ;
34
+ import apijson .orm .Visitor ;
33
35
34
36
35
37
/**可远程调用的函数类,用于自定义业务逻辑处理
@@ -45,31 +47,51 @@ public DemoFunctionParser() {
45
47
public DemoFunctionParser (RequestMethod method , String tag , int version , JSONObject request , HttpSession session ) {
46
48
super (method , tag , version , request , session );
47
49
}
50
+
51
+ public Visitor <Long > getCurrentUser (@ NotNull JSONObject current ) {
52
+ return DemoVerifier .getVisitor (getSession ());
53
+ }
54
+
55
+ public Long getCurrentUserId (@ NotNull JSONObject current ) {
56
+ return DemoVerifier .getVisitorId (getSession ());
57
+ }
58
+
59
+ public List <Long > getCurrentUserIdAsList (@ NotNull JSONObject current ) {
60
+ return Arrays .asList (DemoVerifier .getVisitorId (getSession ()));
61
+ }
62
+
63
+ public List <Long > getCurrentContactIdList (@ NotNull JSONObject current ) {
64
+ Visitor <Long > user = getCurrentUser (current );
65
+ return user == null ? null : user .getContactIdList ();
66
+ }
67
+
48
68
49
69
/**
50
70
* @param current
51
71
* @param idList
52
72
* @return
53
73
* @throws Exception
54
74
*/
55
- public Object verifyIdList (@ NotNull JSONObject current , @ NotNull String idList ) throws Exception {
75
+ public void verifyIdList (@ NotNull JSONObject current , @ NotNull String idList ) throws Exception {
56
76
Object obj = current .get (idList );
57
77
if (obj == null ) {
58
- return null ;
78
+ return ;
59
79
}
60
80
61
81
if (obj instanceof Collection == false ) {
62
- throw new IllegalArgumentException (idList + " 不符合 Array 类型 ! 结构必须是 [] !" );
82
+ throw new IllegalArgumentException (idList + " 不符合 Array 数组类型 ! 结构必须是 [] !" );
63
83
}
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 类型!" );
84
+
85
+ Collection <?> collection = (Collection <?>) obj ;
86
+ if (collection != null ) {
87
+ int i = -1 ;
88
+ for (Object item : collection ) {
89
+ i ++;
90
+ if (item instanceof Long == false && item instanceof Integer == false ) {
91
+ throw new IllegalArgumentException (idList + "/" + i + ": " + item + " 不符合 Long 数字类型!" );
69
92
}
70
93
}
71
94
}
72
- return null ;
73
95
}
74
96
75
97
@@ -79,24 +101,26 @@ public Object verifyIdList(@NotNull JSONObject current, @NotNull String idList)
79
101
* @return
80
102
* @throws Exception
81
103
*/
82
- public Object verifyURLList (@ NotNull JSONObject current , @ NotNull String urlList ) throws Exception {
104
+ public void verifyURLList (@ NotNull JSONObject current , @ NotNull String urlList ) throws Exception {
83
105
Object obj = current .get (urlList );
84
106
if (obj == null ) {
85
- return null ;
107
+ return ;
86
108
}
87
109
88
110
if (obj instanceof Collection == false ) {
89
- throw new IllegalArgumentException (urlList + " 不符合 Array 类型 ! 结构必须是 [] !" );
111
+ throw new IllegalArgumentException (urlList + " 不符合 Array 数组类型 ! 结构必须是 [] !" );
90
112
}
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 格式!" );
113
+
114
+ Collection <?> collection = (Collection <?>) obj ;
115
+ if (collection != null ) {
116
+ int i = -1 ;
117
+ for (Object item : collection ) {
118
+ i ++;
119
+ if (item instanceof String == false || StringUtil .isUrl ((String ) item ) == false ) {
120
+ throw new IllegalArgumentException (urlList + "/" + i + ": " + item + " 不符合 URL 字符串格式!" );
96
121
}
97
122
}
98
123
}
99
- return null ;
100
124
}
101
125
102
126
@@ -211,6 +235,7 @@ public JSONArray getIdList(@NotNull JSONObject current) {
211
235
* @return
212
236
* @throws Exception
213
237
*/
238
+ @ Override
214
239
public Object verifyAccess (@ NotNull JSONObject current ) throws Exception {
215
240
long userId = current .getLongValue (JSONRequest .KEY_USER_ID );
216
241
String role = current .getString (JSONRequest .KEY_ROLE );
0 commit comments