2121
2222import javax .servlet .http .HttpSession ;
2323
24- import com .alibaba .fastjson .JSONArray ;
25- import com .alibaba .fastjson .JSONObject ;
26-
2724import apijson .JSONResponse ;
2825import apijson .NotNull ;
2926import apijson .RequestMethod ;
3027import apijson .StringUtil ;
28+ import com .alibaba .fastjson .JSONArray ;
29+ import com .alibaba .fastjson .JSONObject ;
30+
3131import apijson .framework .javax .APIJSONFunctionParser ;
3232import apijson .orm .AbstractVerifier ;
33- import apijson .orm .JSONRequest ;
3433import apijson .orm .Visitor ;
3534
35+ import static apijson .JSONObject .KEY_COLUMN ;
36+ import static apijson .JSONRequest .KEY_COUNT ;
37+
3638
3739/**可远程调用的函数类,用于自定义业务逻辑处理
3840 * 具体见 https://github.com/Tencent/APIJSON/issues/101
3941 * @author Lemon
4042 */
41- public class DemoFunctionParser extends APIJSONFunctionParser {
43+ public class DemoFunctionParser extends APIJSONFunctionParser < Long , JSONObject , JSONArray > {
4244 public static final String TAG = "DemoFunctionParser" ;
4345
4446 public DemoFunctionParser () {
@@ -48,34 +50,34 @@ public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObj
4850 super (method , tag , version , request , session );
4951 }
5052
51- public Visitor <Long > getCurrentUser (@ NotNull JSONObject current ) {
53+ public Visitor <Long > getCurrentUser (@ NotNull JSONObject curObj ) {
5254 return DemoVerifier .getVisitor (getSession ());
5355 }
5456
55- public Long getCurrentUserId (@ NotNull JSONObject current ) {
57+ public Long getCurrentUserId (@ NotNull JSONObject curObj ) {
5658 return DemoVerifier .getVisitorId (getSession ());
5759 }
5860
59- public List <Long > getCurrentUserIdAsList (@ NotNull JSONObject current ) {
61+ public List <Long > getCurrentUserIdAsList (@ NotNull JSONObject curObj ) {
6062 List <Long > list = new ArrayList <>(1 );
6163 list .add (DemoVerifier .getVisitorId (getSession ()));
6264 return list ;
6365 }
6466
65- public List <Long > getCurrentContactIdList (@ NotNull JSONObject current ) {
66- Visitor <Long > user = getCurrentUser (current );
67+ public List <Long > getCurrentContactIdList (@ NotNull JSONObject curObj ) {
68+ Visitor <Long > user = getCurrentUser (curObj );
6769 return user == null ? null : user .getContactIdList ();
6870 }
6971
7072
7173 /**
72- * @param current
74+ * @param curObj
7375 * @param idList
7476 * @return
7577 * @throws Exception
7678 */
77- public void verifyIdList (@ NotNull JSONObject current , @ NotNull String idList ) throws Exception {
78- Object obj = current .get (idList );
79+ public void verifyIdList (@ NotNull JSONObject curObj , @ NotNull String idList ) throws Exception {
80+ Object obj = curObj .get (idList );
7981 if (obj == null ) {
8082 return ;
8183 }
@@ -98,13 +100,13 @@ public void verifyIdList(@NotNull JSONObject current, @NotNull String idList) th
98100
99101
100102 /**
101- * @param current
103+ * @param curObj
102104 * @param urlList
103105 * @return
104106 * @throws Exception
105107 */
106- public void verifyURLList (@ NotNull JSONObject current , @ NotNull String urlList ) throws Exception {
107- Object obj = current .get (urlList );
108+ public void verifyURLList (@ NotNull JSONObject curObj , @ NotNull String urlList ) throws Exception {
109+ Object obj = curObj .get (urlList );
108110 if (obj == null ) {
109111 return ;
110112 }
@@ -127,21 +129,21 @@ public void verifyURLList(@NotNull JSONObject current, @NotNull String urlList)
127129
128130
129131 /**
130- * @param current
132+ * @param curObj
131133 * @param momentId
132134 * @return
133135 * @throws Exception
134136 */
135- public int deleteCommentOfMoment (@ NotNull JSONObject current , @ NotNull String momentId ) throws Exception {
136- long mid = current .getLongValue (momentId );
137- if (mid <= 0 || current .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
137+ public int deleteCommentOfMoment (@ NotNull JSONObject curObj , @ NotNull String momentId ) throws Exception {
138+ long mid = curObj .getLongValue (momentId );
139+ if (mid <= 0 || curObj .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
138140 return 0 ;
139141 }
140142
141- JSONRequest request = new JSONRequest ();
143+ JSONObject request = JSON . createJSONObject ();
142144
143145 //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
144- JSONRequest comment = new JSONRequest ();
146+ JSONObject comment = JSON . createJSONObject ();
145147 comment .put ("momentId" , mid );
146148
147149 request .put ("Comment" , comment );
@@ -155,22 +157,22 @@ public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String mo
155157
156158
157159 /**删除评论的子评论
158- * @param current
160+ * @param curObj
159161 * @param toId
160162 * @return
161163 */
162- public int deleteChildComment (@ NotNull JSONObject current , @ NotNull String toId ) throws Exception {
163- long tid = current .getLongValue (toId );
164- if (tid <= 0 || current .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
164+ public int deleteChildComment (@ NotNull JSONObject curObj , @ NotNull String toId ) throws Exception {
165+ long tid = curObj .getLongValue (toId );
166+ if (tid <= 0 || curObj .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
165167 return 0 ;
166168 }
167169
168170 //递归获取到全部子评论id
169171
170- JSONRequest request = new JSONRequest ();
172+ JSONObject request = JSON . createJSONObject ();
171173
172174 //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
173- JSONRequest comment = new JSONRequest ();
175+ JSONObject comment = JSON . createJSONObject ();
174176 comment .put ("id{}" , getChildCommentIdList (tid ));
175177
176178 request .put ("Comment" , comment );
@@ -187,19 +189,20 @@ private JSONArray getChildCommentIdList(long tid) {
187189
188190 JSONArray arr = new JSONArray ();
189191
190- JSONRequest request = new JSONRequest ();
192+ JSONObject request = JSON . createJSONObject ();
191193
192194 //Comment-id[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
193- JSONRequest idItem = new JSONRequest ();
195+ JSONObject idItem = JSON . createJSONObject ();
194196
195197 //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
196- JSONRequest comment = new JSONRequest ();
198+ JSONObject comment = JSON . createJSONObject ();
197199 comment .put ("toId" , tid );
198- comment .setColumn ( "id" );
200+ comment .put ( KEY_COLUMN , "id" );
199201 idItem .put ("Comment" , comment );
202+ idItem .put (KEY_COUNT , 0 );
200203 //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
201204
202- request .putAll ( idItem . toArray ( 0 , 0 , "Comment-id" ) );
205+ request .put ( "Comment-id[]" , idItem );
203206 //Comment-id[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
204207
205208 JSONObject rp = new DemoParser ().setNeedVerify (false ).parseResponse (request );
@@ -223,23 +226,23 @@ private JSONArray getChildCommentIdList(long tid) {
223226
224227
225228 /**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
226- * @param current
229+ * @param curObj
227230 * @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
228231 * @throws Exception
229232 */
230- public JSONArray getIdList (@ NotNull JSONObject current ) {
233+ public JSONArray getIdList (@ NotNull JSONObject curObj ) {
231234 return new JSONArray (new ArrayList <Object >(Arrays .asList (12 , 15 , 301 , 82001 , 82002 , 38710 )));
232235 }
233236
234237
235238 /**TODO 仅用来测试 "key-()":"verifyAccess()"
236- * @param current
239+ * @param curObj
237240 * @return
238241 * @throws Exception
239242 */
240- public Object verifyAccess (@ NotNull JSONObject current ) throws Exception {
241- long userId = current .getLongValue (JSONRequest .KEY_USER_ID );
242- String role = current .getString (JSONRequest .KEY_ROLE );
243+ public Object verifyAccess (@ NotNull JSONObject curObj ) throws Exception {
244+ long userId = curObj .getLongValue (apijson . JSONObject .KEY_USER_ID );
245+ String role = curObj .getString (apijson . JSONObject .KEY_ROLE );
243246 if (AbstractVerifier .OWNER .equals (role ) && userId != (Long ) DemoVerifier .getVisitorId (getSession ())) {
244247 throw new IllegalAccessException ("登录用户与角色OWNER不匹配!" );
245248 }
0 commit comments