From 24e986fa22f7e1b94c0b2197fb1b62ae51023cef Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Wed, 10 Aug 2022 17:18:50 +0800 Subject: [PATCH] 20220810 --- src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java | 24 +++++++++++++++--------- 1 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java b/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java index a301c09..8c587d6 100644 --- a/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java +++ b/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java @@ -6,13 +6,16 @@ import cc.mrbird.febs.common.utils.SpringContextHolder; import cc.mrbird.febs.pay.util.WechatConfigure; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.extension.exceptions.ApiException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import javax.annotation.Resource; import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -25,26 +28,29 @@ @Autowired private RedisUtils redisUtils; + @Resource + RestTemplate restTemplate; /** * 获取access_token * 有效期两小时 */ @Scheduled(cron = "0 0/5 * * * ? ") public void getAccessToken() throws IOException { - String appId = xcxProperties.getXcxAppid(); String appSecret = xcxProperties.getXcxSecret(); + String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret; + String jsonStr = restTemplate.getForObject(url, String.class); - Map<String,String> params = new HashMap<>(); - params.put("grant_type","client_credential"); - params.put("appid",appId); - params.put("secret",appSecret); - - //发送请求 - String result = HttpCurlUtil.sendGetHttp(WechatConfigure.WX_ACCESS_TOKEN , params); + /** + * 返回结果 + * {"access_token":"ACCESS_TOKEN","expires_in":7200} + */ + if (!jsonStr.contains("access_token")) { + System.out.println("获取微信access_token失败"); + } String accessTokenKey = WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY; - JSONObject jsonObject = JSONObject.parseObject(result); + JSONObject jsonObject = JSONObject.parseObject(jsonStr); String accessToken = jsonObject.getString(accessTokenKey); if (StrUtil.isEmpty(accessToken)) { log.error("获取access token失败: {}" , jsonObject.getString("errmsg")); -- Gitblit v1.9.1