Browse Source

用户相关的API

remotes/1611766341912730171/master
richie 5 years ago
parent
commit
ac58649a40
  1. 1
      readme.md
  2. 11
      src/main/java/com/fanruan/api/decision/AuthorityKit.java
  3. 45
      src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java
  4. 73
      src/main/java/com/fanruan/api/decision/user/OpenUser.java
  5. 35
      src/main/java/com/fanruan/api/decision/user/UserKit.java
  6. 13
      src/main/java/com/fanruan/api/i18n/I18nKit.java
  7. 37
      src/main/java/com/fanruan/api/net/NetworkKit.java
  8. 53
      src/main/java/com/fanruan/api/web/FlushKit.java
  9. 64
      src/main/java/com/fanruan/api/web/ResponseKit.java
  10. 21
      src/main/java/com/fanruan/api/web/TextGeneratorKit.java

1
readme.md

@ -54,6 +54,7 @@ SessionKit#getSession(@NotNull String sessionID);
|网页框插件|https://git.fanruan.com/fanruan/demo-widget-iframe|
|时钟控件|https://git.fanruan.com/fanruan/demo-show-clock|
|ldaps认证插件|https://git.fanruan.com/fanruan/demo-ldaps-passport|
|个人国际化设置插件|https://git.fanruan.com/fanruan/decision-user-language|
## 如何判断插件中调用的API需要增加到FineKit中

11
src/main/java/com/fanruan/api/decision/AuthorityKit.java

@ -0,0 +1,11 @@
package com.fanruan.api.decision;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-09-19
* 权限相关的工具类
*/
public class AuthorityKit {
}

45
src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java

@ -1,45 +0,0 @@
package com.fanruan.api.decision.middle;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-09-19
*/
public class ReadOnlyUser {
/**
* 非空项无默认值
*/
private String userName = null;
/**
* 非空项无默认值
*/
private String password = null;
/**
* 姓名
*/
private String realName = null;
public static ReadOnlyUser build(String userName, String password, String realName) {
return new ReadOnlyUser(userName, password, realName);
}
private ReadOnlyUser(String userName, String password, String realName) {
this.userName = userName;
this.password = password;
this.realName = realName;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public String getRealName() {
return realName;
}
}

73
src/main/java/com/fanruan/api/decision/user/OpenUser.java

@ -0,0 +1,73 @@
package com.fanruan.api.decision.user;
import com.fr.decision.authority.data.User;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-09-19
*/
public class OpenUser {
private User user;
static OpenUser wrap(User user) {
return new OpenUser(user);
}
public OpenUser create(String id) {
return new OpenUser(new User().id(id));
}
private OpenUser(User user) {
this.user = user;
}
User select() {
return user;
}
public String getUserName() {
return user.getUserName();
}
public String getPassword() {
return user.getPassword();
}
public void setPassword(String password) {
this.user.setPassword(password);
}
public String getRealName() {
return user.getRealName();
}
public void setRealName(String realName) {
this.user.setRealName(realName);
}
public String getLanguage() {
return user.getLanguage();
}
public void setLanguage(String language) {
this.user.setLanguage(language);
}
public String getEmail() {
return user.getEmail();
}
public void setEmail(String email) {
this.user.setEmail(email);
}
public String getMobile() {
return user.getMobile();
}
public void setMobile(String mobile) {
this.user.setMobile(mobile);
}
}

35
src/main/java/com/fanruan/api/decision/UserKit.java → src/main/java/com/fanruan/api/decision/user/UserKit.java

@ -1,11 +1,13 @@
package com.fanruan.api.decision;
package com.fanruan.api.decision.user;
import com.fanruan.api.decision.middle.ReadOnlyUser;
import com.fr.decision.authority.AuthorityContext;
import com.fr.decision.authority.data.User;
import com.fr.decision.webservice.utils.UserSourceFactory;
import com.fr.decision.webservice.utils.user.source.UserSource;
import com.fr.decision.webservice.v10.user.UserService;
import javax.servlet.http.HttpServletRequest;
/**
* @author richie
* @version 10.0
@ -25,6 +27,11 @@ public class UserKit {
return UserSourceFactory.getInstance().getUserSource(user);
}
public static OpenUser getUserByRequest(HttpServletRequest req) throws Exception {
User user = UserService.getInstance().getUserByRequest(req);
return OpenUser.wrap(user);
}
/**
* 根据用户名获取用户信息
*
@ -32,8 +39,28 @@ public class UserKit {
* @return 用户信息
* @throws Exception 如果获取用户出错则抛出此异常
*/
public static ReadOnlyUser getReadOnlyUser(String username) throws Exception {
public static OpenUser getUser(String username) throws Exception {
User user = UserService.getInstance().getUserByUserName(username);
return ReadOnlyUser.build(user.getUserName(), user.getPassword(), user.getRealName());
return OpenUser.wrap(user);
}
/**
* 更新一个用户信息
*
* @param user 用户
* @throws Exception 更新用户信息失败则抛出此异常
*/
public static void update(OpenUser user) throws Exception {
AuthorityContext.getInstance().getUserController().update(user.select());
}
/**
* 添加一个用户
*
* @param user 用户
* @throws Exception 添加用户失败则抛出此异常
*/
public static void add(OpenUser user) throws Exception {
AuthorityContext.getInstance().getUserController().update(user.select());
}
}

13
src/main/java/com/fanruan/api/i18n/I18nKit.java

@ -2,6 +2,9 @@ package com.fanruan.api.i18n;
import com.fr.locale.InterProviderFactory;
import java.util.Locale;
import java.util.Map;
public class I18nKit {
/**
* 获取国际化文本
@ -23,4 +26,14 @@ public class I18nKit {
public static String getLocText(String string, String... args) {
return InterProviderFactory.getProvider().getLocText(string, args);
}
/**
* 获取某种语言下所有的国际化键值对
*
* @param locale 国际化
* @return 国际化键值对集合
*/
public static Map<String, String> getEntireKV(Locale locale) {
return InterProviderFactory.getClientProvider().getEntireKV(locale);
}
}

37
src/main/java/com/fanruan/api/net/NetworkKit.java

@ -2,12 +2,14 @@ package com.fanruan.api.net;
import com.fr.base.ServerConfig;
import com.fr.data.NetworkHelper;
import com.fr.decision.webservice.utils.WebServiceUtils;
import org.jetbrains.annotations.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Map;
/**
@ -21,7 +23,7 @@ public class NetworkKit {
/**
* 生成一个打印输出器
*
* @param res HTTP响应
* @param res http响应
* @return 打印输出器
* @throws java.io.IOException 如果无法创建输出器则抛出此异常
*/
@ -32,7 +34,7 @@ public class NetworkKit {
/**
* 生成一个打印输出器
*
* @param res HTTP响应
* @param res http响应
* @param charsetName 编码
* @return 打印输出器
* @throws java.io.IOException 如果无法创建输出器则抛出此异常
@ -45,7 +47,7 @@ public class NetworkKit {
* 写出指定的模板
*
* @param resource 模板路径
* @param response HTTP响应
* @param response http响应
* @param map 用于替换模板中参数的的参数集
* @throws java.io.IOException 如果无法创建输出器则抛出此异常
*/
@ -56,7 +58,7 @@ public class NetworkKit {
/**
* 把HTTP请求中指定名字的参数值转化为整数参数为空或不是整数则返回-1
*
* @param req HTTP请求
* @param req http请求
* @param paraName 参数名
* @return 整型参数值
*/
@ -67,7 +69,7 @@ public class NetworkKit {
/**
* 把HTTP请求中指定名字的参数值转化为整数
*
* @param req HTTP请求
* @param req http请求
* @param paraName 参数名
* @param defaultValue 默认值
* @return 返回req中参数的整数值参数为空或不是整数则返回defaultValue
@ -79,7 +81,7 @@ public class NetworkKit {
/**
* 从http请求中获取sessionID
*
* @param req HTTP请求
* @param req http请求
* @return session编号
*/
public static @Nullable String getHTTPRequestSessionIDParameter(HttpServletRequest req) {
@ -89,7 +91,7 @@ public class NetworkKit {
/**
* 把HTTP请求中指定名字的参数值转化为布尔值
*
* @param req HTTP请求
* @param req http请求
* @param paraName 参数名
* @return 布尔类型的参数值
*/
@ -100,7 +102,7 @@ public class NetworkKit {
/**
* 获取HTTP请求中指定名字的参数值
*
* @param req HTTP请求
* @param req http请求
* @param paraName 参数名
* @return 字符型参数值
*/
@ -111,7 +113,7 @@ public class NetworkKit {
/**
* 获取第一个不为空的参数
*
* @param req HTTP请求
* @param req http请求
* @param paraNames 参数列表
* @return 字符型参数值
*/
@ -119,8 +121,23 @@ public class NetworkKit {
return NetworkHelper.getHTTPRequestParameter(req, paraNames);
}
/**
* 生成服务器地址
*
* @param req http请求
* @return 服务器地址
*/
public static String createServletURL(HttpServletRequest req) {
return NetworkHelper.createServletURL(req, ServerConfig.getInstance().getServletName());
}
/**
* 从http请求中获取用户的国际化信息
*
* @param req http请求
* @return 国际化对象
*/
public static Locale getLocale(HttpServletRequest req) {
return WebServiceUtils.getLocale(req);
}
}

53
src/main/java/com/fanruan/api/web/FlushKit.java

@ -0,0 +1,53 @@
package com.fanruan.api.web;
import com.fanruan.api.net.NetworkKit;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-09-19
* 将内容输入到输出流中
*/
public class FlushKit {
/**
* 输出JSON类型的字符串
*
* @param res HTTP响应
* @param jo JSON对象
* @throws Exception 输出出现错误则抛出此异常
*/
public static void printAsJSON(HttpServletResponse res, JSONObject jo) throws Exception {
printAsString(res, jo.toString());
}
/**
* 输出JSON类型的字符串
*
* @param res HTTP响应
* @param ja JSON数组对象
* @throws Exception 输出出现错误则抛出此异常
*/
public static void printAsJSON(HttpServletResponse res, JSONArray ja) throws Exception {
printAsString(res, ja.toString());
}
/**
* 输出字符串一般来说是JSON格式
*
* @param res HTTP响应
* @param jo JSON样式的字符串
* @throws Exception 输出出现错误则抛出此异常
*/
public static void printAsString(HttpServletResponse res, String jo) throws Exception {
PrintWriter pw = NetworkKit.createPrintWriter(res);
pw.print(jo);
pw.flush();
pw.close();
}
}

64
src/main/java/com/fanruan/api/web/ResponseKit.java

@ -0,0 +1,64 @@
package com.fanruan.api.web;
import com.fr.decision.webservice.Response;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-09-19
* 响应工具类
*/
public class ResponseKit {
/**
* 成功的响应,关注结果
*
* @param obj 响应结果
* @return 响应
*/
public static Object ok(Object obj) {
return Response.ok(obj);
}
/**
* 成功的响应,不关注结果
*
* @return 响应
*/
public static Object success() {
return Response.success();
}
/**
* 成功的响应,关注操作成功条目数
*
* @param successNum 操作成功的条目数
* @return 响应
*/
public static Object success(int successNum) {
return Response.success(successNum);
}
/**
* 失败的响应
*
* @param errorCode 错误码
* @param errorMsg 错误信息
* @return 响应
*/
public static Object error(String errorCode, String errorMsg) {
return Response.error(errorCode, errorMsg);
}
/**
* 失败的响应
*
* @param status 状态码
* @param errorCode 错误码
* @param errorMsg 错误信息
* @return 响应
*/
public static Object error(int status, String errorCode, String errorMsg) {
return Response.error(status, errorCode, errorMsg);
}
}

21
src/main/java/com/fanruan/api/web/TextGeneratorKit.java

@ -0,0 +1,21 @@
package com.fanruan.api.web;
import com.fr.decision.web.i18n.I18nTextGenerator;
import com.fr.gen.TextGenerator;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-09-19
*/
public class TextGeneratorKit {
/**
* 创建一个用于国际化的文本输出器
*
* @return 文本输出器
*/
public static TextGenerator newI18nTextGenerator() {
return new I18nTextGenerator();
}
}
Loading…
Cancel
Save