From cdac6192fcbdd4e5034b008b8cadfe957b9d1808 Mon Sep 17 00:00:00 2001 From: "weiwei.xing" Date: Fri, 23 Oct 2020 10:30:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Fix:=E8=B0=83=E6=95=B4=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=A5=A8=E6=8D=AE=E7=9A=84=E9=94=81=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=97=B6=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/impl/WxMaJsapiServiceImpl.java | 19 ++++++++++--------- .../mp/api/impl/BaseWxMpServiceImpl.java | 19 ++++++++++--------- .../mp/api/impl/WxMpCardServiceImpl.java | 18 +++++++++--------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java index 9177910e36..35f55b36bb 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java @@ -32,22 +32,23 @@ public String getCardApiTicket() throws WxErrorException { @Override public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { - Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock(); - lock.lock(); - try { - if (forceRefresh) { - this.wxMaService.getWxMaConfig().expireCardApiTicket(); - } - if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) { + if (forceRefresh) { + this.wxMaService.getWxMaConfig().expireCardApiTicket(); + } + + if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) { + Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock(); + lock.lock(); + try { String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null); JsonObject tmpJsonObject = GsonParser.parse(responseContent); String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds); + } finally { + lock.unlock(); } - } finally { - lock.unlock(); } return this.wxMaService.getWxMaConfig().getCardApiTicket(); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java index 555078db18..32f5e7ac88 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java @@ -151,23 +151,24 @@ public String getTicket(TicketType type) throws WxErrorException { @Override public String getTicket(TicketType type, boolean forceRefresh) throws WxErrorException { - Lock lock = this.getWxMpConfigStorage().getTicketLock(type); - lock.lock(); - try { - if (forceRefresh) { - this.getWxMpConfigStorage().expireTicket(type); - } - if (this.getWxMpConfigStorage().isTicketExpired(type)) { + if (forceRefresh) { + this.getWxMpConfigStorage().expireTicket(type); + } + + if (this.getWxMpConfigStorage().isTicketExpired(type)) { + Lock lock = this.getWxMpConfigStorage().getTicketLock(type); + lock.lock(); + try { String responseContent = execute(SimpleGetRequestExecutor.create(this), GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null); JsonObject tmpJsonObject = GsonParser.parse(responseContent); String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds); + } finally { + lock.unlock(); } - } finally { - lock.unlock(); } return this.getWxMpConfigStorage().getTicket(type); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java index 36f49acd35..cf4069cf66 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java @@ -48,24 +48,24 @@ public String getCardApiTicket() throws WxErrorException { @Override public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { final TicketType type = TicketType.WX_CARD; - Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type); - lock.lock(); - try { - if (forceRefresh) { - this.getWxMpService().getWxMpConfigStorage().expireTicket(type); - } + if (forceRefresh) { + this.getWxMpService().getWxMpConfigStorage().expireTicket(type); + } - if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) { + if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) { + Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type); + lock.lock(); + try { String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor .create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null); JsonObject tmpJsonObject = GsonParser.parse(responseContent); String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds); + } finally { + lock.unlock(); } - } finally { - lock.unlock(); } return this.getWxMpService().getWxMpConfigStorage().getTicket(type); } From 277937c70c153cc6683720d06890c4441e94e2ea Mon Sep 17 00:00:00 2001 From: "weiwei.xing" Date: Fri, 23 Oct 2020 10:49:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Fix=EF=BC=9A=E6=9B=B4=E6=96=B0=E7=A5=A8?= =?UTF-8?q?=E6=8D=AE=EF=BC=8C=E9=94=81=E4=B9=8B=E5=90=8E=EF=BC=8C=E5=86=8D?= =?UTF-8?q?=E6=AC=A1=E6=A3=80=E6=9F=A5=E6=98=AF=E5=90=A6=E6=9C=89=E6=95=88?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E5=B9=B6=E5=8F=91=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E5=A4=9A=E6=AC=A1=E9=87=8D=E7=BD=AE=E7=A5=A8?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/impl/WxMaJsapiServiceImpl.java | 12 +++++++----- .../weixin/mp/api/impl/BaseWxMpServiceImpl.java | 14 ++++++++------ .../weixin/mp/api/impl/WxMpCardServiceImpl.java | 14 ++++++++------ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java index 35f55b36bb..43d3c22d1e 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaJsapiServiceImpl.java @@ -41,11 +41,13 @@ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock(); lock.lock(); try { - String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null); - JsonObject tmpJsonObject = GsonParser.parse(responseContent); - String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); - int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); - this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds); + if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) { + String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null); + JsonObject tmpJsonObject = GsonParser.parse(responseContent); + String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); + int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); + this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds); + } } finally { lock.unlock(); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java index 32f5e7ac88..c2ecc475f3 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java @@ -160,12 +160,14 @@ public String getTicket(TicketType type, boolean forceRefresh) throws WxErrorExc Lock lock = this.getWxMpConfigStorage().getTicketLock(type); lock.lock(); try { - String responseContent = execute(SimpleGetRequestExecutor.create(this), - GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null); - JsonObject tmpJsonObject = GsonParser.parse(responseContent); - String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); - int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); - this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds); + if (this.getWxMpConfigStorage().isTicketExpired(type)) { + String responseContent = execute(SimpleGetRequestExecutor.create(this), + GET_TICKET_URL.getUrl(this.getWxMpConfigStorage()) + type.getCode(), null); + JsonObject tmpJsonObject = GsonParser.parse(responseContent); + String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); + int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); + this.getWxMpConfigStorage().updateTicket(type, jsapiTicket, expiresInSeconds); + } } finally { lock.unlock(); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java index cf4069cf66..cbfd5d8d07 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java @@ -57,12 +57,14 @@ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { Lock lock = getWxMpService().getWxMpConfigStorage().getTicketLock(type); lock.lock(); try { - String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor - .create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null); - JsonObject tmpJsonObject = GsonParser.parse(responseContent); - String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); - int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); - this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds); + if (this.getWxMpService().getWxMpConfigStorage().isTicketExpired(type)) { + String responseContent = this.wxMpService.execute(SimpleGetRequestExecutor + .create(this.getWxMpService().getRequestHttp()), WxMpApiUrl.Card.CARD_GET_TICKET, null); + JsonObject tmpJsonObject = GsonParser.parse(responseContent); + String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); + int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); + this.getWxMpService().getWxMpConfigStorage().updateTicket(type, cardApiTicket, expiresInSeconds); + } } finally { lock.unlock(); }