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