|
1 | 1 | package me.chanjar.weixin.cp.api.impl;
|
2 | 2 |
|
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import me.chanjar.weixin.common.WxType; |
| 6 | +import me.chanjar.weixin.common.bean.WxAccessToken; |
| 7 | +import me.chanjar.weixin.common.error.WxError; |
| 8 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 9 | +import me.chanjar.weixin.cp.constant.WxCpApiPathConsts; |
| 10 | +import org.apache.http.client.config.RequestConfig; |
| 11 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 12 | +import org.apache.http.client.methods.HttpGet; |
| 13 | +import org.apache.http.impl.client.BasicResponseHandler; |
| 14 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.concurrent.locks.Lock; |
| 18 | + |
| 19 | +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.GET_AGENT_CONFIG_TICKET; |
| 20 | +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.GET_JSAPI_TICKET; |
| 21 | + |
3 | 22 | /**
|
4 | 23 | * <pre>
|
5 | 24 | * 默认接口实现类,使用apache httpclient实现
|
6 | 25 | * Created by Binary Wang on 2017-5-27.
|
7 | 26 | * </pre>
|
| 27 | + * <pre> |
| 28 | + * 增加分布式锁(基于WxCpConfigStorage实现)的支持 |
| 29 | + * Updated by yuanqixun on 2020-05-13 |
| 30 | + * </pre> |
| 31 | + * |
8 | 32 | *
|
9 | 33 | * @author <a href="https://github.com/binarywang">Binary Wang</a>
|
10 | 34 | */
|
11 | 35 | public class WxCpServiceImpl extends WxCpServiceApacheHttpClientImpl {
|
| 36 | + @Override |
| 37 | + public String getAccessToken(boolean forceRefresh) throws WxErrorException { |
| 38 | + if (!getWxCpConfigStorage().isAccessTokenExpired() && !forceRefresh) { |
| 39 | + return getWxCpConfigStorage().getAccessToken(); |
| 40 | + } |
| 41 | + Lock lock = getWxCpConfigStorage().getAccessTokenLock(); |
| 42 | + lock.lock(); |
| 43 | + try { |
| 44 | + // 拿到锁之后,再次判断一下最新的token是否过期,避免重刷 |
| 45 | + if (!getWxCpConfigStorage().isAccessTokenExpired() && !forceRefresh) { |
| 46 | + return getWxCpConfigStorage().getAccessToken(); |
| 47 | + } |
| 48 | + String url = String.format(getWxCpConfigStorage().getApiUrl(WxCpApiPathConsts.GET_TOKEN), this.configStorage.getCorpId(), this.configStorage.getCorpSecret()); |
| 49 | + try { |
| 50 | + HttpGet httpGet = new HttpGet(url); |
| 51 | + if (getRequestHttpProxy() != null) { |
| 52 | + RequestConfig config = RequestConfig.custom() |
| 53 | + .setProxy(getRequestHttpProxy()).build(); |
| 54 | + httpGet.setConfig(config); |
| 55 | + } |
| 56 | + String resultContent; |
| 57 | + try (CloseableHttpClient httpClient = getRequestHttpClient(); |
| 58 | + CloseableHttpResponse response = httpClient.execute(httpGet)) { |
| 59 | + resultContent = new BasicResponseHandler().handleResponse(response); |
| 60 | + } finally { |
| 61 | + httpGet.releaseConnection(); |
| 62 | + } |
| 63 | + WxError error = WxError.fromJson(resultContent, WxType.CP); |
| 64 | + if (error.getErrorCode() != 0) { |
| 65 | + throw new WxErrorException(error); |
| 66 | + } |
| 67 | + |
| 68 | + WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); |
| 69 | + getWxCpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); |
| 70 | + } catch (IOException e) { |
| 71 | + throw new RuntimeException(e); |
| 72 | + } |
| 73 | + } finally { |
| 74 | + lock.unlock(); |
| 75 | + } |
| 76 | + return getWxCpConfigStorage().getAccessToken(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String getAgentJsapiTicket(boolean forceRefresh) throws WxErrorException { |
| 81 | + if (forceRefresh) { |
| 82 | + getWxCpConfigStorage().expireAgentJsapiTicket(); |
| 83 | + } |
| 84 | + if (getWxCpConfigStorage().isAgentJsapiTicketExpired()) { |
| 85 | + Lock lock = getWxCpConfigStorage().getAgentJsapiTicketLock(); |
| 86 | + lock.lock(); |
| 87 | + try { |
| 88 | + // 拿到锁之后,再次判断一下最新的token是否过期,避免重刷 |
| 89 | + if (getWxCpConfigStorage().isAgentJsapiTicketExpired()) { |
| 90 | + String responseContent = this.get(getWxCpConfigStorage().getApiUrl(GET_AGENT_CONFIG_TICKET), null); |
| 91 | + JsonObject jsonObject = new JsonParser().parse(responseContent).getAsJsonObject(); |
| 92 | + getWxCpConfigStorage().updateAgentJsapiTicket(jsonObject.get("ticket").getAsString(), |
| 93 | + jsonObject.get("expires_in").getAsInt()); |
| 94 | + } |
| 95 | + } finally { |
| 96 | + lock.unlock(); |
| 97 | + } |
| 98 | + } |
| 99 | + return getWxCpConfigStorage().getAgentJsapiTicket(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public String getJsapiTicket(boolean forceRefresh) throws WxErrorException { |
| 104 | + if (forceRefresh) { |
| 105 | + getWxCpConfigStorage().expireJsapiTicket(); |
| 106 | + } |
| 107 | + |
| 108 | + if (getWxCpConfigStorage().isJsapiTicketExpired()) { |
| 109 | + Lock lock = getWxCpConfigStorage().getJsapiTicketLock(); |
| 110 | + lock.lock(); |
| 111 | + try { |
| 112 | + // 拿到锁之后,再次判断一下最新的token是否过期,避免重刷 |
| 113 | + if (getWxCpConfigStorage().isJsapiTicketExpired()) { |
| 114 | + String responseContent = this.get(getWxCpConfigStorage().getApiUrl(GET_JSAPI_TICKET), null); |
| 115 | + JsonObject tmpJsonObject = new JsonParser().parse(responseContent).getAsJsonObject(); |
| 116 | + getWxCpConfigStorage().updateJsapiTicket(tmpJsonObject.get("ticket").getAsString(), |
| 117 | + tmpJsonObject.get("expires_in").getAsInt()); |
| 118 | + } |
| 119 | + } finally { |
| 120 | + lock.unlock(); |
| 121 | + } |
| 122 | + } |
| 123 | + return getWxCpConfigStorage().getJsapiTicket(); |
| 124 | + } |
12 | 125 | }
|
0 commit comments