From af2a143e2eeee993e898ec18e384c95aeb879b53 Mon Sep 17 00:00:00 2001 From: ck <851316342@qq.com> Date: Thu, 30 Jul 2020 23:10:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E8=8E=B7=E5=8F=96unionId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bsd/say/quartz/ScheduledTask.java | 33 +++++++ .../bsd/say/service/impl/WeixinService.java | 91 +++++++++++++++++++ src/main/resources/application.yml | 8 ++ 3 files changed, 132 insertions(+) create mode 100644 src/main/java/com/bsd/say/quartz/ScheduledTask.java create mode 100644 src/main/java/com/bsd/say/service/impl/WeixinService.java diff --git a/src/main/java/com/bsd/say/quartz/ScheduledTask.java b/src/main/java/com/bsd/say/quartz/ScheduledTask.java new file mode 100644 index 0000000..433d729 --- /dev/null +++ b/src/main/java/com/bsd/say/quartz/ScheduledTask.java @@ -0,0 +1,33 @@ +package com.bsd.say.quartz; + +import com.bsd.say.service.impl.WeixinService; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.io.IOException; + +/** + * 定时任务类 + */ +@Component +public class ScheduledTask { + + @Resource + private WeixinService weixinService; + /** + * 一个小时刷新一次第三方access_token + */ + private static final String REFRESH_COMPONENT_TOKEN_TASK = "0 0 0/1 * * ?"; + + + /** + * + * @throws IOException + */ + @Scheduled(cron = REFRESH_COMPONENT_TOKEN_TASK) + public void downloadOrderTaskAM(){ + weixinService.refreshComponentAccessToken(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/bsd/say/service/impl/WeixinService.java b/src/main/java/com/bsd/say/service/impl/WeixinService.java new file mode 100644 index 0000000..2d4e97a --- /dev/null +++ b/src/main/java/com/bsd/say/service/impl/WeixinService.java @@ -0,0 +1,91 @@ +package com.bsd.say.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.bsd.say.util.HttpRequestUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Service +public class WeixinService { + @Value("${wechat.appId}") + private String appId; + @Value("${wechat.componentAppId}") + private String componentAppId; + @Value("${wechat.componentAppSecret}") + private String componentAppSecret; + @Value("${wechat.getComponentAccessTokenUrl}") + private String getComponentAccessTokenUrl; + @Value("${wechat.getAccessTokenUrl}") + private String getAccessTokenUrl; + @Value("${wechat.getUnionIdUrl}") + private String getUnionIdUrl; + @Resource + private RedisTemplate redisTemplate; + + /** + * 刷新第三方accessToken + */ + public void refreshComponentAccessToken(){ + Map reMap; + try { + // 核心定时器,每一个小时执行一次 + String ComponentVerifyTicket = redisTemplate.opsForValue().get("component_verify_ticket").toString(); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("component_appid", componentAppId); + jsonObject.put("component_appsecret", componentAppSecret); + jsonObject.put("component_verify_ticket", ComponentVerifyTicket); + String post = HttpRequestUtils.sendPost(getComponentAccessTokenUrl,jsonObject); +// logger.debug("====================返回post结果:" + post); + HashMap hashMap = JSON.parseObject(post, HashMap.class); + String componentAccessToken = hashMap.get("component_access_token"); + if (StringUtils.isNotEmpty(componentAccessToken)) { + redisTemplate.opsForValue().set("component_access_token", componentAccessToken, 60 * 60 * 2, TimeUnit.SECONDS); + String accessToken = redisTemplate.opsForValue().get("component_access_token").toString(); +// logger.debug("====================令牌component_access_token】:【" + accessToken + "】===================="); + } else { + throw new RuntimeException("微信开放平台,第三方平台获取【令牌】失败"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 获取accessToken + * @return + */ + public JSONObject getAccessToken(String code){ + String component_access_token = redisTemplate.opsForValue().get("component_access_token").toString(); + String param = appId + "&code=" + code + "&grant_type=authorization_code&component_appid=" + componentAppId + + "&component_access_token=" + component_access_token; + String url = getAccessTokenUrl + param; + String result = HttpRequestUtils.sendGet(url); + JSONObject resultJson = JSONObject.parseObject(result); + return resultJson; + } + + /** + * 获取unionId + * @param code + * @return + */ + public String getUnionId(String code){ + JSONObject jsonObject = getAccessToken(code); + String accessToken = jsonObject.getString("access_token"); + String openid = jsonObject.getString("openid"); + String param = accessToken + "&openid=" + openid + "&lang=zh_CN"; + String url = getUnionIdUrl + param; + String result = HttpRequestUtils.sendGet(url); + JSONObject resultJson = JSONObject.parseObject(result); + String unionId = resultJson.getString("unionid"); + return unionId; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f481190..4ea6677 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -43,6 +43,14 @@ server: award: rule: 5 +wechat: + appId: + componentAppId: + componentAppSecret: + getComponentAccessTokenUrl: https://api.weixin.qq.com/cgi-bin/component/api_component_token + getAccessTokenUrl: https://api.weixin.qq.com/sns/oauth2/component/access_token?appid= + getUnionIdUrl: https://api.weixin.qq.com/sns/userinfo?access_token= + bsd: tokenkey: BOSIDENG sendSource: BRAND_VERIFY_CODE