z1111 发表于 2024-5-28 10:25:58

【10.0】【请问如何接收java服务端开发的返回结果】

我用的是程杰大佬的例子,返回值有4个,我想把这4个值都进行后续操作,该如何做呢?

Ben.C 发表于 2024-5-28 10:25:59

看代码第60行,如果你这个命令所创建的变量想在当前服务端命令列表中都可以使用。
直接在当前上下文的参数中添加
dataContext.getParameters().put(resultTo, res);

z1111 发表于 2024-5-28 10:48:32

package org.example;

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;

@Data
public class TestChengJie 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("AppSecret")
    @FormulaProperty
    @Required
    private String appSecret;

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

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

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

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

    @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("resultTo", "resultTo 11111");
      executeResult.getReturnValues().put("结果6666", "结果6666 111111");
      executeResult.getReturnValues().put("结果7777", res+2333);
      executeResult.getReturnValues().put("结果8888", res+34444444);
      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 yourEncodingException.");
      }
    }

    /***
   * 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;
      // two characters form the hex value.
      for (int i = 0, j = 0; i < l; i++) {
            out = DIGITS[(0xF0 & data) >>> 4];
            out = DIGITS];
      }
      return out;
    }

z1111 发表于 2024-5-28 15:05:41

本帖最后由 z1111 于 2024-5-28 15:59 编辑

Ben.C 发表于 2024-5-28 14:55
看代码第60行,如果你这个命令所创建的变量想在当前服务端命令列表中都可以使用。
直接在当前上下文的参数 ...
好了,感谢

z1111 发表于 2024-5-28 16:00:48

Nathan.guo 发表于 2024-5-29 13:42:46

{:5_117:}
页: [1]
查看完整版本: 【10.0】【请问如何接收java服务端开发的返回结果】