BE_Invoke_PostInvoker_DYLK.groovy 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import com.dderp.common.api.BusinessExecutor
  2. import com.dderp.common.api.flycat.PlatformAccessTokenService
  3. import com.dderp.common.datas.ERPModule
  4. import com.dderp.common.entity.base.ProcessMapItem
  5. import com.dderp.common.entity.base.ProcessStringItem
  6. import com.dderp.common.entity.base.ScriptMapResult
  7. import com.dderp.common.entity.base.ScriptStringResult
  8. import com.dderp.common.entity.site.ERPTokenUser
  9. import com.dderp.common.http.HttpTools
  10. import com.sweetfish.convert.json.JsonConvert
  11. import com.sweetfish.service.RetResult
  12. import groovy.json.JsonSlurper
  13. import org.apache.logging.log4j.LogManager
  14. import org.apache.logging.log4j.Logger
  15. import org.rex.RMap
  16. import javax.annotation.Resource
  17. import java.nio.charset.StandardCharsets
  18. import java.time.LocalDateTime
  19. import java.time.ZoneOffset
  20. import java.util.concurrent.ExecutionException
  21. import java.util.concurrent.TimeUnit
  22. import java.util.concurrent.TimeoutException
  23. @SuppressWarnings("unused")
  24. class BE_Invoke_PostInvoker_DYLK implements BusinessExecutor<ProcessMapItem, ScriptStringResult> {
  25. private final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
  26. private Map<String, String> platPropertiesMap = new HashMap<>()
  27. @Resource(name = "APP_HOME")
  28. String homePath
  29. @Resource
  30. JsonConvert jsonConvert
  31. @Resource
  32. PlatformAccessTokenService tokenService
  33. @Override
  34. String scriptName() {
  35. return "[抖音来客]通用post请求"
  36. }
  37. @Override
  38. ERPModule module() {
  39. return ERPModule.PLATFORM_TOKEN
  40. }
  41. @Override
  42. void start(long supplierCode) {
  43. //取到该平台的properties
  44. //这地方严格来说是有问题的,supplierCode的区分没有加进去,做得好的话应该是properties文件用supplierCode区分开
  45. //各平台默认需要以下几个参数
  46. //appId
  47. //appSecret
  48. //reqUrl 接口请求地址
  49. //storeBindUrl 生成门店绑定url时候的请求地址
  50. final File df = new File(homePath, "/conf/platformProperty/DYLK.properties");
  51. if (df.isFile()) {
  52. try {
  53. Properties ps = new Properties();
  54. InputStream ins = new FileInputStream(df);
  55. ps.load(new InputStreamReader(ins, StandardCharsets.UTF_8));
  56. ins.close();
  57. ps.forEach((x, y) ->
  58. platPropertiesMap.putIfAbsent((String) x, (String) y)
  59. );
  60. } catch (IOException e) {
  61. logger.error(e.getMessage(), e);
  62. }
  63. }
  64. }
  65. RetResult<ScriptStringResult> execute(ProcessMapItem source) {
  66. //秒级时间戳,groovy里面不让用system
  67. long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8))
  68. RMap params = source.itemData
  69. String dataSourceId = source.dataSourceId
  70. long supplierCode = source.supplierCode
  71. ERPTokenUser currentUser = source.currentUser
  72. Map<String, String> header = new HashMap<>();
  73. header.put("access-token", tokenService.getAccessToken("DYLK", dataSourceId, supplierCode as String));
  74. header.put("log_id", String.valueOf(new Date().getTime()));
  75. header.put("Content-Type", "application/json");
  76. String resp
  77. try {
  78. resp = HttpTools.postHttpContentAsync(platPropertiesMap.get("reqUrl") +
  79. params.getString("subUrl"), header, params.getString("body")).get(20, TimeUnit.SECONDS)
  80. ScriptStringResult stringResult = new ScriptStringResult()
  81. stringResult.setResultStr(resp)
  82. return RetResult.<ScriptStringResult> successT().result(stringResult)
  83. } catch (InterruptedException | ExecutionException | TimeoutException e) {
  84. logger.error("请求抖音来客接口[" + params.getString("subUrl") + "]失败", e);
  85. }
  86. return RetResult.errorT();
  87. }
  88. }