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.annotation.AFastSession;
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.IFastManager;
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"));
FastHeadExtInfo extInfo = FastExtConfig.getInstance().getExtInfo("theme-color");
if (extInfo != null) {
holders.put("color", extInfo.getColorValue());
} else {
holders.put("color", FastExtConfig.getInstance().getDefaultThemeColor());
}
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;
IFastManager iFastManager = FastChar.getOverrides().singleInstance(false, IFastManager.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");
}
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();
}
}
/**
* 获得验证码
*
* @return FastOutCaptcha
*/
public FastOutCaptcha showCaptcha() {
return FastChar.getOverrides().newInstance(FastOutCaptcha.class).setStatus(200);
}
/**
* 获得枚举列表
*/
@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 + "'不存在!");
}
}
/**
* 获取权限菜单
*/
@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"));
filterMenus(newMenus, checked);
return newMenus;
}
private void filterMenus(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();
filterMenus(menu.getChildren(), checked);
}
menus.removeAll(waitRemove);
}
/**
* 获得菜单列
*
* @return
*/
@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.putAll(column);
menuInfo.fromProperty();
menu.getChildren().add(menuInfo);
}
}
}
menu.fromProperty();
}
}
} else {
filterMenuColumn(menu.getChildren(), checked);
}
}
menus.removeAll(waitRemove);
}
/**
* 上传文件
*/
public void upload() throws Exception {
String type = getParam("type");
FastFile> paramFile = getParamFile();
if (paramFile != null) {
if (FastStringUtils.isNotEmpty(type)) {
paramFile = paramFile.renameTo(new File(paramFile.getAttachDirectory() + File.separator + type,
FastMD5Utils.MD5(System.currentTimeMillis() + paramFile.getFileName()) + paramFile.getExtensionName()), true);
}
String fileUrl = paramFile.getUrl();
Map result = new HashMap<>();
result.put("url", fileUrl);
result.put("http", getProjectHost());
responseJson(0, "上传成功!", result);
} else {
responseJson(-1, "上传失败!");
}
}
/**
* 下载或查看文件
*/
@AFastRoute({"/download", "/attach"})
public void down() {
setLog(FastExtConfig.getInstance().isAttachLog());
String path = getParam("path", true);
if (path.startsWith("http://") || path.startsWith("https://")) {
redirect(path);
}
boolean disposition = getParamToBoolean("disposition", true);
File file = new File(path);
if (!file.exists()) {
file = new File(FastChar.getConstant().getAttachDirectory(), path);
}
if (!file.exists()) {
responseJson(-1, "文件不存在!" + file.getAbsolutePath());
}
responseFile(file, disposition);
}
/**
* 删除附件
*/
public void deleteAttach() {
List paths = getParamToList("path");
for (String path : paths) {
path = path.replace("attach/", "");
File file = new File(path);
if (!file.exists()) {
file = new File(FastChar.getConstant().getAttachDirectory(), path);
}
file.delete();
}
responseJson(0, "删除成功!");
}
/**
* 压缩文件
*/
public void zipFile() throws IOException {
File folderFile = new File(FastChar.getConstant().getAttachDirectory(), "/zip" + System.currentTimeMillis());
if (!folderFile.exists()) {
if (!folderFile.mkdirs()) {
responseJson(-1, "文件夹创建失败!" + folderFile.getAbsolutePath());
}
}
List paths = getParamToList("path", true);
for (String path : paths) {
if (path.startsWith("http://") || path.startsWith("https://")) {
URL url = new URL(path);
String fileName = path.substring(path.lastIndexOf("/") + 1);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String headerField = conn.getHeaderField("content-disposition");
if (FastStringUtils.isNotEmpty(headerField)) {
String regStr = "filename=\"(.*)\"";
Matcher matcher = Pattern.compile(regStr).matcher(headerField);
if (matcher.find()) {
fileName = matcher.group(1);
}
}
FastFileUtils.copyURLToFile(url, new File(folderFile, fileName));
}else{
path = path.replace("attach/", "").split("\\?")[0];
File file = new File(FastChar.getConstant().getAttachDirectory(), path);
if (file.isDirectory()) continue;
if (file.exists()) {
FastFileUtils.copyFileToDirectory(file, folderFile);
}
}
}
File zipFile = FastFileUtils.zipFile(folderFile.getAbsolutePath());
if (zipFile != null) {
FastFileUtils.deleteDirectory(folderFile);
responseFile(zipFile);
} else {
responseJson(-1, "压缩失败!");
}
}
/**
* 查看生成的接口文档
*/
@AFastRoute({"document", "interface", "document.html", "interface.html","api"})
public void doc() throws Exception {
File file = new File(FastChar.getPath().getWebRootPath(), "documents");
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".html");
}
});
List