com.zopen.wechat.mp.service.WechatMpAccessTokenService Maven / Gradle / Ivy
Show all versions of zopen-ato-starter Show documentation
package com.zopen.wechat.mp.service;
import com.zcj.util.UtilRandom;
import com.zcj.web.context.SystemContext;
import com.zopen.wechat.exception.WechatAssert;
import com.zopen.wechat.mp.dto.base.AccessToken;
import com.zopen.wechat.mp.dto.jssdk.JsapiTicket;
import com.zopen.wechat.mp.task.WechatInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component("atoWechatMpAccessTokenService")
public class WechatMpAccessTokenService {
private static final Logger logger = LoggerFactory.getLogger(WechatMpAccessTokenService.class);
@Autowired
private RestTemplate restTemplate;
/**
* 刷新 access_token 和 jsapi_ticket 的值,保存到 WechatInfo 中
*
* 如果获取 access_token 失败,则抛出异常
*
* @param appId * 指定 appId
* @param appSecret * 指定 appSecret
*/
public void init(final String appId, final String appSecret) {
String logId = UtilRandom.getRandomChar(4);
logger.info("[{}]开始刷新 access_token,app_id[{}]", logId, appId);
String accessToken = httpGetAccessToken(appId, appSecret, logId);
String jsTicket = null;
try {
jsTicket = httpGetJsapiTicket(accessToken, logId);
} catch (Exception e) {
logger.warn("[{}]获取 jsapi_ticket 失败,app_id[{}],错误原因[{}]", logId, appId, e.getMessage());
}
logger.debug("[{}]刷新结束,app_id[{}] secret[{}], access_token[{}], jsapi_ticket[{}]", logId, appId, appSecret, accessToken, jsTicket);
WechatInfo.saveAccessToken(appId, appSecret, accessToken, jsTicket);
}
// 异步执行(出错不会影响主线程)
public void initAsyn(final String appId, final String appSecret) {
SystemContext.getExecutorService().execute(() -> {
init(appId, appSecret);
});
}
// 远程获取 access_token 的值
private String httpGetAccessToken(String appId, String appSecret, String logId) {
WechatAssert.notNullAndEmpty(appId, "appId 不能为空");
WechatAssert.notNullAndEmpty(appSecret, "appSecret 不能为空");
String url = String.format(WechatHttpUrl.MP_GET_ACCESS_TOKEN, appId, appSecret);
AccessToken accessToken = restTemplate.getForObject(url, AccessToken.class);
WechatAssert.notNull(accessToken, "获取 access_token 失败");
accessToken.valid("获取 access_token 失败");
logger.debug("[{}]获取 access_token 成功: app_id[{}] access_token[{}]", logId, appId, accessToken.getAccess_token());
return accessToken.getAccess_token();
}
// 远程获取 jsapi_ticket 的值
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
private String httpGetJsapiTicket(String accessToken, String logId) {
WechatAssert.notNullAndEmpty(accessToken, "access_token 不能为空");
String url = String.format(WechatHttpUrl.MP_GET_TICKET, accessToken);
JsapiTicket jsapiTicket = restTemplate.getForObject(url, JsapiTicket.class);
WechatAssert.notNull(jsapiTicket, "获取 jsapi_ticket 失败");
jsapiTicket.valid("获取 jsapi_ticket 失败");
logger.debug("[{}]获取 jsapi_ticket 成功: access_token[{}] jsapi_ticket[{}]", logId, accessToken, jsapiTicket.getTicket());
return jsapiTicket.getTicket();
}
}