import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.sinoi.framework.util.http.HttpClient;
import com.sinoi.framework.util.http.HttpResponse;
import com.sinoi.framework.util.serialize.json.JsonUtil;
import com.sinoi.sitecase.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class WxSiteCaseSDK {
private static final Logger log = LoggerFactory.getLogger(WxSiteCaseSDK.class);
private final StringRedisTemplate redisTemplate;
public WxSiteCaseSDK(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 企微接口-获取微信接口所需的access_token信息
*/
private String getToKen() {
String TOKEN_KEY = "wx:xxx:token";
String token = redisTemplate.opsForValue().get(TOKEN_KEY);
if (StrUtil.isEmpty(token)) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=xxxxx&corpsecret=xxxxxxxxx";
try {
HttpResponse httpResponse = HttpClient.get(url);
String body = httpResponse.asString();
Map<Object, Object> map = JsonUtil.asMap(body);
token = (String) map.get("access_token");
int expires = (int) map.get("expires_in");
redisTemplate.opsForValue().setIfAbsent(TOKEN_KEY, token, expires, TimeUnit.SECONDS);
log.debug("初始化获取ToKen:{}", token);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return token;
}
/**
* 返回企业jsapi_ticket
*/
private String get_corp_jsapi_ticket() {
String redis_key = "wx:sitecase:corp:jsapi_ticket";
String jsapi_ticket = redisTemplate.opsForValue().get(redis_key);
if (StrUtil.isNotEmpty(jsapi_ticket)) {
log.debug("get_corp_jsapi_ticket-取缓存值:{}", jsapi_ticket);
return jsapi_ticket;
}
String biz_url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + this.getToKen();
HttpResponse httpResponse = null;
try {
httpResponse = HttpClient.get(biz_url);
String body = httpResponse.asString();
log.debug("get_corp_jsapi_ticket-接口返回值:{}", body);
Map<Object, Object> map = JsonUtil.asMap(body);
Integer errCode = (Integer) map.get("errcode");
if (errCode != 0) {
log.error("get_corp_jsapi_ticket-接口返回值:{}", body);
return null;
} else {
jsapi_ticket = (String) map.get("ticket");
Integer expires_in = (Integer) map.get("expires_in");
redisTemplate.opsForValue().set(redis_key, jsapi_ticket, expires_in, TimeUnit.SECONDS);
return jsapi_ticket;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 返回应用jsapi_ticket
*/
private String get_app_jsapi_ticket() {
String redis_key = "wx:xxxxxx:app:jsapi_ticket";
String jsapi_ticket = redisTemplate.opsForValue().get(redis_key);
if (StrUtil.isNotEmpty(jsapi_ticket)) {
log.debug("get_app_jsapi_ticket-取缓存值:{}", jsapi_ticket);
return jsapi_ticket;
}
String biz_url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?type=agent_config&access_token=" + this.getToKen();
HttpResponse httpResponse = null;
try {
httpResponse = HttpClient.get(biz_url);
String body = httpResponse.asString();
log.debug("get_app_jsapi_ticket-接口返回值:{}", body);
Map<Object, Object> map = JsonUtil.asMap(body);
Integer errCode = (Integer) map.get("errcode");
if (errCode != 0) {
log.error("get_app_jsapi_ticket-接口返回值:{}", body);
return null;
} else {
jsapi_ticket = (String) map.get("ticket");
Integer expires_in = (Integer) map.get("expires_in");
redisTemplate.opsForValue().set(redis_key, jsapi_ticket, expires_in, TimeUnit.SECONDS);
return jsapi_ticket;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 对调用js接口页面的完整URL进行签名
*
* @param type 签名类型
* @param url 调用js接口页面的完整URL
* @return 签名
*/
public Map<String, String> jsapi_signature(JsapiType type, String url) {
String jsapi_ticket = type == JsapiType.corp ? this.get_corp_jsapi_ticket() : (type == JsapiType.app ? this.get_app_jsapi_ticket() : null);
if (StrUtil.isEmpty(jsapi_ticket)) {
throw new ServiceException("签名失败,ticket出现异常");
}
String noncestr = RandomUtil.randomString(6);
String timestamp = String.valueOf(DateUtil.current());
String signedStr = "".concat("jsapi_ticket=").concat(jsapi_ticket).concat("&noncestr=").concat(noncestr).concat("×tamp=").concat(timestamp);
signedStr = signedStr.concat("&url=").concat(url);
return MapUtil.builder("signature", SecureUtil.sha1(signedStr)).put("noncestr", noncestr).put("timestamp", timestamp).build();
}
public enum JsapiType {
/**
* 企业
*/
corp,
/**
* 应用
*/
app;
}
}```