From e8f1c2aebfa6ec44a18f5264e2d962fa4ac3733b Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Mon, 24 Jul 2023 09:59:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hfkj/schedule/SaasSchedule.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 schedule/src/main/java/com/hfkj/schedule/SaasSchedule.java 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失败!"); + } + } +}