|
|
|
|
@ -1,8 +1,13 @@
|
|
|
|
|
package com.flossom.miniProgram.utils;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.flossom.common.core.constant.CacheConstants;
|
|
|
|
|
import com.flossom.common.core.domain.entity.WxParameterSetting;
|
|
|
|
|
import com.flossom.common.core.mapper.WxParameterSettingMapper;
|
|
|
|
|
import com.flossom.common.core.utils.StringUtils;
|
|
|
|
|
import com.flossom.common.redis.service.RedisService;
|
|
|
|
|
import com.flossom.miniProgram.config.properties.WxConfig;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.WxAccessTokenRet;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
@ -10,6 +15,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 小程序工具类
|
|
|
|
|
@ -17,10 +23,14 @@ import java.util.Map;
|
|
|
|
|
@Component
|
|
|
|
|
public class MiniProgramUtils {
|
|
|
|
|
|
|
|
|
|
protected final static Logger logger = LoggerFactory.getLogger(MiniProgramUtils.class);
|
|
|
|
|
|
|
|
|
|
private static WxConfig wxConfig;
|
|
|
|
|
|
|
|
|
|
private static WxParameterSettingMapper wxParameterSettingMapper;
|
|
|
|
|
|
|
|
|
|
private static RedisService redisService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注入 微信配置mapper,获取 appId,appKey(该配置在数据库)
|
|
|
|
|
*
|
|
|
|
|
@ -31,6 +41,11 @@ public class MiniProgramUtils {
|
|
|
|
|
MiniProgramUtils.wxParameterSettingMapper = wxParameterSettingMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
public void setRedisService(RedisService redisService) {
|
|
|
|
|
MiniProgramUtils.redisService = redisService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注入 微信配置类,读取请求路径(该配置在 nacos)
|
|
|
|
|
*
|
|
|
|
|
@ -95,12 +110,32 @@ public class MiniProgramUtils {
|
|
|
|
|
return HttpClientUtils.postParameters(wxConfig.getObtainPhoneUrl() + "?access_token=" + accessToken, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getAccessToken() throws Exception {
|
|
|
|
|
Map<String, String> params = new HashMap();
|
|
|
|
|
WxParameterSetting wxParameterSetting = wxParameterSettingMapper.selectWxParameterSettingById(1L);
|
|
|
|
|
params.put("appid", wxParameterSetting.getAppid());
|
|
|
|
|
params.put("secret", wxParameterSetting.getAppkey());
|
|
|
|
|
params.put("grant_type", "client_credential");
|
|
|
|
|
return HttpClientUtils.getParameters(wxConfig.getAccessTokenUrl(), params);
|
|
|
|
|
public static String getAccessToken(String openid) throws Exception {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO: 将 access_token 保存在 redis,官方定义说 access_token 的有效期暂定为 2个小时,使用注意事项:
|
|
|
|
|
* 1、从 redis 获取 access_token,存在则未过期,不存在则过期了,重新获取 access_token
|
|
|
|
|
*/
|
|
|
|
|
String accessToken = redisService.getCacheObject(CacheConstants.WX_ACCESS_TOKEN_CACHE + openid);
|
|
|
|
|
if (StringUtils.isBlank(accessToken)) {
|
|
|
|
|
// 获取 access_token
|
|
|
|
|
Map<String, String> params = new HashMap();
|
|
|
|
|
WxParameterSetting wxParameterSetting = wxParameterSettingMapper.selectWxParameterSettingById(1L);
|
|
|
|
|
params.put("appid", wxParameterSetting.getAppid());
|
|
|
|
|
params.put("secret", wxParameterSetting.getAppkey());
|
|
|
|
|
params.put("grant_type", "client_credential");
|
|
|
|
|
String result = HttpClientUtils.getParameters(wxConfig.getAccessTokenUrl(), params);
|
|
|
|
|
logger.info("请求微信服务器获取access_token返回结果:{}", result);
|
|
|
|
|
WxAccessTokenRet wxAccessTokenRet = JSON.parseObject(result, WxAccessTokenRet.class);
|
|
|
|
|
if (wxAccessTokenRet.getErrcode() != null) {
|
|
|
|
|
logger.error("获取 access_token 失败: {}", wxAccessTokenRet.getErrmsg());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 保存缓存
|
|
|
|
|
redisService.setCacheObject(CacheConstants.WX_ACCESS_TOKEN_CACHE + openid,
|
|
|
|
|
wxAccessTokenRet.getAccess_token(), CacheConstants.WX_ACCESS_TOKEN_EXPIRATION, TimeUnit.MINUTES);
|
|
|
|
|
return wxAccessTokenRet.getAccess_token();
|
|
|
|
|
}
|
|
|
|
|
return accessToken;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|