diff --git a/mallinkBApi/src/main/java/com/iformall/controller/BaseController.java b/mallinkBApi/src/main/java/com/iformall/controller/BaseController.java index 0e8b25816..233fa7750 100644 --- a/mallinkBApi/src/main/java/com/iformall/controller/BaseController.java +++ b/mallinkBApi/src/main/java/com/iformall/controller/BaseController.java @@ -46,6 +46,9 @@ public class BaseController { @Autowired private WxBuserService wxBuserService; + + @Autowired + MaUtil maUtil; @InitBinder public void InitBinder(WebDataBinder dataBinder) { @@ -144,14 +147,14 @@ public class BaseController { return openService.getWxOpenComponentService().getWxMaServiceByAppid(appId); } else { WxAppinfo appinfo = wxAppinfoService.getByAppId(appId); - WxMaService service = MaUtil.getWeappService(appinfo); + WxMaService service = maUtil.getWeappService(appinfo); return service; } } public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { if(!isFmOpen) { - WxMaService service = MaUtil.getWeappService(appinfo); + WxMaService service = maUtil.getWeappService(appinfo); return service; } return null; diff --git a/mallinkCApi/src/main/java/com/iformall/controller/BaseController.java b/mallinkCApi/src/main/java/com/iformall/controller/BaseController.java index d443c15dd..be188da4b 100644 --- a/mallinkCApi/src/main/java/com/iformall/controller/BaseController.java +++ b/mallinkCApi/src/main/java/com/iformall/controller/BaseController.java @@ -70,6 +70,9 @@ public class BaseController { @Qualifier("objectCommonRedisTemplate") RedisTemplate objectCommonRedisTemplate; + @Autowired + MaUtil maUtil; + @InitBinder public void InitBinder(WebDataBinder dataBinder) { dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { @@ -204,7 +207,7 @@ public class BaseController { if (appinfo == null) { return null; } - WxMaService service = MaUtil.getWeappService(appinfo); + WxMaService service = maUtil.getWeappService(appinfo); return service; } @@ -212,7 +215,7 @@ public class BaseController { public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { if(!isFmOpen) { - WxMaService service = MaUtil.getWeappService(appinfo); + WxMaService service = maUtil.getWeappService(appinfo); return service; } return null; diff --git a/mallinkPosApi/src/main/java/com/iformall/controller/BaseController.java b/mallinkPosApi/src/main/java/com/iformall/controller/BaseController.java index b6a067993..9de3e25f0 100644 --- a/mallinkPosApi/src/main/java/com/iformall/controller/BaseController.java +++ b/mallinkPosApi/src/main/java/com/iformall/controller/BaseController.java @@ -30,6 +30,9 @@ public class BaseController { @Autowired private FmOpenService openService; + + @Autowired + MaUtil maUtil; @InitBinder public void InitBinder(WebDataBinder dataBinder) { @@ -62,14 +65,14 @@ public class BaseController { return openService.getWxOpenComponentService().getWxMaServiceByAppid(appId); } else { WxAppinfo appinfo = wxAppinfoService.getByAppId(appId); - WxMaService service = MaUtil.getWeappService(appinfo); + WxMaService service = maUtil.getWeappService(appinfo); return service; } } public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { if(!isFmOpen) { - WxMaService service = MaUtil.getWeappService(appinfo); + WxMaService service = maUtil.getWeappService(appinfo); return service; } return null; diff --git a/mallinkSchedule/src/main/java/com/iformall/schedule/WeChatAccessTokenSchedule.java b/mallinkSchedule/src/main/java/com/iformall/schedule/WeChatAccessTokenSchedule.java index fcce0b6bc..fdb34194d 100644 --- a/mallinkSchedule/src/main/java/com/iformall/schedule/WeChatAccessTokenSchedule.java +++ b/mallinkSchedule/src/main/java/com/iformall/schedule/WeChatAccessTokenSchedule.java @@ -24,6 +24,9 @@ public class WeChatAccessTokenSchedule { @Autowired WxAppinfoMapper wxAppinfoMapper; + + @Autowired + MaUtil maUtil; @Scheduled(cron = "0 1 * * * ?") // 每小时第一分钟执行 @@ -44,7 +47,7 @@ public class WeChatAccessTokenSchedule { } public void resetAccessToken(WxAppinfo appinfo) { - WxMaService wxMaService = MaUtil.getWeappService(appinfo); + WxMaService wxMaService = maUtil.getWeappService(appinfo); try { String accessToken = wxMaService.getAccessToken(true); WxAppinfo updateAppInfo = new WxAppinfo(); diff --git a/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java b/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java index f04a5e328..c3b8441c2 100644 --- a/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java +++ b/mallinkSchedule/src/main/java/com/iformall/schedule/WxAppVisitSchedule.java @@ -44,6 +44,9 @@ public class WxAppVisitSchedule { @Autowired private WxMallService wxMallService; + + @Autowired + MaUtil maUtil; @Scheduled(cron = "0 0 9/1 * * ? ") //早晨9点开始每小时取一次,获取前一日,微信时间为GMT @@ -128,7 +131,7 @@ public class WxAppVisitSchedule { if(isFmOpen) { wxMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appInfo.getAppId()); } else { - wxMaService = MaUtil.getWeappService(appInfo); + wxMaService = maUtil.getWeappService(appInfo); } try { List vtList = wxMaService.getAnalysisService().getDailyVisitTrend(lastDate, lastDate); diff --git a/mallinkService/src/main/java/com/iformall/common/FmHttpClientBuilder.java b/mallinkService/src/main/java/com/iformall/common/FmHttpClientBuilder.java index 33dae43f4..1687296a6 100644 --- a/mallinkService/src/main/java/com/iformall/common/FmHttpClientBuilder.java +++ b/mallinkService/src/main/java/com/iformall/common/FmHttpClientBuilder.java @@ -89,8 +89,9 @@ public class FmHttpClientBuilder implements ApacheHttpClientBuilder { private FmHttpClientBuilder() { } + private static FmHttpClientBuilder instance = new FmHttpClientBuilder(); public static FmHttpClientBuilder get() { - return new FmHttpClientBuilder(); + return instance; } @Override diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxPayAccountServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxPayAccountServiceImpl.java index 8764bce31..dae85bed7 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxPayAccountServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxPayAccountServiceImpl.java @@ -27,6 +27,9 @@ public class WxPayAccountServiceImpl implements WxPayAccountService { @Autowired WxAppinfoService wxAppinfoService; + + @Autowired + MaUtil maUtil; @Override @@ -87,7 +90,7 @@ public class WxPayAccountServiceImpl implements WxPayAccountService { WxAppinfo cAppInfo = wxAppinfoService.getCAppInfo(tenantEntity, EnumPayWay.PAY_WAY_WECHAT); WxPayAccount payAccount = this.getByTenantId(tenantId); if(cAppInfo != null && payAccount != null){ - return MaUtil.getWxPayService(cAppInfo,payAccount); + return maUtil.getWxPayService(cAppInfo,payAccount); }else{ return null; } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxTemplateMsgServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxTemplateMsgServiceImpl.java index 748923261..ad7ff3ece 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxTemplateMsgServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxTemplateMsgServiceImpl.java @@ -41,6 +41,9 @@ public class WxTemplateMsgServiceImpl implements WxTemplateMsgService { @Autowired private FmOpenService openService; + + @Autowired + MaUtil maUtil; @Override @@ -121,7 +124,7 @@ public class WxTemplateMsgServiceImpl implements WxTemplateMsgService { wxMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appinfo.getAppId()); } else { WxAppinfo appInfo = wxAppinfoService.getByAppId(appinfo.getAppId()); - wxMaService = MaUtil.getWeappService(appInfo); + wxMaService = maUtil.getWeappService(appInfo); } String s = ""; if(byId.getType() == EnumTemplateType.ACTIVITY_REMIND.getCode()){ diff --git a/mallinkService/src/main/java/com/iformall/service/msg/impl/SendSmartAppMsgServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/msg/impl/SendSmartAppMsgServiceImpl.java index 374affe5b..725a76f2a 100644 --- a/mallinkService/src/main/java/com/iformall/service/msg/impl/SendSmartAppMsgServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/msg/impl/SendSmartAppMsgServiceImpl.java @@ -48,6 +48,9 @@ public class SendSmartAppMsgServiceImpl implements MsgSendService { @Autowired private WxCUserMapper wxCUserMapper; + + @Autowired + MaUtil maUtil; @Override public void send(BaseMsg baseMsg) throws Exception{ @@ -66,7 +69,7 @@ public class SendSmartAppMsgServiceImpl implements MsgSendService { wxMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(smartAppMsg.getAppId()); } else { WxAppinfo appInfo = wxAppinfoService.getByAppId(smartAppMsg.getAppId()); - wxMaService = MaUtil.getWeappService(appInfo); + wxMaService = maUtil.getWeappService(appInfo); } //查询信息模板 diff --git a/mallinkService/src/main/java/com/iformall/service/pay/service/pay/wx/BaseWxPayAdapterService.java b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/wx/BaseWxPayAdapterService.java index d22f97f09..5dfb38dbb 100644 --- a/mallinkService/src/main/java/com/iformall/service/pay/service/pay/wx/BaseWxPayAdapterService.java +++ b/mallinkService/src/main/java/com/iformall/service/pay/service/pay/wx/BaseWxPayAdapterService.java @@ -232,13 +232,16 @@ public class BaseWxPayAdapterService { @Autowired WxOpenService openService; + @Autowired + MaUtil maUtil; + protected File getQrCode(WxAppinfo appinfo,String pageUrl,int type,String sceneParam) throws WxErrorException { boolean isFmOpen = false; WxMaService wxMaService; if(isFmOpen) { wxMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appinfo.getAppId()); } else { - wxMaService = MaUtil.getWeappService(appinfo); + wxMaService = maUtil.getWeappService(appinfo); } boolean autoColor = false; diff --git a/mallinkService/src/main/java/com/iformall/utils/MaUtil.java b/mallinkService/src/main/java/com/iformall/utils/MaUtil.java index 0f15e9d29..ff46f09ef 100644 --- a/mallinkService/src/main/java/com/iformall/utils/MaUtil.java +++ b/mallinkService/src/main/java/com/iformall/utils/MaUtil.java @@ -11,11 +11,27 @@ import com.iformall.domain.po.WxAppinfo; import com.iformall.domain.po.WxPayAccount; import com.iformall.douyin.miniapp.api.TtMaService; import com.iformall.douyin.miniapp.api.impl.TtMaServiceImpl; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +@Service public class MaUtil { - static public WxMaService getWeappService(WxAppinfo appinfo) { + private static Map serviceMap = new ConcurrentHashMap(); + private static Map appServiceKeyMap = new ConcurrentHashMap(); + + + private static Map payServiceMap = new ConcurrentHashMap(); + + private static Map wxMaServiceMap = new ConcurrentHashMap(); + private static Map wxMaAppServiceKeyMap = new ConcurrentHashMap(); + + public WxMaService getWeappService(WxAppinfo appinfo) { WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); config.setAppid(appinfo.getAppId()); config.setSecret(appinfo.getSecret()); @@ -27,12 +43,33 @@ public class MaUtil { config.setExpiresTime(appinfo.getLastTokenTime().getTime()+appinfo.getExpiresIn()*1000); } config.setApacheHttpClientBuilder(FmHttpClientBuilder.get()); - WxMaService service = new WxMaServiceImpl(); - service.setWxMaConfig(config); + + String key = appinfo.getAppId()+String.valueOf(config.getExpiresTime()); + WxMaService service = wxMaServiceMap.get(key); + if ( null == service ) { + synchronized("getWxMaServcieBlock"+appinfo.getAppId()) { + service = wxMaServiceMap.get(key); + if (null == service) { + //删除掉之前的 + String appServiceKey = wxMaAppServiceKeyMap.get(appinfo.getAppId()); + if (!StringUtils.isBlank(appServiceKey)) { + if (wxMaServiceMap.containsKey(appServiceKey)) { + wxMaServiceMap.remove(appServiceKey); + } + wxMaAppServiceKeyMap.remove(appinfo.getAppId()); + } + + service = new WxMaServiceImpl(); + service.setWxMaConfig(config); + wxMaServiceMap.put(key, service); + wxMaAppServiceKeyMap.put(appinfo.getAppId(),key); + } + } + } return service; } - static public WxPayService getWxPayService(WxAppinfo appinfo, WxPayAccount payAccount) { + public WxPayService getWxPayService(WxAppinfo appinfo, WxPayAccount payAccount) { WxPayConfig config = new WxPayConfig(); config.setAppId(appinfo.getAppId()); config.setMchId(payAccount.getMchId()); @@ -50,12 +87,21 @@ public class MaUtil { config.setCertSerialNo(payAccount.getCertSerialNo());//证书序列号 config.setApiV3Key(payAccount.getApiV3Key());//apiV3秘钥 - WxPayService wxPayService = new WxPayServiceImpl(); - wxPayService.setConfig(config); - return wxPayService; + WxPayService service = payServiceMap.get(appinfo.getAppId()); + if ( null == service ) { + synchronized("getWxPayServiceBlock"+appinfo.getAppId()) { + service = payServiceMap.get(appinfo.getAppId()); + if (null == service) { + service = new WxPayServiceImpl(); + service.setConfig(config); + payServiceMap.put(appinfo.getAppId(), service); + } + } + } + return service; } - static public TtMaService getTtappService(WxAppinfo appinfo) { + public TtMaService getTtappService(WxAppinfo appinfo) { WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); config.setAppid(appinfo.getAppId()); config.setSecret(appinfo.getSecret()); @@ -67,8 +113,29 @@ public class MaUtil { config.setExpiresTime(appinfo.getLastTokenTime().getTime()+appinfo.getExpiresIn()*1000); } config.setApacheHttpClientBuilder(FmHttpClientBuilder.get()); - TtMaService service = new TtMaServiceImpl(); - service.setWxMaConfig(config); + + String key = appinfo.getAppId()+String.valueOf(config.getExpiresTime()); + TtMaService service = serviceMap.get(key); + if ( null == service ) { + synchronized("getTtappServiceBlock"+appinfo.getAppId()) { + service = serviceMap.get(key); + if (null == service) { + //删除掉之前的 + String appServiceKey = appServiceKeyMap.get(appinfo.getAppId()); + if (!StringUtils.isBlank(appServiceKey)) { + if (serviceMap.containsKey(appServiceKey)) { + serviceMap.remove(appServiceKey); + } + appServiceKeyMap.remove(appinfo.getAppId()); + } + + service = new TtMaServiceImpl(); + service.setWxMaConfig(config); + serviceMap.put(key, service); + appServiceKeyMap.put(appinfo.getAppId(),key); + } + } + } return service; } } diff --git a/mallinkTTAdmin/src/main/java/com/iformall/controller/base/BaseController.java b/mallinkTTAdmin/src/main/java/com/iformall/controller/base/BaseController.java index 0f11f0f13..703efdf0d 100644 --- a/mallinkTTAdmin/src/main/java/com/iformall/controller/base/BaseController.java +++ b/mallinkTTAdmin/src/main/java/com/iformall/controller/base/BaseController.java @@ -34,6 +34,9 @@ public class BaseController { @Autowired private WxAppinfoService wxAppinfoService; + + @Autowired + MaUtil maUtil; @InitBinder public void InitBinder(WebDataBinder dataBinder) { @@ -73,7 +76,7 @@ public class BaseController { // } public TtMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { - TtMaService ttMaService = MaUtil.getTtappService(appinfo); + TtMaService ttMaService = maUtil.getTtappService(appinfo); if (StringUtils.isBlank(appinfo.getAccessToken())) { // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 updateAppAccessToken(appinfo, ttMaService); diff --git a/mallinkTTCApi/src/main/java/com/iformall/controller/BaseController.java b/mallinkTTCApi/src/main/java/com/iformall/controller/BaseController.java index 5dfdf07e5..363e932ad 100644 --- a/mallinkTTCApi/src/main/java/com/iformall/controller/BaseController.java +++ b/mallinkTTCApi/src/main/java/com/iformall/controller/BaseController.java @@ -53,6 +53,9 @@ public class BaseController { @Qualifier("objectCommonRedisTemplate") RedisTemplate objectCommonRedisTemplate; + @Autowired + MaUtil maUtil; + @InitBinder public void InitBinder(WebDataBinder dataBinder) { dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { @@ -109,7 +112,7 @@ public class BaseController { } public TtMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { - TtMaService ttMaService = MaUtil.getTtappService(appinfo); + TtMaService ttMaService = maUtil.getTtappService(appinfo); if (StringUtils.isBlank(appinfo.getAccessToken())) { // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 updateAppAccessToken(appinfo, ttMaService);