com.fastchar.extjs.action.ExtDefaultAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastchar-extjs Show documentation
Show all versions of fastchar-extjs Show documentation
FastChar-ExtJs is a Java Web framework that uses extjs libraries.Quickly build a background management system
package com.fastchar.extjs.action;
import com.fastchar.annotation.AFastCache;
import com.fastchar.annotation.AFastRoute;
import com.fastchar.core.*;
import com.fastchar.database.FastDatabases;
import com.fastchar.database.FastPage;
import com.fastchar.database.FastType;
import com.fastchar.database.info.FastColumnInfo;
import com.fastchar.database.info.FastDatabaseInfo;
import com.fastchar.database.info.FastSqlInfo;
import com.fastchar.database.info.FastTableInfo;
import com.fastchar.database.sql.FastSql;
import com.fastchar.extjs.FastExtConfig;
import com.fastchar.extjs.FastExtHelper;
import com.fastchar.extjs.annotation.AFastSession;
import com.fastchar.extjs.core.*;
import com.fastchar.extjs.core.database.FastExtColumnInfo;
import com.fastchar.extjs.core.database.FastSqlTool;
import com.fastchar.extjs.core.enums.FastEnumInfo;
import com.fastchar.extjs.core.heads.FastHeadExtInfo;
import com.fastchar.extjs.core.heads.FastHeadInfo;
import com.fastchar.extjs.core.heads.FastHeadLinkInfo;
import com.fastchar.extjs.core.heads.FastHeadScriptInfo;
import com.fastchar.extjs.core.menus.FastMenuInfo;
import com.fastchar.extjs.entity.ExtBugReportEntity;
import com.fastchar.extjs.entity.ExtManagerEntity;
import com.fastchar.extjs.entity.ExtManagerRoleEntity;
import com.fastchar.extjs.entity.ExtSystemConfigEntity;
import com.fastchar.extjs.interfaces.IFastExtEnum;
import com.fastchar.extjs.interfaces.IFastManagerListener;
import com.fastchar.extjs.utils.ColorUtils;
import com.fastchar.extjs.utils.ZXingUtils;
import com.fastchar.out.FastOutCaptcha;
import com.fastchar.utils.*;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.CentralProcessor.TickType;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.util.Util;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExtDefaultAction extends FastAction {
@Override
protected String getRoute() {
return "/";
}
/**
* 进入系统首页
* 参数:
* 无
*/
@AFastRoute({"/fast_index.html", "/index.html", "/index.jsp", "/index.vm"})
public void index() throws Exception {
if (getUrlParams().size() > 0) {
String firstParams = getUrlParam(0);
if (FastStringUtils.isNotEmpty(firstParams)) {
//苹果Universal Links的验证文件
if (firstParams.equals("apple-app-site-association")) {
responseJson(new File(FastChar.getPath().getWebRootPath(), "apple-app-site-association"));
}
}
}
FastExtHeadHtmlParser headHtmlParser = FastExtHeadHtmlParser.getInstance();
headHtmlParser.initHeadHtml();
//自动移除webroot目录下的auto文件夹
FastExtConfig fastExtConfig = FastChar.getConfig(FastExtConfig.class);
if (fastExtConfig.isRemoveAutoDirectory()) {
FastFileUtils.deleteQuietly(new File(FastChar.getPath().getWebRootPath(), "app/auto"));
}
List heads = headHtmlParser.getHeads();
if (heads == null) {
responseText("未获取到系统fast-head-*.html相关配置文件!");
return;
}
List system = ExtSystemConfigEntity.getInstance().getExtConfigs(-1, "System");
for (ExtSystemConfigEntity extSystemConfigEntity : system) {
FastHeadExtInfo extInfo = FastExtConfig.getInstance().getExtInfo(extSystemConfigEntity.getConfigKey());
if (extInfo != null) {
extInfo.setValue(extSystemConfigEntity.getConfigValue());
}
}
FastHeadExtInfo version = FastExtConfig.getInstance().getExtInfo("version");
if (version != null && FastChar.getConstant().isDebug()) {
version.setValue(String.valueOf(System.currentTimeMillis()));
}
Map extInfoToMap = FastExtConfig.getInstance().getExtInfoToMap();
StringBuilder headString = new StringBuilder();
for (FastHeadInfo head : heads) {
if (head.isWriteHtml()) {
headString.append(FastExtConfig.replacePlaceholder(extInfoToMap, head.getText())).append("\n");
}
}
Map holders = new HashMap<>(extInfoToMap);
holders.put("head", headString.toString());
holders.put("http", getProjectHost());
holders.put("version", version);
holders.put("power", getParamToBoolean("power"));
holders.put("indexUrl", getRequest().getRequestURL().toString());
holders.put("main", getParam("main", ""));
holders.put("role", getParam("role", "manager"));
Map allParam = getParamToMap();
holders.put("params", FastChar.getJson().toJson(allParam));
FastHeadExtInfo extInfo = FastExtConfig.getInstance().getExtInfo("theme-color");
if (extInfo != null) {
holders.put("color", extInfo.getColorValue());
} else {
holders.put("color", FastExtConfig.getInstance().getDefaultThemeColor());
}
FastHeadExtInfo fontSize = FastExtConfig.getInstance().getExtInfo("font-size");
if (fontSize != null) {
holders.put("fontSize", fontSize.getValue());
} else {
holders.put("fontSize", "14px");
}
String indexHtml = FastFileUtils.readFileToString(new File(FastChar.getPath().getWebRootPath(), "fast-index.html"), "utf-8");
if (FastStringUtils.isEmpty(indexHtml)) {
response404("系统fast-index.html文件读取异常!请及时告知开发人员!");
}
indexHtml = FastExtConfig.replacePlaceholder(holders, indexHtml);
responseHtml(indexHtml);
}
/**
* 获取系统加载的文件
* 参数:
* 无
*/
public void loadApp() {
FastExtHeadHtmlParser fastHeadHtmlObserver = FastExtHeadHtmlParser.getInstance();
List heads = fastHeadHtmlObserver.getHeads();
String main = getParam("main");
String role = getParam("role", "manager");
String errorMessage = null;
FastHeadExtInfo mainHeadInfo = FastExtConfig.getInstance().getExtInfo(main);
if (mainHeadInfo != null) {
main = mainHeadInfo.getValue();
}
List newHeads = new ArrayList<>();
for (FastHeadInfo head : heads) {
if (head.isWriteHtml()) {
continue;
}
FastHeadInfo fastHeadInfo = FastChar.getOverrides().newInstance(head.getClass());
fastHeadInfo.putAll(head);
newHeads.add(fastHeadInfo);
}
String baseJsUrl = main;
boolean hasLogin = false;
boolean continueInfo = true;
if (role.equalsIgnoreCase("manager")) {
ExtManagerEntity manager = ExtManagerEntity.getSession(this);
if (manager != null) {
errorMessage = manager.getString("responsePageMessage");
FastHeadExtInfo indexUrl = FastChar.getConfig(FastExtConfig.class).getExtInfo("indexUrl");
if (indexUrl != null) {
baseJsUrl = indexUrl.getValue();
if (FastStringUtils.isNotEmpty(main)) {
baseJsUrl = main;
indexUrl.setValue(main);
}
} else {
responseJson(-1, "初始化失败!系统{{indexUrl}}配置文件异常,请及时告知开发人员!");
}
if (ExtManagerAction.MANAGER_SINGLE_LOGIN_CODE.containsKey(manager.getManagerId())) {
String loginCode = getSession("loginCode");
String lastLoginCode = ExtManagerAction.MANAGER_SINGLE_LOGIN_CODE.get(manager.getManagerId());
if (FastStringUtils.isNotEmpty(lastLoginCode) && FastStringUtils.isNotEmpty(loginCode) && !lastLoginCode.equalsIgnoreCase(loginCode)) {
errorMessage = "您的账户已在其他终端登录!";
removeSession("manager");
removeSession("loginCode");
continueInfo = false;
}
}
if (continueInfo) {
ExtManagerEntity byId = ExtManagerEntity.dao().getById(manager.getId());
if (byId != null) {
hasLogin = true;
IFastManagerListener iFastManager = FastChar.getOverrides().singleInstance(false, IFastManagerListener.class);
if (iFastManager != null) {
FastHandler handler = new FastHandler();
iFastManager.onManagerLogin(byId, handler);
if (handler.getCode() != 0) {
responseJson(-1, handler.getError());
}
}
manager.putAll(byId);
manager.pullInfo();
ExtManagerEntity.setSession(this, manager);
} else {
errorMessage = "您的账户信息已丢失,请您重新登录!";
}
}
}
if (!hasLogin) {
FastHeadExtInfo loginUrl = FastChar.getConfig(FastExtConfig.class).getExtInfo("loginUrl");
if (loginUrl != null) {
baseJsUrl = loginUrl.getValue();
} else {
responseJson(-1, "初始化失败!系统{{loginUrl}}配文件置异常,请及时告知开发人员!");
}
}
}
newHeads.add(FastExtConfig.getInstance().getThemeInfo());
newHeads.addAll(FastExtConfig.getInstance().getAllTabThemeInfo());
FastHeadScriptInfo headScriptInfo = new FastHeadScriptInfo();
headScriptInfo.setSrc(baseJsUrl);
newHeads.add(headScriptInfo);
FastHeadExtInfo debugExtInfo = new FastHeadExtInfo();
debugExtInfo.setName("debug");
debugExtInfo.setValue(String.valueOf(FastChar.getConstant().isDebug()));
newHeads.add(debugExtInfo);
FastHeadExtInfo localExtInfo = new FastHeadExtInfo();
localExtInfo.setName("local");
localExtInfo.setValue(String.valueOf(getProjectHost().startsWith("http://localhost")));
newHeads.add(localExtInfo);
for (FastHeadInfo newHead : newHeads) {
if (newHead instanceof FastHeadLinkInfo) {
FastHeadLinkInfo linkInfo = (FastHeadLinkInfo) newHead;
linkInfo.wrapHttp(getProjectHost());
} else if (newHead instanceof FastHeadScriptInfo) {
FastHeadScriptInfo scriptInfo = (FastHeadScriptInfo) newHead;
scriptInfo.wrapHttp(getProjectHost());
}
}
responseJson(0, "获取成功!", newHeads, errorMessage);
}
/**
* 进入系统管理员权限编辑页面
* 参数:
* 无
*/
@AFastSession
public void power() throws Exception {
addParam("power", "true");
index();
}
/**
* 获取系统配置信息
* 参数:
* 无
*/
@AFastSession
public void showConfig() throws Exception {
FastExtMenuXmlParser fastMenuXmlParser = FastExtMenuXmlParser.newInstance();
ExtManagerEntity session = ExtManagerEntity.getSession(this);
List appJs = FastExtConfig.getInstance().getAppJs(session, this);
List appJsUrls = new ArrayList<>();
for (File app : appJs) {
appJsUrls.add(parseFilePathToUrl(app));
}
Map data = new HashMap<>();
data.put("app", appJsUrls);
data.put("http", getProjectHost());
data.put("layer", FastExtConfig.getInstance().getLayerType() != FastExtLayerType.None);
data.put("entities", FastExtConfig.getInstance().getExtEntities().getEntityInfo(fastMenuXmlParser));
FastMenuInfo menus = fastMenuXmlParser.getMenus();
filterPowerMenus(menus.getChildren(), getParam("menuPower"));
fastMenuXmlParser.notifyListener(menus);
data.put("menus", menus.getChildren());
data.put("menusCss", buildCssContent(menus.getChildren()));
data.put("noticeListener", FastExtConfig.getInstance().isNoticeListener());
data.put("manager", session);
data.put("needInit", false);
FastHeadExtInfo initExtInfo = FastExtConfig.getInstance().getExtInfo("init");
if (initExtInfo != null) {
String initExtInfoValue = initExtInfo.getValue();
if (FastStringUtils.isNotEmpty(initExtInfoValue)) {
boolean needInit = FastNumberUtils.formatToInt(session.getString("initCode", "")) < FastNumberUtils.formatToInt(initExtInfoValue);
data.put("needInit", needInit);
if (needInit) {
session.set("initCode", initExtInfoValue);
session.update();
}
}
}
responseJson(0, "获取成功!", data);
}
private String parseFilePathToUrl(File file) {
String replace = file.getAbsolutePath().replace(FastChar.getPath().getWebRootPath(), "");
if (replace.startsWith("http://") || replace.startsWith("https://") || replace.startsWith("/")) {
if (new File(replace).exists()) {
return getProjectHost() + "attach?disposition=false&path=" + replace;
} else {
return appendFileTime(replace, file);
}
} else {
return getProjectHost() + appendFileTime(replace, file);
}
}
private String appendFileTime(String source, File file) {
if (source.contains("?")) {
return source + "&t=" + file.lastModified();
}
return source + "?t=" + file.lastModified();
}
private void filterPowerMenus(List menus) {
filterPowerMenus(menus, null);
}
private void filterPowerMenus(List menus, String menuPower) {
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
List waitRemove = new ArrayList<>();
for (FastMenuInfo menu : menus) {
if (managerEntity != null && managerEntity.getManagerRole().getRoleType() != ExtManagerRoleEntity.RoleTypeEnum.超级角色) {
String managerMenuPower = managerEntity.getString("managerMenuPower");
if (!managerMenuPower.contains(menu.getId())) {
waitRemove.add(menu);
continue;
}
}
if (FastStringUtils.isNotEmpty(menuPower)) {
if (!menuPower.contains(menu.getId())) {
waitRemove.add(menu);
continue;
}
}
filterPowerMenus(menu.getChildren(), menuPower);
}
menus.removeAll(waitRemove);
}
private String buildCssContent(List menus) {
if (menus == null || menus.size() == 0) {
return "";
}
List iconCls = new ArrayList();
for (FastMenuInfo menu : menus) {
if (FastStringUtils.isEmpty(menu.getIconCls())) {
String cssName = "ICON" + menu.getId().toUpperCase();
if (FastStringUtils.isNotEmpty(menu.getIcon())) {
if (FastChar.getConstant().isDebug()) {//调试模式并且是本地模式
iconCls.add("." + cssName + ":before{" +
"content: url(" + menu.getIcon() + "&t=" + System.currentTimeMillis() + ");" +
"}");
} else {
iconCls.add("." + cssName + ":before{content: url(" + menu.getIcon() + ");}");
}
menu.setIconCls(cssName);
}
} else {
iconCls.add(".x-treelist-nav .x-treelist-item-icon, .x-treelist-nav .x-treelist-item-tool ." + FastStringUtils.join(menu.getIconCls().split(" "), " .") + "{color:#" + FastStringUtils.stripStart(menu.getColor(), "#") + "}");
}
iconCls.add(buildCssContent(menu.getChildren()));
}
return FastStringUtils.join(iconCls, "\n");
}
/**
* 获取系统svg文件
* 参数:
* path svg相对项目的位置 {String}
* color svg填充的颜色 {String}
*/
public void icon(String path, String color) {
try {
setLog(false);
addResponseHeader("Cache-Control", "max-age=" + (60 * 60 * 24 * 360));
File iconFile = new File(path);
List webRootPaths = new ArrayList<>();
webRootPaths.add(FastChar.getPath().getWebRootPath());
webRootPaths.addAll(FastChar.getModules().getPathLoadModules());
for (String webRootPath : webRootPaths) {
String localPath = webRootPath + File.separator + path;
if (new File(localPath).exists()) {
iconFile = new File(localPath);
break;
}
}
if (iconFile.exists()) {
if (FastStringUtils.isNotEmpty(color)) {
color = color.replace("#", "");
if (iconFile.getName().toLowerCase().endsWith(".svg")) {
String coloLocalPath = iconFile.getParent() + File.separator + FastMD5Utils.MD5To16(color) + File.separator + iconFile.getName();
File colorFile = new File(coloLocalPath);
if (colorFile.exists()) {
responseFile(colorFile);
return;
}
String svgContent = FastFileUtils.readFileToString(iconFile);
String reg = "fill=\"#([0-9a-zA-Z]{6,8})\"";
String replaceAll = svgContent.replaceAll(reg, "fill=\"#" + color + "\"");
FastFileUtils.writeStringToFile(colorFile, replaceAll);
responseFile(colorFile);
}
}
responseFile(iconFile);
} else {
responseText("文件不存在!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获得图形验证码
*/
public FastOutCaptcha showCaptcha() {
return FastChar.getOverrides().newInstance(FastOutCaptcha.class).setStatus(200);
}
/**
* 获得枚举的值列表
* 参数:
* enumName 枚举的类名 {String}
*/
@AFastCache(checkClass = true)
public void showEnums() throws Exception {
String enumName = getParam("enumName", true);
IFastExtEnum enumClass = FastChar.getOverrides().singleInstance(IFastExtEnum.class, enumName);
if (enumClass != null) {
List enums = new ArrayList<>(enumClass.getEnums());
List waitRemove = new ArrayList<>();
for (FastEnumInfo anEnum : enums) {
if (anEnum == null) {
continue;
}
if (anEnum.getMapWrap().getBoolean("disabled", false)) {
waitRemove.add(anEnum);
}
}
Collections.sort(enums, new Comparator() {
@Override
public int compare(FastEnumInfo o1, FastEnumInfo o2) {
return Integer.compare(o1.getMapWrap().getInt("index"), o2.getMapWrap().getInt("index"));
}
});
enums.removeAll(waitRemove);
responseJson(0, "获取成功!", enums);
} else {
responseJson(-1, "获取失败!枚举'" + enumName + "'不存在!");
}
}
/**
* 获取权限菜单
* 参数:
* checked 默认选中的菜单Id {String}
* parent 父级的权限值 {String}
*/
@AFastSession
public List showPowerMenus() throws Exception {
String checked = getParam("checked");
FastExtMenuXmlParser fastMenuXmlParser = FastExtMenuXmlParser.newInstance();
FastMenuInfo menus = fastMenuXmlParser.getMenus();
filterPowerMenus(menus.getChildren(), getParam("parent"));
filterMenusByPower(menus.getChildren(), checked);
return menus.getChildren();
}
private void filterMenusByPower(List menus, String checked) {
List waitRemove = new ArrayList<>();
for (FastMenuInfo menu : menus) {
if (!menu.getMapWrap().getBoolean("power", true)) {
waitRemove.add(menu);
continue;
}
menu.setChecked(checked.contains(menu.getId()));
filterMenusByPower(menu.getChildren(), checked);
}
menus.removeAll(waitRemove);
}
/**
* 获得后台菜单列表
* 参数:
* checked 默认选中的菜单 {String}
*/
@AFastSession
public List showMenuColumn() throws Exception {
String checked = getParam("checked");
FastExtMenuXmlParser fastMenuXmlParser = FastExtMenuXmlParser.newInstance();
FastMenuInfo menus = fastMenuXmlParser.getMenus();
filterPowerMenus(menus.getChildren());
filterMenuColumn(menus.getChildren(), checked);
return menus.getChildren();
}
private void filterMenuColumn(List menus, String checked) {
ExtManagerEntity managerEntity = ExtManagerEntity.getSession(this);
if (managerEntity == null) {
return;
}
List waitRemove = new ArrayList<>();
for (FastMenuInfo menu : menus) {
menu.setChecked(checked.contains(menu.getId()));
if (FastBooleanUtils.formatToBoolean(menu.getLeaf(), false)) {
ExtSystemConfigEntity extConfig = ExtSystemConfigEntity.getInstance().getExtConfig(managerEntity.getId(), menu.getId());
if (extConfig == null) {
waitRemove.add(menu);
} else {
Map> stringMapMap = extConfig.toColumns();
if (stringMapMap == null) {
waitRemove.add(menu);
} else {
menu.setLeaf(false);
for (Map.Entry> stringMapEntry : stringMapMap.entrySet()) {
Map column = stringMapEntry.getValue();
if (column != null) {
if (column.containsKey("dataIndex")) {
FastMenuInfo menuInfo = new FastMenuInfo();
menuInfo.setLeaf(true);
menuInfo.setIcon("icons/icon_column.svg");
menuInfo.setId(FastMD5Utils.MD5(stringMapEntry.getKey() + menu.getId()));
menuInfo.setChecked(checked.contains(menuInfo.getId()));
menuInfo.setDepth(menu.getDepth() + 1);
menuInfo.setParentId(menu.getId());
menuInfo.putAll(column);
menu.getChildren().add(menuInfo);
}
}
}
}
}
} else {
filterMenuColumn(menu.getChildren(), checked);
}
}
menus.removeAll(waitRemove);
}
/**
* 上传文件
* 参数:
* type 文件保存的子目录名 {String}
* url 网络文件地址 {Array}【可选】
* file 上传的文件流 {Array} {File}
*/
public void upload() throws Exception {
setLogResponse(true);
String type = getParam("type");
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy