diff --git a/designer-base/src/main/java/com/fr/design/dialog/UIDialog.java b/designer-base/src/main/java/com/fr/design/dialog/UIDialog.java index 458daddab..63aea87c3 100644 --- a/designer-base/src/main/java/com/fr/design/dialog/UIDialog.java +++ b/designer-base/src/main/java/com/fr/design/dialog/UIDialog.java @@ -18,6 +18,7 @@ import java.awt.BorderLayout; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; +import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; @@ -44,6 +45,10 @@ public abstract class UIDialog extends JDialog { super(parent); } + public UIDialog(Window parent) { + super(parent); + } + public UIDialog(Frame parent, BasicPane pane) { this(parent, pane, true); diff --git a/designer-base/src/main/java/com/fr/design/mainframe/toast/SimpleToast.java b/designer-base/src/main/java/com/fr/design/mainframe/toast/SimpleToast.java new file mode 100644 index 000000000..b3b49a09f --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/mainframe/toast/SimpleToast.java @@ -0,0 +1,181 @@ +package com.fr.design.mainframe.toast; + +import com.fr.concurrent.NamedThreadFactory; +import com.fr.design.dialog.UIDialog; +import com.fr.design.gui.ilable.UILabel; +import com.fr.design.layout.FRGUIPaneFactory; +import com.fr.module.ModuleContext; + +import javax.swing.BorderFactory; +import javax.swing.Icon; +import javax.swing.JPanel; +import javax.swing.SwingConstants; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Point; +import java.awt.Window; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * toast弹窗 + * */ +public class SimpleToast extends UIDialog { + private static final int MIN_HEIGHT = 36; + private static final String TOAST_MSG_TIMER = "TOAST_MSG_TIMER"; + private static final long DEFAULT_DISAPPEAR_DELAY = 5000; + private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MILLISECONDS; + + + private ScheduledExecutorService TIMER; + private int hide_height = 0; + private JPanel contentPane; + private boolean show = false; + private Window parent; + private boolean autoDisappear; + + public SimpleToast(Window parent, Icon icon, String text, boolean autoDisappear) { + super(parent); + this.parent = parent; + this.autoDisappear = autoDisappear; + JPanel panel = createContentPane(icon, text); + init(panel); + } + + private JPanel createContentPane(Icon icon, String text) { + JPanel pane = FRGUIPaneFactory.createBorderLayout_S_Pane(); + + UILabel iconLabel = new UILabel(icon); + iconLabel.setVerticalAlignment(SwingConstants.TOP); + iconLabel.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0)); + + + UILabel textLabel = new UILabel(text); + pane.add(iconLabel, BorderLayout.WEST); + pane.add(textLabel, BorderLayout.CENTER); + pane.setBorder(BorderFactory.createEmptyBorder(8, 15, 8, 15)); + + + return pane; + } + + + private void init(JPanel panel) { + setFocusable(false); + setAutoRequestFocus(false); + setUndecorated(true); + contentPane = panel; + initComponent(); + } + + private void initComponent() { + this.getContentPane().setLayout(null); + this.getContentPane().add(contentPane); + Dimension dimension = calculatePreferSize(); + hide_height = dimension.height; + setSize(new Dimension(dimension.width, 0)); + contentPane.setSize(dimension); + setRelativeLocation(dimension); + if (autoDisappear) { + disappear(contentPane); + } + } + + private void setRelativeLocation(Dimension dimension) { + int positionX = parent.getLocationOnScreen().x + (parent.getWidth() - dimension.width) / 2; + int positionY = parent.getLocationOnScreen().y + 10; + this.setLocation(positionX, positionY); + } + + private Dimension calculatePreferSize() { + Dimension contentDimension = contentPane.getPreferredSize(); + int height = Math.max(MIN_HEIGHT, contentDimension.height); + return new Dimension(contentDimension.width, height); + } + + + public void display(JPanel outerJPanel) { + show = true; + outerJPanel.setLocation(0, -hide_height); + ScheduledExecutorService TIP_TOOL_TIMER = createToastScheduleExecutorService(); + TIP_TOOL_TIMER.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + Point point = outerJPanel.getLocation(); + if (point.y >= 0) { + TIP_TOOL_TIMER.shutdown(); + } + int showDistance = 5 + point.y < 0 ? 5 : -point.y; + outerJPanel.setLocation(point.x, point.y + showDistance); + Dimension dimension = SimpleToast.this.getSize(); + SimpleToast.this.setSize(new Dimension(dimension.width, dimension.height + showDistance)); + } + }, 0, 50, TimeUnit.MILLISECONDS); + + } + + private void disappear(JPanel outerJPanel, long delay, TimeUnit timeUnit) { + TIMER = createToastScheduleExecutorService(); + TIMER.schedule(new DisappearMotion(outerJPanel), delay, timeUnit); + } + + /** + * toast消失的动画效果 + * */ + class DisappearMotion implements Runnable { + JPanel panel; + + DisappearMotion(JPanel panel) { + this.panel = panel; + } + + @Override + public void run() { + ScheduledExecutorService TIP_TOOL_TIMER = createToastScheduleExecutorService(); + TIP_TOOL_TIMER.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + Point point = panel.getLocation(); + if (point.y <= -hide_height) { + TIP_TOOL_TIMER.shutdown(); + SimpleToast.this.setVisible(false); + SimpleToast.this.dispose(); + SimpleToast.this.show = false; + } + panel.setLocation(point.x, point.y - 5); + Dimension dimension = SimpleToast.this.getSize(); + SimpleToast.this.setSize(new Dimension(dimension.width, dimension.height - 5)); + } + }, 0, 50, TimeUnit.MILLISECONDS); + } + } + + + private void disappear(JPanel outerJPanel) { + disappear(outerJPanel, DEFAULT_DISAPPEAR_DELAY, DEFAULT_TIME_UNIT); + } + + private ScheduledExecutorService createToastScheduleExecutorService() { + return ModuleContext.getExecutor().newSingleThreadScheduledExecutor(new NamedThreadFactory(TOAST_MSG_TIMER)); + } + + @Override + public void checkValid() throws Exception { + } + + public void setVisible(boolean visible) { + super.setVisible(visible); + if (visible) { + display(contentPane); + } + } + + @Override + public void dispose() { + super.dispose(); + } + + public boolean isShow() { + return show; + } +} \ No newline at end of file diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/action/StartUseAction.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/action/StartUseAction.java index 0afd89308..e2758bb40 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/action/StartUseAction.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/action/StartUseAction.java @@ -1,8 +1,10 @@ package com.fr.design.mainframe.alphafine.action; +import com.fr.design.mainframe.alphafine.AlphaFineHelper; +import com.fr.design.mainframe.alphafine.download.FineMarketDownloadManager; import com.fr.design.mainframe.alphafine.model.TemplateResourceDetail; -import com.fr.design.mainframe.alphafine.search.helper.FineMarketClientHelper; +import javax.swing.SwingWorker; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -24,6 +26,13 @@ public class StartUseAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - FineMarketClientHelper.getInstance().openBrowserAndDownload(resourceDetail.getRoot()); + new SwingWorker() { + + @Override + protected Void doInBackground() throws Exception { + FineMarketDownloadManager.getInstance().downloadAndOpenTemplateResource(resourceDetail.getRoot(), AlphaFineHelper.getAlphaFineDialog()); + return null; + } + }.execute(); } } \ No newline at end of file diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/download/FineMarketDownloadManager.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/download/FineMarketDownloadManager.java new file mode 100644 index 000000000..88ef8dc4a --- /dev/null +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/download/FineMarketDownloadManager.java @@ -0,0 +1,170 @@ +package com.fr.design.mainframe.alphafine.download; + +import com.fr.base.svg.IconUtils; +import com.fr.common.util.Strings; +import com.fr.design.DesignerEnvManager; +import com.fr.design.extra.Process; +import com.fr.design.i18n.Toolkit; +import com.fr.design.login.DesignerLoginHelper; +import com.fr.design.login.DesignerLoginSource; +import com.fr.design.mainframe.DesignerContext; +import com.fr.design.mainframe.alphafine.AlphaFineHelper; +import com.fr.design.mainframe.alphafine.model.TemplateResource; +import com.fr.design.mainframe.alphafine.search.helper.FineMarketClientHelper; +import com.fr.design.mainframe.toast.SimpleToast; +import com.fr.file.FileFILE; +import com.fr.log.FineLoggerFactory; +import com.fr.third.jodd.io.ZipUtil; +import com.fr.workspace.WorkContext; + +import java.awt.Desktop; +import java.awt.Window; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; + +public class FineMarketDownloadManager { + + private static final FineMarketDownloadManager INSTANCE = new FineMarketDownloadManager(); + public static final FineMarketDownloadManager getInstance() { + return INSTANCE; + } + + + public static final double PROCESS_SUCCESS = 1d; + public static final double PROCESS_FAILED = -1d; + public static final double OPENING_FILE = 2d; + + private static final String OPENING_PLEASE_WAIT = Toolkit.i18nText("Fine-Design_Report_AlphaFine_Template_Resource_Opening"); + private static final String DOWNLOAD_FAILED = Toolkit.i18nText("Fine-Design_Report_AlphaFine_Template_Resource_Download_Failed_Check_Network"); + private static final String REPORTLETS = "/reportlets"; + + private HashMap processMap = new HashMap<>(); + + + /** + * 下载资源并解压, + * */ + public void downloadAndOpenTemplateResource(TemplateResource resource, Window parentWindow){ + // 验证登录 + String token = DesignerEnvManager.getEnvManager().getDesignerLoginRefreshToken(); + if (Strings.isEmpty(token)) { + DesignerLoginHelper.showLoginDialog(DesignerLoginSource.NORMAL, new HashMap<>(), AlphaFineHelper.getAlphaFineDialog()); + return; + } + downloadAndOpen(resource, parentWindow); + } + + private void downloadAndOpen(TemplateResource resource, Window parentWindow) { + // 获取报表录作为下载位置 + String workDir = WorkContext.getCurrent().getPath() + REPORTLETS; + File destDir = new File(workDir); + + DownloadProcess downloadProcess = new DownloadProcess(parentWindow); + String fileName = null; + try { + fileName = FineMarketClientHelper.getInstance().download(resource, destDir, downloadProcess); + open(fileName, downloadProcess); + } catch (Exception e) { + downloadProcess.process(FineMarketDownloadManager.PROCESS_FAILED); + FineLoggerFactory.getLogger().error(e, e.getMessage()); + } + } + + void open(String fileName, DownloadProcess process) throws IOException { + process.process(OPENING_FILE); + if (Strings.isEmpty(fileName)) { + return; + } + File fileNeedOpen = new File(fileName); + if (fileName.endsWith(".zip")) { + File[] files = unzip(fileName); + fileNeedOpen = getFirstCptOrFrm(files); + } else if (fileName.endsWith(".rar")) { + // rar直接打开系统文件夹 + File parentDir = new File(fileName).getParentFile(); + Desktop.getDesktop().open(parentDir); + return; + } + openInDesigner(fileNeedOpen); + } + + private File getFirstCptOrFrm(File[] files) { + for (File f : files) { + if (f.getName().endsWith(".cpt") || f.getName().endsWith(".frm")) { + return f; + } + } + return null; + } + + File[] unzip(String fileName) throws IOException { + File file = new File(fileName); + File parentDir = file.getParentFile(); + ZipUtil.unzip(file, parentDir); + return parentDir.listFiles(); + } + + void openInDesigner(File file) { + DesignerContext.getDesignerFrame().openTemplate(new FileFILE(file)); + } + + + /** + * 下载流程控制,主要控制ui的显示 + * */ + class DownloadProcess implements Process { + + SimpleToast downloadingToast; + SimpleToast openingToast; + SimpleToast failedToast; + Window parent; + + public DownloadProcess(Window parentWindow) { + this.parent = parentWindow; + init(); + } + + void init() { + showLoadingToast(); + } + + @Override + public void process(Double aDouble) { + if (aDouble == PROCESS_FAILED) { + downloadFailed(); + } else if (aDouble == PROCESS_SUCCESS) { + downloadSuccess(); + } else if (aDouble == OPENING_FILE) { + openingFile(); + } + } + + public void downloadFailed() { + downloadingToast.setVisible(false); + showFailedToast(); + } + + public void downloadSuccess() { + downloadingToast.setVisible(false); + } + + + private void openingFile() { + downloadingToast.setVisible(false); + openingToast = new SimpleToast(AlphaFineHelper.getAlphaFineDialog(), IconUtils.readIcon("/com/fr/design/mainframe/alphafine/images/loading.svg"), OPENING_PLEASE_WAIT, true); + openingToast.setVisible(true); + } + + private void showLoadingToast() { + downloadingToast = new SimpleToast(AlphaFineHelper.getAlphaFineDialog(), IconUtils.readIcon("/com/fr/design/mainframe/alphafine/images/loading.svg"), OPENING_PLEASE_WAIT, false); + downloadingToast.setVisible(true); + } + + private void showFailedToast() { + failedToast = new SimpleToast(AlphaFineHelper.getAlphaFineDialog(), IconUtils.readIcon("/com/fr/design/mainframe/alphafine/images/caution.svg"), DOWNLOAD_FAILED, true); + failedToast.setVisible(true); + } + + } +} diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/model/TemplateResource.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/model/TemplateResource.java index 93d1f4d0a..7bb4c879f 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/model/TemplateResource.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/model/TemplateResource.java @@ -1,5 +1,6 @@ package com.fr.design.mainframe.alphafine.model; +import com.fr.common.util.Strings; import com.fr.design.i18n.Toolkit; import com.fr.design.mainframe.alphafine.search.manager.impl.TemplateResourceSearchManager; import com.fr.general.IOUtils; @@ -46,6 +47,7 @@ public class TemplateResource { public static final String IMAGE_URL = "pic"; public static final String DEMO_URL = "demoUrl"; public static final String PKG_SIZE = "pkgsize"; + public static final String FILE_NAME = "fileLoca"; private static final String RECOMMEND_SEARCH_IMG_URL = "com/fr/design/mainframe/alphafine/images/more.png"; @@ -57,6 +59,7 @@ public class TemplateResource { private Image image; private String name; private String demoUrl; + private String fileName; private int pkgSize; private List recommendSearchKey; @@ -74,7 +77,7 @@ public class TemplateResource { public static TemplateResource createByJson(JSONObject jsonObject) { TemplateResource templateResource = new TemplateResource().setId(jsonObject.getString(ID)).setUuid(jsonObject.getString(UUID)).setName(jsonObject.getString(NAME)) - .setDemoUrl(jsonObject.getString(DEMO_URL)).setPkgSize(jsonObject.getInt(PKG_SIZE)); + .setDemoUrl(jsonObject.getString(DEMO_URL)).setPkgSize(jsonObject.getInt(PKG_SIZE)).setFileName(jsonObject.getString(FILE_NAME)); int pkgSize = templateResource.getPkgSize(); if (pkgSize == 0) { templateResource.type = Type.SINGLE_TEMPLATE; @@ -110,6 +113,20 @@ public class TemplateResource { } + + public String getFileName() { + return fileName; + } + + public TemplateResource setFileName(String fileName) { + if (Strings.isEmpty(fileName)) { + this.fileName = getName() + ".zip"; + } else { + this.fileName = fileName; + } + return this; + } + public Type getType() { return type; } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java index 81a165ca0..f261d97ba 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java @@ -1,18 +1,45 @@ package com.fr.design.mainframe.alphafine.search.helper; +import com.fr.design.DesignerEnvManager; +import com.fr.design.extra.PluginConstants; +import com.fr.design.mainframe.alphafine.download.FineMarketDownloadManager; import com.fr.design.mainframe.alphafine.model.TemplateResource; -import com.fr.design.utils.BrowseUtils; +import com.fr.ftp.util.Base64; import com.fr.general.CloudCenter; import com.fr.general.http.HttpToolbox; import com.fr.json.JSONArray; import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StableUtils; +import com.fr.third.org.apache.http.HttpEntity; +import com.fr.third.org.apache.http.HttpException; +import com.fr.third.org.apache.http.HttpStatus; +import com.fr.third.org.apache.http.client.config.CookieSpecs; +import com.fr.third.org.apache.http.client.config.RequestConfig; +import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; +import com.fr.third.org.apache.http.client.methods.HttpUriRequest; +import com.fr.third.org.apache.http.client.methods.RequestBuilder; +import com.fr.third.org.apache.http.impl.client.BasicCookieStore; +import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; +import com.fr.third.org.apache.http.impl.client.HttpClients; import org.jetbrains.annotations.Nullable; +import javax.crypto.Cipher; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.security.KeyFactory; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.X509EncodedKeySpec; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class FineMarketClientHelper { private static final FineMarketClientHelper INSTANCE = new FineMarketClientHelper(); @@ -20,10 +47,13 @@ public class FineMarketClientHelper { return INSTANCE; } + private static final String CERTIFICATE_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtsz62CPSWXZE/IYZRiAuTSZkw\n" + + "1WOwer8+JFktK0uKLAUuQoBr+UjAMFtRA8W7JgKMDwZy/2liEAiXEOSPU/hrdV8D\n" + + "tT541LnGi1X/hXiRwuttPWYN3L2GYm/d5blU+FBNwghBIrdAxXTzYBc6P4KL/oYX\n" + + "nMdTIrkz8tYkG3QoFQIDAQAB"; public static final String FINE_MARKET_TEMPLATE_INFO = CloudCenter.getInstance().acquireUrlByKind("market.template.info"); public static final String FINE_MARKET_TEMPLATE_URL = CloudCenter.getInstance().acquireUrlByKind("market.template.url"); - public static final String LOGIN_FINE_CLUB_AND_REDIRECT_SHOP = CloudCenter.getInstance().acquireUrlByKind("af.login.redirect.market"); - public static final String FILE_DOWNLOAD = "file/"; + public static final String FILE_DOWNLOAD = "file/download/"; public static final String PACKAGE_DOWNLOAD = "package/download/"; public static final String TEMPLATES_PARENT_PACKAGE = "parent/"; public static final String TEMPLATES_TAGS = "filter"; @@ -46,44 +76,157 @@ public class FineMarketClientHelper { * */ public String getResourceDownloadUrl(TemplateResource templateResource) { if (TemplateResource.Type.SCENARIO_SOLUTION.equals(templateResource.getType())) { - return getPackageDownloadUrl(templateResource.getId()); + return getPackageDownloadUrl(); } else { - return getFileDownLoadUrl(templateResource.getId()); + return getFileDownLoadUrl(); } } - public void openBrowserAndDownload(TemplateResource templateResource) { - String url = LOGIN_FINE_CLUB_AND_REDIRECT_SHOP; - if (TemplateResource.Type.SCENARIO_SOLUTION.equals(templateResource.getType())) { - url += getPackageDownloadUrl(templateResource.getId()); + private String getPackageDownloadUrl() { + return FINE_MARKET_TEMPLATE_INFO + PACKAGE_DOWNLOAD; + } + + private String getFileDownLoadUrl() { + return FINE_MARKET_TEMPLATE_INFO + FILE_DOWNLOAD; + } + + + public String download(TemplateResource resource, File destDir, com.fr.design.extra.Process process) throws Exception { + String resourceId = resource.getId(); + + CloseableHttpResponse fileRes = getFileResponse(resource, resourceId); + + if (fileRes.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + + File destFile = createDestFile(destDir, resource); + + StableUtils.makesureFileExist(destFile); + FileOutputStream writer = new FileOutputStream(destFile.getAbsolutePath()); + + HttpEntity entity = fileRes.getEntity(); + long totalSize = entity.getContentLength(); + InputStream content = entity.getContent(); + + + byte[] data = new byte[PluginConstants.BYTES_NUM]; + int bytesRead; + int totalBytesRead = 0; + + while ((bytesRead = content.read(data)) > 0) { + writer.write(data, 0, bytesRead); + data = new byte[PluginConstants.BYTES_NUM]; + totalBytesRead += bytesRead; + process.process(totalBytesRead / (double) totalSize); + } + content.close(); + writer.flush(); + writer.close(); + FineLoggerFactory.getLogger().info("download resource{} success", resourceId); + process.process(FineMarketDownloadManager.PROCESS_SUCCESS); + + return destFile.getAbsolutePath(); } else { - url += getFileDownLoadUrl(templateResource.getId()); + FineLoggerFactory.getLogger().info("download resource{} failed", resourceId); + process.process(FineMarketDownloadManager.PROCESS_FAILED); + throw new HttpException(); } - BrowseUtils.browser(url); + + } - /** - * 暂时没有package的下载接口,需要用户在浏览器点击下载 - * */ - private String getPackageDownloadUrl(String id) { - return getTemplateUrlById(id); + private CloseableHttpResponse getFileResponse(TemplateResource resource, String resourceId) throws Exception { + CloseableHttpResponse fileRes = postDownloadHttpResponse(getResourceDownloadUrl(resource), resourceId); + if (fileRes.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { + fileRes = getDownloadHttpResponse(fileRes.getHeaders("Location")[0].getValue()); + } + return fileRes; } /** - * 打开浏览器,下载单一模板 + * 在目标路径下(destDir)创建与资源名称相同的文件夹 (finalDir) + * 将资源下载到 destDir/finalDir + * 如果文件重复,则在文件名前加自增id * */ - private String getFileDownLoadUrl(String id) { - return FINE_MARKET_TEMPLATE_INFO + FILE_DOWNLOAD + id; + private File createDestFile(File destDir, TemplateResource resource) { + String fileName = resource.getName(); + try { + File finalDir = new File(destDir.getAbsolutePath() + "/" + fileName); + if (!finalDir.exists()) { + finalDir.mkdir(); + } + + // 获取文件名(含后缀) + fileName = resource.getFileName(); + + // 处理重复文件名 + String fileNameFormat = "(%d)" + fileName; + Pattern pattern = Pattern.compile("\\((\\d)\\)" + fileName); + int cnt = 0; + + File[] files = finalDir.listFiles(); + for (File f : files) { + Matcher matcher = pattern.matcher(f.getName()); + if (matcher.find()) { + cnt = Math.max(cnt, Integer.parseInt(matcher.group(1))); + } + } + cnt++; + fileName = String.format(fileNameFormat, cnt); + + + + File destFile = new File(finalDir.getAbsolutePath() + "/" + fileName); + destFile.createNewFile(); + return destFile; + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e, e.getMessage()); + } + return null; + } + + private static CloseableHttpResponse getDownloadHttpResponse(String url) throws Exception { + //先登录一下。不然可能失败 + CloseableHttpClient client = createClient(); + HttpUriRequest file = RequestBuilder.get() + .setUri(url) + .build(); + return client.execute(file); } + private static CloseableHttpResponse postDownloadHttpResponse(String url, String id) throws Exception { + return postDownloadHttpResponse(url, id, new HashMap<>()); + } + private static CloseableHttpResponse postDownloadHttpResponse(String url, String id, Map params) throws Exception { + //先登录一下。不然可能失败 + CloseableHttpClient client = createClient(); + FineLoggerFactory.getLogger().info("login fr-market"); + FineLoggerFactory.getLogger().info("start download resource {}", id); + RequestBuilder builder = RequestBuilder.post() + .setHeader("User-Agent", "Mozilla/5.0") + .setUri(url) + .addParameter("id", encrypt(id)) + .addParameter("userId", String.valueOf(DesignerEnvManager.getEnvManager().getDesignerLoginUid())); - /** - * 获取帆软市场模板资源页面链接 - * */ - public String getFineMarketTemplateUrl() { - return FINE_MARKET_TEMPLATE_URL; + if (params != null) { + Set keys = params.keySet(); + for (String key: keys) { + builder.addParameter(key, params.get(key)); + } + } + return client.execute(builder.build()); + } + + + private static CloseableHttpClient createClient() { + + BasicCookieStore cookieStore = new BasicCookieStore(); + return HttpClients.custom() + .setDefaultRequestConfig(RequestConfig.custom() + .setCookieSpec(CookieSpecs.STANDARD).build()) + .setDefaultCookieStore(cookieStore) + .build(); } public @Nullable JSONObject getTemplateInfoById(String id) throws IOException { @@ -175,4 +318,15 @@ public class FineMarketClientHelper { } } + + private static String encrypt(String str) throws Exception { + //base64编码的公钥 + byte[] decoded = Base64.decodeBase64(CERTIFICATE_PUBLIC_KEY); + RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); + //RSA加密 + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, pubKey); + String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8))); + return outStr; + } } \ No newline at end of file diff --git a/designer-realize/src/main/resources/com/fr/design/mainframe/alphafine/images/caution.svg b/designer-realize/src/main/resources/com/fr/design/mainframe/alphafine/images/caution.svg new file mode 100644 index 000000000..731e79d0d --- /dev/null +++ b/designer-realize/src/main/resources/com/fr/design/mainframe/alphafine/images/caution.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/designer-realize/src/main/resources/com/fr/design/mainframe/alphafine/images/loading.svg b/designer-realize/src/main/resources/com/fr/design/mainframe/alphafine/images/loading.svg new file mode 100644 index 000000000..1df6ef5f2 --- /dev/null +++ b/designer-realize/src/main/resources/com/fr/design/mainframe/alphafine/images/loading.svg @@ -0,0 +1,3 @@ + + +