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.
package com.fastchar.extjs.action;
import com.fastchar.annotation.AFastCache;
import com.fastchar.annotation.AFastRoute;
import com.fastchar.core.*;
import com.fastchar.extjs.FastExtConfig;
import com.fastchar.extjs.FastExtHelper;
import com.fastchar.extjs.annotation.AFastSession;
import com.fastchar.extjs.core.heads.*;
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.observer.FastHeadXmlObserver;
import com.fastchar.extjs.observer.FastMenuXmlObserver;
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.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
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"));
}
}
}
if (FastHeadXmlObserver.isModified()) {
FastChar.getObservable().notifyObservers("refreshHeads");
}
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());
extInfo.fromProperty();
}
}
List heads = FastChar.getValues().get("heads");
if (heads == null) {
responseText("FastCharExtWeb未启动!请先配置启动!");
return;
}
FastHeadExtInfo version = FastExtConfig.getInstance().getExtInfo("version");
if (version != null && FastChar.getConstant().isDebug()) {
version.setValue(String.valueOf(System.currentTimeMillis()));
version.fromProperty();
}
StringBuilder headString = new StringBuilder();
for (FastHeadInfo head : heads) {
if (head.isWriteHtml()) {
headString.append(head.getText()).append("\n");
}
}
Map holders = new HashMap<>();
holders.put("head", headString.toString());
holders.put("http", getProjectHost());
holders.put("power", getParamToBoolean("power"));
holders.put("indexUrl", getRequest().getRequestURL().toString());
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() {
List heads = FastChar.getValues().get("heads");
List newHeads = new ArrayList<>(heads);
String baseJsUrl = null;
boolean hasLogin = false;
ExtManagerEntity manager = getSession("manager");
if (manager != null) {
FastHeadExtInfo indexUrl = FastChar.getConfig(FastExtConfig.class).getExtInfo("indexUrl");
if (indexUrl != null) {
baseJsUrl = indexUrl.getValue();
} else {
responseJson(-1, "初始化失败!系统index.js文件异常,请及时告知开发人员!");
}
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());
}
}
setSession("manager", byId);
}
}
if (!hasLogin) {
FastHeadExtInfo loginUrl = FastChar.getConfig(FastExtConfig.class).getExtInfo("loginUrl");
if (loginUrl != null) {
baseJsUrl = loginUrl.getValue();
} else {
responseJson(-1, "初始化失败!系统login.js文件异常,请及时告知开发人员!");
}
}
FastHeadScriptInfo headScriptInfo = new FastHeadScriptInfo();
headScriptInfo.setSrc(baseJsUrl);
headScriptInfo.fromProperty();
newHeads.add(FastExtConfig.getInstance().getThemeInfo());
newHeads.add(headScriptInfo);
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);
}
/**
* 进入系统管理员权限编辑页面
* 参数:
* 无
*/
@AFastSession
public void power() throws Exception {
addParam("power", "true");
index();
}
/**
* 获取系统配置信息
* 参数:
* 无
*/
@AFastSession
public void showConfig() throws Exception {
if (FastMenuXmlObserver.isModified()) {
FastChar.getObservable().notifyObservers("refreshMenus");
}
List appJs = FastExtConfig.getInstance().getAppJs();
List appJsUrls = new ArrayList<>();
for (File app : appJs) {
String replace = app.getAbsolutePath().replace(FastChar.getPath().getWebRootPath(), "");
if (replace.startsWith("http://") || replace.startsWith("https://") || replace.startsWith("/")) {
appJsUrls.add(replace);
} else {
appJsUrls.add(getProjectHost() + replace);
}
}
Map data = new HashMap<>();
data.put("app", appJsUrls);
data.put("http", getProjectHost());
data.put("entities", FastExtConfig.getInstance().getExtEntities().getEntityInfo());
FastMenuInfo menus = FastChar.getValues().get("menus");
List newMenus = new ArrayList<>(menus.copy().getChildren());
filterPowerMenus(newMenus, getParam("menuPower"));
data.put("menus", newMenus);
data.put("menusCss", buildCssContent(newMenus));
data.put("manager", getSession("manager"));
responseJson(0, "获取成功!", data);
}
private void filterPowerMenus(List menus) {
filterPowerMenus(menus, null);
}
private void filterPowerMenus(List menus, String menuPower) {
ExtManagerEntity managerEntity = getSession("manager");
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(), "#") + "}");
}
menu.fromProperty();
iconCls.add(buildCssContent(menu.getChildren()));
}
return FastStringUtils.join(iconCls, "\n");
}
/**
* 获取系统svg文件
* 参数:
* path svg相对项目的位置 {String}
* color svg填充的颜色 {String}
*/
public void icon() {
try {
setLog(false);
String path = getParam("path");
String color = getParam("color");
String localPath = FastChar.getPath().getWebRootPath() + "/" + path;
File file = new File(localPath);
if (file.exists()) {
if (FastStringUtils.isNotEmpty(color)) {
if (file.getName().toLowerCase().endsWith(".svg")) {
String coloLocalPath = file.getParent() + "/" + FastMD5Utils.MD5(color) + "/" + file.getName();
File colorFile = new File(coloLocalPath);
if (colorFile.exists()) {
responseFile(colorFile);
return;
}
String svgContent = FastFileUtils.readFileToString(file);
String reg = "fill=\"#([0-9a-zA-Z]{6,8})\"";
String replaceAll = svgContent.replaceAll(reg, "fill=\"#" + color + "\"");
FastFileUtils.writeStringToFile(colorFile, replaceAll);
responseFile(colorFile);
}
}
responseFile(file);
} 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) {
responseJson(0, "获取成功!", enumClass.getEnums());
} else {
responseJson(-1, "获取失败!枚举'" + enumName + "'不存在!");
}
}
/**
* 获取权限菜单
* 参数:
* checked 默认选中的菜单Id {String}
* parent 父级的权限值 {String}
*/
@AFastSession
public List showPowerMenus() {
String checked = getParam("checked");
FastMenuInfo menus = FastChar.getValues().get("menus");
List newMenus = new ArrayList<>(menus.copy().getChildren());
filterPowerMenus(newMenus, getParam("parent"));
filterMenusByPower(newMenus, checked);
return newMenus;
}
private void filterMenusByPower(List menus, String checked) {
List waitRemove = new ArrayList<>();
for (FastMenuInfo menu : menus) {
if (!menu.getBoolean("power", true)) {
waitRemove.add(menu);
continue;
}
menu.setChecked(checked.contains(menu.getId()));
menu.fromProperty();
filterMenusByPower(menu.getChildren(), checked);
if (menu.getLeaf()) {
}
}
menus.removeAll(waitRemove);
}
/**
* 获得后台菜单列表
* 参数:
* checked 默认选中的菜单 {String}
*/
@AFastSession
public List showMenuColumn() {
String checked = getParam("checked");
FastMenuInfo menus = FastChar.getValues().get("menus");
List newMenus = new ArrayList<>(menus.copy().getChildren());
filterPowerMenus(newMenus);
filterMenuColumn(newMenus, checked);
return newMenus;
}
private void filterMenuColumn(List menus, String checked) {
ExtManagerEntity managerEntity = getSession("manager");
if (managerEntity == null) {
return;
}
List waitRemove = new ArrayList<>();
for (FastMenuInfo menu : menus) {
menu.setChecked(checked.contains(menu.getId()));
menu.fromProperty();
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 (String key : stringMapMap.keySet()) {
Map column = stringMapMap.get(key);
if (column != null) {
if (column.containsKey("dataIndex")) {
FastMenuInfo menuInfo = new FastMenuInfo();
menuInfo.setLeaf(true);
menuInfo.setIcon("icons/icon_column.svg");
menuInfo.setId(FastMD5Utils.MD5(key + menu.getId()));
menuInfo.setChecked(checked.contains(menuInfo.getId()));
menuInfo.setDepth(menu.getDepth() + 1);
menuInfo.setParentId(menu.getId());
menuInfo.setAll(column);
menuInfo.fromProperty();
menu.getChildren().add(menuInfo);
}
}
}
menu.fromProperty();
}
}
} 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