当前位置:   article > 正文

宜搭,高级认证实操2,FaaS连接器指导教程_宜搭faas连接器 恭喜您, 成功调用宜搭fass连接器!

宜搭faas连接器 恭喜您, 成功调用宜搭fass连接器!

连接器工厂-->新建FaaS连接器

保存-->前往云IDE开发

等待代码加载完毕

如图所示在util下新建EDSUtil.java代码如下

  1. package com.alibaba.work.faas.util;
  2. import javax.crypto.SecretKeyFactory;
  3. import javax.crypto.spec.DESKeySpec;
  4. import com.aliyuncs.utils.StringUtils;
  5. import java.security.SecureRandom;
  6. import org.apache.commons.codec.binary.Base64;
  7. import javax.crypto.SecretKey;
  8. import java.io.IOException;
  9. import javax.crypto.Cipher;
  10. public class DESUtil {
  11. private final static String DES = "DES";
  12. public static void main(String[] args) {
  13. String data = "XXX";
  14. String key = "yida12#$";
  15. String encryptStr = encrypt(data, key);
  16. System.out.println("加密后的字符串:" + encryptStr);
  17. String decryptStr = decrypt(encryptStr, key);
  18. System.out.println("解密后的字符串:" + decryptStr);
  19. }
  20. /**
  21. * Description 根据键值进行加密
  22. *
  23. * @param data
  24. * @param key 加密键byte数组
  25. * @return
  26. * @throws Exception
  27. */
  28. public static String encrypt(String data, String key) {
  29. if (!StringUtils.isEmpty(data) && !StringUtils.isEmpty(key)) {
  30. try {
  31. byte[] bt = encrypt(data.getBytes(), key.getBytes());
  32. String strs = Base64.encodeBase64String(bt);
  33. return strs;
  34. } catch (Exception var4) {
  35. System.out.println("加密遇到错误:");
  36. var4.printStackTrace();
  37. return null;
  38. }
  39. } else {
  40. System.out.println("入参为空,data:" + data + ",key=" + key);
  41. return null;
  42. }
  43. }
  44. /**
  45. * Description 根据键值进行解密
  46. *
  47. * @param data
  48. * @param key 加密键byte数组
  49. * @return
  50. * @throws IOException
  51. * @throws Exception
  52. */
  53. public static String decrypt(String data, String key) {
  54. if (!StringUtils.isEmpty(data) && !StringUtils.isEmpty(key)) {
  55. try {
  56. byte[] dataNew = Base64.decodeBase64(data);
  57. byte[] bt = decrypt(dataNew, key.getBytes());
  58. return new String(bt);
  59. } catch (Exception var4) {
  60. System.out.println("解密遇到错误:");
  61. var4.printStackTrace();
  62. return null;
  63. }
  64. } else {
  65. System.out.println("入参为空,data:" + data + ",key=" + key);
  66. return null;
  67. }
  68. }
  69. /**
  70. * Description 根据键值进行加密
  71. *
  72. * @param data
  73. * @param key 加密键byte数组
  74. * @return
  75. * @throws Exception
  76. */
  77. private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
  78. // 生成一个可信任的随机数源
  79. SecureRandom sr = new SecureRandom();
  80. // 从原始密钥数据创建DESKeySpec对象
  81. DESKeySpec dks = new DESKeySpec(key);
  82. // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
  83. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
  84. SecretKey securekey = keyFactory.generateSecret(dks);
  85. // Cipher对象实际完成加密操作
  86. Cipher cipher = Cipher.getInstance(DES);
  87. // 用密钥初始化Cipher对象
  88. //cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
  89. cipher.init(1, securekey, sr);
  90. return cipher.doFinal(data);
  91. }
  92. /**
  93. * Description 根据键值进行解密
  94. *
  95. * @param data
  96. * @param key 加密键byte数组
  97. * @return
  98. * @throws Exception
  99. */
  100. private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
  101. // 生成一个可信任的随机数源
  102. SecureRandom sr = new SecureRandom();
  103. // 从原始密钥数据创建DESKeySpec对象
  104. DESKeySpec dks = new DESKeySpec(key);
  105. // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
  106. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
  107. SecretKey securekey = keyFactory.generateSecret(dks);
  108. // Cipher对象实际完成解密操作
  109. Cipher cipher = Cipher.getInstance(DES);
  110. // 用密钥初始化Cipher对象
  111. //cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
  112. cipher.init(2, securekey, sr);
  113. return cipher.doFinal(data);
  114. }
  115. }

找到FaasEntry.java将如下代码整体替换,注意引包

  1. package com.alibaba.work.faas;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Objects;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.alibaba.work.faas.common.AbstractEntry;
  11. import com.alibaba.work.faas.common.FaasInputs;
  12. import com.alibaba.work.faas.util.DESUtil;
  13. import com.alibaba.work.faas.util.DingOpenApiUtil;
  14. import com.alibaba.work.faas.util.YidaConnectorUtil;
  15. import com.alibaba.work.faas.util.YidaConnectorUtil.ConnectorTypeEnum;
  16. import com.aliyun.dingtalkyida_1_0.models.BatchSaveFormDataRequest;
  17. import org.apache.commons.lang3.StringUtils;
  18. /**
  19. * 您的业务逻辑请从此类开始
  20. *
  21. * @Date 2023/09/21 11:16 AM
  22. * @Description 宜搭Faas连接器函数入口,FC handler:com.alibaba.work.faas.FaasEntry::handleRequest
  23. * @Version 2.0
  24. * @Author bufan
  25. *
  26. **/
  27. public class FaasEntry extends AbstractEntry {
  28. public FaasEntry() {}
  29. @Override
  30. public JSONObject execute(FaasInputs faasInputs) {
  31. System.out.println("faasInputs: " + JSON.toJSONString(faasInputs));
  32. //填充宜搭工具类的上下文, 必须调用此方法!!! 请不要删除
  33. initYidaUtil(faasInputs);
  34. //登录态里的钉钉userId
  35. String userId = (String) faasInputs.getYidaContext().get("userId");
  36. //登录态里的钉钉corpId
  37. String corpId = (String) faasInputs.getYidaContext().get("corpId");
  38. //业务传的实际入参(如果您配置了参数映射(也就是点击了连接器工厂里的解析Body按钮并配置了各个参数描述和映射), 那么就是业务实际参数经过参数映射处理后的值)
  39. Map<String,Object> input = faasInputs.getInputs();
  40. /**
  41. * 示例1, 在doYourBusiness方法里编写您的业务代码, 也可以将业务代码封装到其他Class源文件或方法里, cloudIDE和您的本地IDE基本无区别。
  42. */
  43. JSONObject result = new JSONObject();
  44. try {
  45. Object businessResult = doYourBusiness(input);
  46. result.put("success",true);
  47. result.put("result",businessResult);
  48. result.put("error","");
  49. return result;
  50. } catch (Exception e) {
  51. result.put("success",false);
  52. result.put("result",null);
  53. result.put("error",e.getMessage());
  54. return result;
  55. }
  56. /**
  57. * 返回的JSONObject并不是一定要带success、result、error, 下面的代码只是示例, 具体返回哪些key-value由您自己决定, 尽量与您在宜搭连接器工厂里配置的出参结构保持一致即可
  58. */
  59. // JSONObject result = new JSONObject();
  60. // result.put("success",true);
  61. // result.put("result","恭喜您, 成功调用宜搭FASS连接器!");
  62. // result.put("error","");
  63. // return result;
  64. }
  65. /**
  66. * 将相关参数存到宜搭工具类里, 必须要调用此方法!!! 请不要删除!!!
  67. *
  68. * @param faasInputs
  69. */
  70. private static final void initYidaUtil(FaasInputs faasInputs){
  71. /**
  72. * 用于调用钉钉开放平台OpenAPI的accessToken, 宜搭提供的, 仅申请了钉钉开放平台的部分OpenAPI的调用权限
  73. * 如果此accessToken不满足您的需求,可在钉钉开放平台创建您自己的钉钉应用并获取appKey和APPSecret并使用com.alibaba.work.faas.util.DingOpenApiUtil获取您自己的customAccessToken
  74. *
  75. * @see com.alibaba.work.faas.util.DingOpenApiUtil#getCustomAccessTokenThenCache(String,String)
  76. */
  77. String accessToken = (String) faasInputs.getYidaContext().get("accessToken");
  78. // 设置钉开放平台访问token, 后续无需再设置
  79. DingOpenApiUtil.setAccessToken(accessToken);
  80. // try {
  81. // //调用该方法就会自动存储customAccessToken, 之后请不要调用DingOpenApiUtil.setAccessToken(accessToken)将返回的customAccessToken覆盖宜搭传入的accessToken;
  82. // DingOpenApiUtil.getCustomAccessTokenThenCache("您的钉钉应用appKey", "您的钉钉应用appSecret");
  83. // } catch (Exception e) {
  84. // System.out.println("getCustomAccessTokenThenCache error:"+e.getMessage());
  85. // }
  86. /**
  87. *调用宜搭连接器依赖consumeCode
  88. */
  89. String consumeCode = (String)faasInputs.getYidaContext().get("consumeCode");
  90. //设置连接器消费码, 后续无需再设置
  91. YidaConnectorUtil.setConsumeCode(consumeCode);
  92. }
  93. private Object doYourBusiness(Map<String,Object> input) throws Exception{
  94. String content = (String)input.get("content");
  95. String password = (String)input.get("password");
  96. Integer type = Integer.parseInt(String.valueOf(input.get("type")));
  97. /**
  98. *在这里编写您的业务代码, 也可以将业务代码封装到其他类或方法里.
  99. */
  100. JSONObject result = new JSONObject();
  101. result.put("success",false);
  102. result.put("result","");
  103. result.put("error","");
  104. String encryptContent;
  105. if (0 == type) {
  106. /**
  107. * 加密
  108. */
  109. encryptContent = DESUtil.encrypt(content, password);
  110. System.out.println("加密后的字符串123:" + encryptContent);
  111. if (StringUtils.isEmpty(encryptContent)) {
  112. result.put("error", "empty string got!");
  113. return DESUtil.encrypt(content, password);
  114. }
  115. result.put("result", encryptContent);
  116. result.put("success", true);
  117. }
  118. else {
  119. /**
  120. * 解密
  121. */
  122. encryptContent = DESUtil.decrypt(content, password);
  123. System.out.println("解密后的字符串:" + encryptContent);
  124. if (StringUtils.isEmpty(encryptContent)) {
  125. result.put("error", "empty string got!");
  126. return result;
  127. }
  128. result.put("result", encryptContent);
  129. result.put("success", true);
  130. }
  131. System.out.println("返回:" + JSON.toJSONString(result));
  132. return result.get("result");
  133. }
  134. public static void main(String[] args) {
  135. Map<String, Object> m = new HashMap();
  136. m.put("content", "xxxx");
  137. m.put("password", "YYYYYYYY");
  138. m.put("type", 0);
  139. // //取实际的入参
  140. // String param1 = (String)input.get("参数1");
  141. // String param2 = (String)input.get("参数2");
  142. // String paramN = (String)input.get("参数N");
  143. // //业务处理
  144. // return "doYourBusiness成功";
  145. }
  146. private Object invokeYidaConnector(FaasInputs faasInputs) throws Exception {
  147. Map<String, Object> input = faasInputs.getInputs();
  148. String userId = (String)faasInputs.getYidaContext().get("userId");
  149. String corpId = (String)faasInputs.getYidaContext().get("corpId");
  150. JSONObject connectorActionInputs = new JSONObject();
  151. connectorActionInputs.put("unionId", Arrays.asList(userId));
  152. connectorActionInputs.put("subject", (String)input.get("title"));
  153. connectorActionInputs.put("creatorId", Arrays.asList(userId));
  154. connectorActionInputs.put("description", "Faas连接器里调用钉钉官方连接器创建待办");
  155. connectorActionInputs.put("dueTime", System.currentTimeMillis() + 600000L);
  156. connectorActionInputs.put("priority", 10L);
  157. YidaConnectorUtil.YidaResponse response = YidaConnectorUtil.invokeService("G-CONN-1016B8AEBED50B01B8D00009", "G-ACT-1016B8B1911A0B01B8D0000I", ConnectorTypeEnum.DING_INNER_CONNECTOR, (Long)null, connectorActionInputs);
  158. if (Objects.nonNull(response) && response.isSuccess()) {
  159. try {
  160. return YidaConnectorUtil.extractYidaConnectorExecutionResult(response);
  161. } catch (Exception var8) {
  162. throw new Exception(var8.getMessage());
  163. }
  164. } else {
  165. throw new Exception(Objects.isNull(response) ? "执行宜搭连接器失败" : response.getErrorCode() + " " + response.getErrorMsg());
  166. }
  167. }
  168. }

部署

回到连接器界面点击右上角测试-->测试

返回如图所示代码,部署成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/449796
推荐阅读
相关标签
  

闽ICP备14008679号