使用Java扩展低代码系列:高效构建自定义插件

发布时间:2024/04/15 19:04 发布者:Leo

返回博客中心

前言

随着软件开发的快速发展和需求的不断增长,开发人员面临着更多的压力和挑战。传统的开发方法需要花费大量的时间和精力,而低代码开发平台的出现为开发人员提供了一种更加高效、快速的开发方式。对于需要和外部系统对接的许多场景,需要对低代码平台进行编程扩展。本文就以构建命令插件为例,展示如何使用Java语言高效构建自定义插件。

环境准备

插件生成器

打开活字格插件构建工具-Java版链接(forguncyJavaPluginGenerator),下载【活字格Java扩展创建工具】。推荐使用压缩包版本创建。

注意:如果压缩包版本出现闪退的现象,是因为系统太老没有webview2。推荐使用msi安装包安装,或者更新系统到2018年4月之后的win10 或更高版本。

打开【forguncyJavaExtensionGenerateTool.exe】,在如下界面配置插件的基础信息:

点击创建服务端命令插件,创建完成后,在设置的对应目录下会生成工程文件:

接下来使用IDE编译器打开MyPlugin工程,打开后,工程目录如下图:

至此就完成了前期的准备工作,下面我们来进行代码逻辑的编写。

代码实现

添加依赖

在实现代码之前,我们先要增加一些活字格的相关依赖,如下图我们需要给pom文件中添加如下依赖:

插件的图标和Logo替换Icon.png和PluginLogo.png即可。

而【PluginConfig.json】用于对插件的信息做基本的配置:

{
  "assembly": [],                                    // 如需要加载其他类
  "javascript": [],                                  // 如需加载其他JavaScript文件
  "css": [],                                         // 如需加载其他css文件
  "image": "resources/PluginLogo.png",               // 需要加载图片的相对路径
  "description": "这是一个活字格插件",                 // 插件的文本描述信息
  "description_cn": "这是一个活字格插件",              // 插件的中文文本描述信息
  "name": "MyPlugin",                                // 插件名称
  "name_cn": "我的插件",                              // 插件中午名称
  "pluginType": "command",                           // 插件类型,当前为命令类型插件
  "guid": "fefeb164-ab98-48c8-b309-b5410052e504",    // 插件唯一标识GUID,建议勿修改
  "version": "1.0.0.0",                              // 插件版本
  "dependenceVersion": "10.0.0.0"                    // 插件支持依赖最低活字格版本
}

编写核心代码逻辑

在完成上述配置之后,就可以编写插件逻辑了。下面是插件的一段示例代码,主要是通过5个参数(AppSecret、请求ID、时间戳、数据和签名结果)生成一段随机数签名。

package org.example;

import com.grapecity.forguncy.LoggerContext;
import com.grapecity.forguncy.commands.ICommandExecutableInServerSide;
import com.grapecity.forguncy.commands.IServerCommandExecuteContext;
import com.grapecity.forguncy.commands.annotation.ResultToProperty;
import com.grapecity.forguncy.commands.annotation.common.Category;
import com.grapecity.forguncy.commands.entity.Command;
import com.grapecity.forguncy.commands.entity.ExecuteResult;
import com.grapecity.forguncy.commands.enumeration.CommandScope;
import com.grapecity.forguncy.plugincommon.common.annotation.*;
import lombok.Data;
import org.apache.commons.codec.binary.Base64;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.servlet.http.HttpServletRequest;

@Data
@Icon( uri= "resources/Icon.png")
@Category(category = "程杰合集")
public class MyPlugin extends Command implements ICommandExecutableInServerSide {

    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    @DisplayName(displayName = "AppSecret")
    @FormulaProperty
    @Required
    private String appSecret;

    @DisplayName(displayName = "请求ID")
    @FormulaProperty
    @Required
    private String requestId;

    @DisplayName(displayName = "时间戳")
    @FormulaProperty
    @Required
    private String timestamp;

    @DisplayName(displayName = "数据")
    @FormulaProperty
    @Required
    private String data;

    @ResultToProperty
    @FormulaProperty
    @DisplayName(displayName = "签名结果")
    private String resultTo = "结果";

    @Override
    public ExecuteResult execute(IServerCommandExecuteContext dataContext) {
        Long innerTimestamp = Long.parseLong(timestamp);
        String res = null;
        try {
            res = sign(appSecret, requestId, innerTimestamp, data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            dataContext.getParameters().put(resultTo, res);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        ExecuteResult executeResult = new ExecuteResult();
        executeResult.getReturnValues().put("结果", res);
        return executeResult;
    }

    @Override
    public boolean getDesignerPropertyVisible(String propertyName, CommandScope commandScope) {

        return super.getDesignerPropertyVisible(propertyName, commandScope);
    }

    @Override
    public String toString() {
        return "签名程杰";
    }

    public static String sign(String appSecret, String requestId, Long timestamp, String data) throws Exception{
        // 1.签名参数按自然升序排列,拼接上data
        StringBuilder sb = new StringBuilder();
        sb.append("appSecret=").append(appSecret).append("&")
                .append("requestId=").append(requestId).append("&")
                .append("timestamp=").append(timestamp)
                .append(data);
        // 2.对签名字符串base64编码后获取32位md5值
        // 2.对签名字符串base64编码后获取32位md5值
        String base64Encode = base64Encode(sb.toString().getBytes("UTF-8"));
        String md5Value = md5(base64Encode);
        // 3.将得到的MD5值进行sha1散列,转换为16进制字符串
        String sign = sha1(md5Value);
        return sign;
    }

    /**
     * 对字符串进行MD5加密,得到32位MD5值
     * @param text 明文
     * @return 密文
     */
    public static String md5(String text) {
        try {
            MessageDigest msgDigest = MessageDigest.getInstance("MD5");
            msgDigest.update(text.getBytes("UTF-8"));
            byte[] bytes = msgDigest.digest();
            // 转成16进制
            return new String(encodeHex(bytes));
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("System doesn't support MD5 algorithm.");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("System doesn't support your  EncodingException.");
        }
    }

    /***
     * SHA加密
     * @return
     */
    public static String sha1(String content) throws Exception {
        MessageDigest sha = MessageDigest.getInstance("SHA1");
        byte[] byteArray = content.getBytes("UTF-8");
        return new String(encodeHex(sha.digest(byteArray)));
    }


    /**
     * base64编码
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String base64Encode(byte[] content) throws Exception {
        return Base64.encodeBase64String(content).replaceAll("(\\\r\\\n|\\\r|\\\n|\\\n\\\r)", "");
    }

    /**
     * base64解码
     *
     * @param content
     * @return
     * @throws Exception
     */

    public static byte[] base64Decode(String content) throws Exception {
        return Base64.decodeBase64(content);
    }

    /**
     * 转换成16进制
     * @param data
     * @return
     */
    private static char[] encodeHex(byte[] data) {
        int l = data.length;
        char[] out = new char[l << 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }
        return out;
    }
    public static String getPostData(HttpServletRequest request) {
        StringBuilder data = new StringBuilder();
        String line;
        BufferedReader reader;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine())) {
                data.append(line);
            }
        } catch (IOException e) {
            return null;
        }
        return data.toString();
    }
}

使用maven打包插件

代码写完后,将整个项目打包:在此处点击【clean】,然后点击【install】:

接着在【target】目录会出现打包产物:

紧接着把打包后的zip插件安装到活字格设计器使用。

新建命令,在命令选择中就可以找到刚才打包的插件。

填写参数:

可以在服务端命令中进行测试:

可以看到,上图的测试结果中返回了一段随机数签名。这样,一个使用Java语言构建的插件就已经开发完成。

总结

以上就是如何使用Java如何在低代码平台中开发一个命令插件的全过程,如果您想了解更多的信息,欢迎点击这里查看。


活字格企业级低代码开发平台 | 下载试用

活字格 是葡萄城基于在专业控件领域 40 年的技术积累而推出的企业级低代码开发平台 ,由简单易用的可视化设计器和部署灵活的服务器构成,能帮助开发人员、IT 技术人员和业务人员快速构建美观易用、架构专业、安全可控的企业级多终端应用,并随需而变。活字格高度开放灵活,支持云部署和本地部署,能与微信、钉钉及各行业应用软件无缝集成,并可对接智能硬件、AI 等技术,全面支撑核心业务系统开发。

了解更多关于活字格企业级低代码开发平台内容,请点击此处访问官网,立即下载体验。