| @@ -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; | |||
| @@ -70,6 +70,9 @@ public class BaseController { | |||
| @Qualifier("objectCommonRedisTemplate") | |||
| RedisTemplate<String, Object> 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; | |||
| @@ -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; | |||
| @@ -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(); | |||
| @@ -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<WxMaVisitTrend> vtList = wxMaService.getAnalysisService().getDailyVisitTrend(lastDate, lastDate); | |||
| @@ -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 | |||
| @@ -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; | |||
| } | |||
| @@ -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()){ | |||
| @@ -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); | |||
| } | |||
| //查询信息模板 | |||
| @@ -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; | |||
| @@ -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<String,TtMaService> serviceMap = new ConcurrentHashMap<String,TtMaService>(); | |||
| private static Map<String,String> appServiceKeyMap = new ConcurrentHashMap<String,String>(); | |||
| private static Map<String,WxPayService> payServiceMap = new ConcurrentHashMap<String,WxPayService>(); | |||
| private static Map<String,WxMaService> wxMaServiceMap = new ConcurrentHashMap<String,WxMaService>(); | |||
| private static Map<String,String> wxMaAppServiceKeyMap = new ConcurrentHashMap<String,String>(); | |||
| 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; | |||
| } | |||
| } | |||
| @@ -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); | |||
| @@ -53,6 +53,9 @@ public class BaseController { | |||
| @Qualifier("objectCommonRedisTemplate") | |||
| RedisTemplate<String, Object> 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); | |||