package cc.mrbird.febs.mall.quartz; import cc.mrbird.febs.common.properties.XcxProperties; import cc.mrbird.febs.common.utils.HttpCurlUtil; import cc.mrbird.febs.common.utils.RedisUtils; import cc.mrbird.febs.common.utils.SpringContextHolder; import cc.mrbird.febs.pay.util.WechatConfigure; import cn.hutool.core.util.StrUtil; 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 java.io.IOException; import java.util.HashMap; import java.util.Map; @Slf4j @Component public class WxxcxJob { private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class); @Autowired private RedisUtils redisUtils; /** * 获取access_token * 有效期两小时 */ @Scheduled(cron = "0 0/5 * * * ? ") public void getAccessToken() throws IOException { String appId = xcxProperties.getXcxAppid(); String appSecret = xcxProperties.getXcxSecret(); Map 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); String accessTokenKey = WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY; JSONObject jsonObject = JSONObject.parseObject(result); String accessToken = jsonObject.getString(accessTokenKey); if (StrUtil.isEmpty(accessToken)) { log.error("获取access token失败: {}" , jsonObject.getString("errmsg")); throw new ApiException("获取access token失败"); } else { log.info("wx access_token : {}",accessToken); redisUtils.set(accessTokenKey,accessToken); } } }