微信 获取token
This commit is contained in:
@@ -1,17 +1,13 @@
|
||||
package com.czg.service.account.util;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.czg.constants.ParamCodeCst;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.system.service.SysParamsService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -26,9 +22,7 @@ import java.util.Map;
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AcAccountUtil {
|
||||
@Resource
|
||||
@Lazy
|
||||
private RedisService redisService;
|
||||
|
||||
@DubboReference
|
||||
private SysParamsService paramsService;
|
||||
|
||||
@@ -54,7 +48,7 @@ public class AcAccountUtil {
|
||||
String accessToken = getAccessToken();
|
||||
// String accessToken = "97_HZVThQrtvOiCZGrr23ZHN0cVpHBJHc18RSFHU6dvkQDMAme4GsG0NU-Dax1HP5Wx-aGa1l35KaqiMVv61TCj0Qk8DK1LC6kQ8uKLDfgRYVJjX3QjcelmIjp4PCkERBeABAUHR";
|
||||
if (StrUtil.isBlank(accessToken)) {
|
||||
log.error("获取 access_token 失败");
|
||||
log.error("银收客czg 公众号获取 access_token 失败");
|
||||
return "";
|
||||
}
|
||||
String bodyJson = "{\"action_info\":{\"scene\":{\"scene_str\":\"" + userId + "\"}},\"action_name\":\"QR_STR_SCENE\",\"expire_seconds\":\"2592000\"}";
|
||||
@@ -87,23 +81,24 @@ public class AcAccountUtil {
|
||||
|
||||
|
||||
public String getAccessToken() {
|
||||
String accessToken = Convert.toStr(redisService.get("wx:ac:AccessToken"));
|
||||
if (StrUtil.isNotEmpty(accessToken)) {
|
||||
return accessToken;
|
||||
}
|
||||
Map<String, String> userAc = paramsService.getParamsByMap("user_ac_key_set", ParamCodeCst.USER_AC_KEY_SET);
|
||||
// 用户小程序参数
|
||||
String acAppId = userAc.get(ParamCodeCst.Wechat.Ac.USER_WX_AC_APP_ID);
|
||||
String acSecrete = userAc.get(ParamCodeCst.Wechat.Ac.USER_WX_AC_SECRETE);
|
||||
String resp = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", acAppId, acSecrete));
|
||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||
if (!respInfo.containsKey("access_token")) {
|
||||
log.warn("公众号获取token失败, 响应内容: {}", resp);
|
||||
return "";
|
||||
|
||||
String resp = HttpUtil.get(StrUtil.format("https://access-token.sxczgkj.com/accessToken?appId={}&appSecret={}", acAppId, acSecrete));
|
||||
// 响应 {"accessToken":"100_6C_jltHANT1y2Fot5PXKFDzPXTyWumCsao0oMoNRvJUTuxS0IOVO4nBmjdmx5dZfYItShFVSAKYzNDf7ZGLPlx52ii1Y1qerrbbSmIiLWCrec5qjBY4gV5Tfv8YKKTdABAEEN","appId":"wx212769170d2c6b2a"}
|
||||
if (StrUtil.isNotBlank(resp)) {
|
||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||
return respInfo.getString("accessToken");
|
||||
} else {
|
||||
String resp2 = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", acAppId, acSecrete));
|
||||
JSONObject respInfo = JSONObject.parseObject(resp2);
|
||||
if (!respInfo.containsKey("access_token")) {
|
||||
log.warn("银收客czg 公众号获取token失败, 响应内容: {}", resp2);
|
||||
return "";
|
||||
}
|
||||
return respInfo.getString("access_token");
|
||||
}
|
||||
accessToken = respInfo.getString("access_token");
|
||||
int expiresIn = respInfo.getInteger("expires_in");
|
||||
redisService.set("wx:ac:AccessToken", accessToken, expiresIn - 10);
|
||||
return accessToken;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.czg.service.account.util;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
@@ -11,6 +10,7 @@ import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.config.RedisCst;
|
||||
import com.czg.constants.ParamCodeCst;
|
||||
import com.czg.exception.CzgException;
|
||||
import com.czg.resp.CzgResult;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.system.dto.SysParamsDTO;
|
||||
@@ -140,7 +140,7 @@ public class WechatMiniMsgUtil {
|
||||
|
||||
public String getAccountOpenId(String code) {
|
||||
Map<String, String> shopAc = sysParamsService.getParamsByMap("shop_ac_key_set", ParamCodeCst.SHOP_AC_KEY_SET);
|
||||
String accountAppId = shopAc.get(ParamCodeCst.Wechat.Ac.SHOP_WX_AC_APP_ID);
|
||||
String accountAppId = shopAc.get(ParamCodeCst.Wechat.Ac.SHOP_WX_AC_APP_ID);
|
||||
String accountSecrete = shopAc.get(ParamCodeCst.Wechat.Ac.SHOP_WX_AC_SECRETE);
|
||||
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?";
|
||||
Map<String, Object> requestUrlParam = new HashMap<>();
|
||||
@@ -162,25 +162,23 @@ public class WechatMiniMsgUtil {
|
||||
|
||||
//获取小程序token
|
||||
private String getAccessToken() {
|
||||
String accessToken = Convert.toStr(redisService.get("wx:mini:AccessToken"));
|
||||
if (StrUtil.isNotEmpty(accessToken)) {
|
||||
return accessToken;
|
||||
}
|
||||
// 商户小程序参数
|
||||
Map<String, String> shopMiniKeyMap = sysParamsService.getParamsByMap("shop_mini_key_set", ParamCodeCst.SHOP_MINI_KEY_SET);
|
||||
String appId = shopMiniKeyMap.get(ParamCodeCst.Wechat.Mini.SHOP_WX_APP_ID);
|
||||
String secrete = shopMiniKeyMap.get(ParamCodeCst.Wechat.Mini.SHOP_WX_SECRETE);
|
||||
|
||||
String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", TOKEN_URL, appId, secrete);
|
||||
String response = HttpUtil.get(url);
|
||||
JSONObject jsonResponse = JSONObject.parseObject(response);
|
||||
if (!jsonResponse.containsKey("access_token")) {
|
||||
throw new RuntimeException("Failed to retrieve access token: " + response);
|
||||
String resp = HttpUtil.get(StrUtil.format("https://access-token.sxczgkj.com/accessToken?appId={}&appSecret={}", appId, secrete));
|
||||
if (StrUtil.isNotBlank(resp)) {
|
||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||
return respInfo.getString("accessToken");
|
||||
} else {
|
||||
String response = HttpUtil.get(String.format("%s?grant_type=client_credential&appid=%s&secret=%s", TOKEN_URL, appId, secrete));
|
||||
JSONObject jsonResponse = JSONObject.parseObject(response);
|
||||
if (!jsonResponse.containsKey("access_token")) {
|
||||
throw new CzgException("零点八零商户端 获取access_token失败: " + response);
|
||||
}
|
||||
return jsonResponse.getString("access_token");
|
||||
}
|
||||
accessToken = jsonResponse.getString("access_token");
|
||||
int expiresIn = jsonResponse.getInteger("expires_in");
|
||||
redisService.set("wx:mini:AccessToken", accessToken, expiresIn - 10);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
package com.czg.service.market.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.constants.ParamCodeCst;
|
||||
import com.czg.exception.CzgException;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.system.service.SysParamsService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -17,6 +16,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信支付service
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@Component
|
||||
@@ -27,35 +27,26 @@ public class AppWxServiceImpl extends BaseWx {
|
||||
@DubboReference
|
||||
private SysParamsService paramsService;
|
||||
|
||||
public AppWxServiceImpl(@Autowired RedisService autoRedisService) {
|
||||
this.redisService = autoRedisService;
|
||||
public AppWxServiceImpl() {
|
||||
config = new Config();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAccessToken(boolean refresh) {
|
||||
init();
|
||||
Object token = redisService.get("wx:user:access_token");
|
||||
if (!refresh && token instanceof String) {
|
||||
return (String) token;
|
||||
}
|
||||
|
||||
String response = HttpUtil.get(WX_ACCESS_TOKEN_URL,
|
||||
Map.of("grant_type", "client_credential", "appid", config.appId, "secret", config.appSecret)
|
||||
);
|
||||
|
||||
log.info("获取access_token响应: {}", response);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
String accessToken = jsonObject.getString("access_token");
|
||||
if (accessToken == null) {
|
||||
throw new RuntimeException("获取access_token失败");
|
||||
String resp = HttpUtil.get(StrUtil.format("https://access-token.sxczgkj.com/accessToken?appId={}&appSecret={}", config.appId, config.appSecret));
|
||||
// 响应 {"accessToken":"100_6C_jltHANT1y2Fot5PXKFDzPXTyWumCsao0oMoNRvJUTuxS0IOVO4nBmjdmx5dZfYItShFVSAKYzNDf7ZGLPlx52ii1Y1qerrbbSmIiLWCrec5qjBY4gV5Tfv8YKKTdABAEEN","appId":"wx212769170d2c6b2a"}
|
||||
if (StrUtil.isNotBlank(resp)) {
|
||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||
return respInfo.getString("accessToken");
|
||||
} else {
|
||||
String response = HttpUtil.get(WX_ACCESS_TOKEN_URL,
|
||||
Map.of("grant_type", "client_credential", "appid", config.appId, "secret", config.appSecret)
|
||||
);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
return jsonObject.getString("access_token");
|
||||
}
|
||||
Long expiresIn = jsonObject.getLong("expires_in");
|
||||
if (expiresIn == null) {
|
||||
expiresIn = DEFAULT_EXPIRES_IN;
|
||||
}
|
||||
redisService.set("wx:user:access_token", accessToken, expiresIn - EXPIRES_OFFSET);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
package com.czg.service.market.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.constants.ParamCodeCst;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.system.service.SysParamsService;
|
||||
import com.ijpay.core.kit.RsaKit;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
@@ -27,38 +25,25 @@ public class WxServiceImpl extends BaseWx {
|
||||
@DubboReference
|
||||
private SysParamsService paramsService;
|
||||
|
||||
private final Set<String> shopMiniKeys = Set.of(ParamCodeCst.Wechat.Mini.SHOP_WX_APP_ID, ParamCodeCst.Wechat.Mini.SHOP_WX_SECRETE);
|
||||
|
||||
|
||||
public WxServiceImpl(@Autowired RedisService redisService) {
|
||||
this.redisService = redisService;
|
||||
public WxServiceImpl() {
|
||||
config = new Config();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAccessToken(boolean refresh) {
|
||||
init();
|
||||
Object token = redisService.get("wx:shop:access_token");
|
||||
if (!refresh && token instanceof String) {
|
||||
return (String) token;
|
||||
}
|
||||
|
||||
String response = HttpUtil.get(WX_ACCESS_TOKEN_URL,
|
||||
Map.of("grant_type", "client_credential", "appid", config.appId, "secret", config.appSecret)
|
||||
);
|
||||
|
||||
log.info("获取access_token响应: {}", response);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
String accessToken = jsonObject.getString("access_token");
|
||||
if (accessToken == null) {
|
||||
throw new RuntimeException("获取access_token失败");
|
||||
String resp = HttpUtil.get(StrUtil.format("https://access-token.sxczgkj.com/accessToken?appId={}&appSecret={}", config.appId, config.appSecret));
|
||||
if (StrUtil.isNotBlank(resp)) {
|
||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||
return respInfo.getString("accessToken");
|
||||
} else {
|
||||
String response = HttpUtil.get(WX_ACCESS_TOKEN_URL,
|
||||
Map.of("grant_type", "client_credential", "appid", config.appId, "secret", config.appSecret)
|
||||
);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
return jsonObject.getString("access_token");
|
||||
}
|
||||
Long expiresIn = jsonObject.getLong("expires_in");
|
||||
if (expiresIn == null) {
|
||||
expiresIn = DEFAULT_EXPIRES_IN;
|
||||
}
|
||||
redisService.set("wx:shop:access_token", accessToken, expiresIn - EXPIRES_OFFSET);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,12 +7,9 @@ import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.czg.exception.CzgException;
|
||||
import com.czg.service.RedisService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -41,9 +38,6 @@ public class WxAccountUtil {
|
||||
@Value("${wx.ysk.warnMsgTmpId}")
|
||||
private String warnMsgTmpId;
|
||||
|
||||
@Resource
|
||||
@Lazy
|
||||
private RedisService redisService;
|
||||
|
||||
static LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
|
||||
|
||||
@@ -97,16 +91,20 @@ public class WxAccountUtil {
|
||||
}
|
||||
|
||||
public String getAccessToken() {
|
||||
String resp = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appId, secrete));
|
||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||
if (!respInfo.containsKey("access_token")) {
|
||||
log.warn("公众号获取token失败, 响应内容: {}", resp);
|
||||
throw new RuntimeException(resp);
|
||||
String resp = HttpUtil.get(StrUtil.format("https://access-token.sxczgkj.com/accessToken?appId={}&appSecret={}", appId, secrete));
|
||||
// 响应 {"accessToken":"100_6C_jltHANT1y2Fot5PXKFDzPXTyWumCsao0oMoNRvJUTuxS0IOVO4nBmjdmx5dZfYItShFVSAKYzNDf7ZGLPlx52ii1Y1qerrbbSmIiLWCrec5qjBY4gV5Tfv8YKKTdABAEEN","appId":"wx212769170d2c6b2a"}
|
||||
if(StrUtil.isNotBlank(resp)){
|
||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||
return respInfo.getString("accessToken");
|
||||
}else {
|
||||
String resp1 = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appId, secrete));
|
||||
JSONObject respInfo = JSONObject.parseObject(resp1);
|
||||
if (!respInfo.containsKey("access_token")) {
|
||||
log.warn("银收客 公众号获取token失败, 响应内容: {}", resp1);
|
||||
throw new CzgException(resp);
|
||||
}
|
||||
return respInfo.getString("access_token");
|
||||
}
|
||||
String accessToken = respInfo.getString("access_token");
|
||||
// int expiresIn = respInfo.getInteger("expires_in");
|
||||
// redisService.set("accessToken", accessToken, expiresIn - 10);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
public JSONObject sendTemplateMsg(String detail, String templateId, String toUserOpenId, Map<String, Object> data) {
|
||||
|
||||
Reference in New Issue
Block a user