微信获取unionId

master
ck 5 years ago
parent 38119363c7
commit af2a143e2e

@ -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();
}
}

@ -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<String, String> 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<String, String> 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;
}
}

@ -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

Loading…
Cancel
Save