You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.9 KiB
Java

package com.bsd.say.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
5 years ago
import com.bsd.say.config.RedisProperies;
import com.bsd.say.service.WxOpenServiceDemo;
import com.bsd.say.util.HttpRequestUtils;
5 years ago
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
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;
5 years ago
import redis.clients.jedis.JedisPool;
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;
5 years ago
private static JedisPool pool;
@Resource
private RedisTemplate redisTemplate;
/**
* accessToken
*/
public void refreshComponentAccessToken(){
5 years ago
// Map<String, String> reMap;
try {
// 核心定时器,每一个小时执行一次
5 years ago
WxOpenInRedisConfigStorage inRedisConfigStorage = new WxOpenInRedisConfigStorage(getJedisPool());
String ComponentVerifyTicket = inRedisConfigStorage.getComponentVerifyTicket();
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();
5 years ago
System.out.println("accessToken"+accessToken);
// 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;
}
5 years ago
private JedisPool getJedisPool() {
RedisProperies redisProperies = new RedisProperies();
if (pool == null) {
synchronized (WxOpenServiceDemo.class) {
if (pool == null) {
pool = new JedisPool(redisProperies, redisProperies.getHost(),
redisProperies.getPort(), redisProperies.getConnectionTimeout(),
redisProperies.getSoTimeout(), redisProperies.getPassword(),
redisProperies.getDatabase(), redisProperies.getClientName(),
redisProperies.isSsl(), redisProperies.getSslSocketFactory(),
redisProperies.getSslParameters(), redisProperies.getHostnameVerifier());
}
}
}
return pool;
}
}