forked from APIJSON/APIJSON-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoFunctionParser.java
More file actions
93 lines (79 loc) · 2.47 KB
/
DemoFunctionParser.java
File metadata and controls
93 lines (79 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package apijson.demo;
import javax.servlet.http.HttpSession;
import com.alibaba.fastjson.JSONObject;
import apijson.NotNull;
import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.framework.APIJSONFunctionParser;
import apijson.framework.APIJSONVerifier;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DemoFunctionParser extends APIJSONFunctionParser {
public DemoFunctionParser() {
this(null, null, 0, null, null);
}
// 展示在远程函数内部可以用 this 拿到的东西
public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) {
super(method, tag, version, request, session);
}
/***
* 获取当前用户id
*
* @param current
* @return
*/
public String getCurrentUserId(@NotNull JSONObject current) {
if (this.getSession() == null) {
return "test"; // 启动时的自动测试
}
return APIJSONVerifier.getVisitorId(getSession());
}
/**
* 一个最简单的远程函数示例,返回一个前面拼接了 Hello 的字符串
*
* @param current
* @param name
* @return
* @throws Exception
*/
public String sayHello(@NotNull JSONObject current, @NotNull String name) throws Exception {
// 注意这里参数 name 是 key,不是 value
Object obj = current.get(name);
if (this.getSession() == null) {
return "test"; // 启动时的自动测试
}
if (obj == null) {
throw new IllegalArgumentException();
}
if (!(obj instanceof String)) {
throw new IllegalArgumentException();
}
// 之后可以用 this.getSession 拿到当前的 HttpSession
return "Hello, " + obj.toString();
}
/***
* 密码加密
*
* @param current
* @param id 添加id生成
* @param password 密码字段名
* @return
* @throws Exception
*/
public void pwdEncrypt(@NotNull JSONObject current, @NotNull String id, @NotNull String password)
throws Exception {
String c_password = current.getString(password);
current.put(password, c_password + "_" + System.currentTimeMillis());
}
public void childFunTest(@NotNull JSONObject current, @NotNull String addr) throws Exception {
String c_addr = current.getString(addr);
current.put(addr, c_addr + "_" + System.currentTimeMillis());
}
public void removeKeys(@NotNull JSONObject current, String keys) {
String[] ks = StringUtil.split(keys, ";"); // 用分号 ; 分割
// 根据 ks remove 掉 current 里的字段
for (int i = 0; i < ks.length; i++) {
current.remove(ks[i]);
}
}
}