diff --git a/schedule/src/main/java/com/hfkj/schedule/SaasSchedule.java b/schedule/src/main/java/com/hfkj/schedule/SaasSchedule.java new file mode 100644 index 0000000..3e515c4 --- /dev/null +++ b/schedule/src/main/java/com/hfkj/schedule/SaasSchedule.java @@ -0,0 +1,86 @@ +package com.hfkj.schedule; + +import com.alibaba.fastjson.JSONObject; +import com.hfkj.channel.saas.config.SaasConfig; +import com.hfkj.channel.saas.utils.SaasUtils; +import com.hfkj.common.Base64Util; +import com.hfkj.common.utils.HttpsUtils; +import com.hfkj.common.utils.RedisUtil; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.Scheduled; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +/** + * @className: SaasSchedule + * @author: HuRui + * @date: 2023/7/20 + **/ +@Configuration +public class SaasSchedule { + + private static final Logger log = LoggerFactory.getLogger(SaasSchedule.class); + + @Resource + private RedisUtil redisUtil; + + @Scheduled(cron = "0 0/60 * * * ?") + public void getSaasToken() { + try { + Map param = new HashMap<>(); + param.put("grant_type", "client_credentials"); + param.put("client_id", SaasConfig.client_id); + param.put("client_secret", SaasConfig.client_secret); + + Map header = new HashMap<>(); + header.put("Authorization", "Basic " + Base64Util.encode(SaasConfig.client_id+":"+SaasConfig.client_secret)); + + // 请求参数 + String requestParam = SaasUtils.paramSort(param); + + log.info("==============start 获取token start =============="); + log.info("请求header:" + JSONObject.toJSONString(header)); + log.info("请求参数:" + requestParam); + JSONObject jsonObject = HttpsUtils.doPostForm(SaasConfig.requestMerRegistrationTokenUrl, requestParam, header); + log.info("响应参数:" + jsonObject); + if (StringUtils.isNotBlank(jsonObject.getString("access_token"))) { + redisUtil.set("SAAS_TOKEN", jsonObject.getString("access_token"), jsonObject.getLong("expires_in")); + } + } catch (Exception e) { + log.info("获取SaasToken失败!"); + } + } + + @Scheduled(cron = "0 0/60 * * * ?") + public void getSaasUpdateToken() { + try { + Map param = new HashMap<>(); + param.put("grant_type", "password"); + param.put("username", SaasConfig.username); + param.put("password", SaasConfig.password); + param.put("scope", "all"); + + Map header = new HashMap<>(); + header.put("Authorization", "Basic " + Base64Util.encode(SaasConfig.client_id+":"+SaasConfig.client_secret)); + + // 请求参数 + String requestParam = SaasUtils.paramSort(param); + + log.info("==============start 获取token start =============="); + log.info("请求header:" + JSONObject.toJSONString(header)); + log.info("请求参数:" + requestParam); + JSONObject jsonObject = HttpsUtils.doPostForm(SaasConfig.requestMerRegistrationTokenUrl, requestParam, header); + log.info("响应参数:" + jsonObject); + if (StringUtils.isNotBlank(jsonObject.getString("access_token"))) { + redisUtil.set("SAAS_UPDATE_TOKEN", jsonObject.getString("access_token"), jsonObject.getLong("expires_in")); + } + } catch (Exception e) { + log.info("获取SaasUpdateToken失败!"); + } + } +}