|
|
@ -13,9 +13,9 @@ import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; |
|
|
|
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.epmet.commons.tools.redis.RedisKeys; |
|
|
|
import com.epmet.commons.tools.redis.RedisUtils; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.dto.CustomerAppDTO; |
|
|
|
import com.epmet.dto.CustomerAppRedisDTO; |
|
|
|
import com.epmet.feign.OperCrmOpenFeignClient; |
|
|
|
import com.google.common.collect.Maps; |
|
|
|
import org.apache.logging.log4j.LogManager; |
|
|
@ -31,7 +31,6 @@ import org.springframework.util.CollectionUtils; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* 客户app Redis |
|
|
@ -50,75 +49,69 @@ public class CustomerAppWxServiceUtil implements ApplicationRunner { |
|
|
|
private final static String JSON_STR = "JSON"; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisUtils redisUtils; |
|
|
|
@Autowired |
|
|
|
private RedisTemplate<String,String> redisTemplate; |
|
|
|
private RedisTemplate redisTemplate; |
|
|
|
@Autowired |
|
|
|
private OperCrmOpenFeignClient operCrmOpenFeignClient; |
|
|
|
|
|
|
|
private static Map<String, WxMaService> maServices = Maps.newHashMap(); |
|
|
|
private static Map<String, WxMaService> maServices = Maps.newConcurrentMap(); |
|
|
|
|
|
|
|
public static WxMaService getWxMaService(String appId) { |
|
|
|
WxMaService wxMaService = maServices.get(appId); |
|
|
|
if (wxMaService == null){ |
|
|
|
logger.error("getMaService appId:{} is not config from customer_app",appId); |
|
|
|
if (wxMaService == null) { |
|
|
|
logger.error("getMaService appId:{} is not config from customer_app", appId); |
|
|
|
} |
|
|
|
return wxMaService; |
|
|
|
} |
|
|
|
/*public String get(String appId) { |
|
|
|
String key = RedisKeys.getAppSecretKey(appId); |
|
|
|
String secret = (String) redisUtils.get(key); |
|
|
|
if (StringUtils.isBlank(secret)) { |
|
|
|
CustomerAppSecretFormDTO param = new CustomerAppSecretFormDTO(); |
|
|
|
param.setAppId(appId); |
|
|
|
Result<String> result = operCrmOpenFeignClient.getSecretByAppId(param); |
|
|
|
if (result.success()) { |
|
|
|
secret = result.getData(); |
|
|
|
if (StringUtils.isNotBlank(secret)) { |
|
|
|
redisUtils.set(key, secret, MINUTE_THIRTY_EXPIRE); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return secret; |
|
|
|
}*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
public void run(ApplicationArguments args) { |
|
|
|
Map<String, WxMaService> maServicesNew = Maps.newConcurrentMap(); |
|
|
|
SetOperations appSet = null; |
|
|
|
List<CustomerAppDTO> result = null; |
|
|
|
String appKey = RedisKeys.getCustomerAppKey(); |
|
|
|
try { |
|
|
|
appSet = redisTemplate.opsForSet(); |
|
|
|
Set<CustomerAppRedisDTO> members = appSet.members(appKey); |
|
|
|
if (!CollectionUtils.isEmpty(members)) { |
|
|
|
members.forEach(app -> { |
|
|
|
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); |
|
|
|
config.setAppid(app.getAppId()); |
|
|
|
config.setSecret(app.getSecret()); |
|
|
|
config.setMsgDataFormat(JSON_STR); |
|
|
|
WxMaService service = new WxMaServiceImpl(); |
|
|
|
service.setWxMaConfig(config); |
|
|
|
maServicesNew.put(app.getAppId(), service); |
|
|
|
}); |
|
|
|
} |
|
|
|
} catch (Exception ex) { |
|
|
|
logger.error("init wxMaService from redis error", ex); |
|
|
|
} |
|
|
|
try { |
|
|
|
Result<List<CustomerAppDTO>> configAllAppResult = operCrmOpenFeignClient.getConfigAllApp(); |
|
|
|
logger.info("initWxMa operCrmOpenFeignClient.getConfigAllApp result:{}", JSON.toJSONString(configAllAppResult)); |
|
|
|
if (configAllAppResult == null || !configAllAppResult.success()){ |
|
|
|
logger.info("initWxMa operCrmOpenFeignClient.getConfigAllApp fail"); |
|
|
|
logger.info("wxMaService operCrmOpenFeignClient.getConfigAllApp result:{}", JSON.toJSONString(configAllAppResult)); |
|
|
|
if (configAllAppResult == null || !configAllAppResult.success()) { |
|
|
|
logger.info("wxMaService operCrmOpenFeignClient.getConfigAllApp fail"); |
|
|
|
return; |
|
|
|
} |
|
|
|
String appKey = RedisKeys.getCustomerAppKey(); |
|
|
|
SetOperations<String, String> appSet = redisTemplate.opsForSet(); |
|
|
|
Set<String> members = appSet.members(appKey); |
|
|
|
|
|
|
|
if ( !CollectionUtils.isEmpty(configAllAppResult.getData())) { |
|
|
|
//if (!CollectionUtils.isEmpty(members) && CollectionUtils.isEmpty()){
|
|
|
|
//todo
|
|
|
|
//}
|
|
|
|
maServices = configAllAppResult.getData().stream() |
|
|
|
.map(a -> { |
|
|
|
result = configAllAppResult.getData(); |
|
|
|
result.forEach(app -> { |
|
|
|
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); |
|
|
|
config.setAppid(a.getAppId()); |
|
|
|
config.setSecret(a.getSecret()); |
|
|
|
config.setAppid(app.getAppId()); |
|
|
|
config.setSecret(app.getSecret()); |
|
|
|
config.setMsgDataFormat(JSON_STR); |
|
|
|
|
|
|
|
WxMaService service = new WxMaServiceImpl(); |
|
|
|
service.setWxMaConfig(config); |
|
|
|
redisTemplate.opsForSet().add(appKey,a.getSecret()); |
|
|
|
return service; |
|
|
|
}).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
maServicesNew.put(app.getAppId(), service); |
|
|
|
}); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("init wxMaservice exception",e); |
|
|
|
} finally { |
|
|
|
logger.info("init wxMaservice end"); |
|
|
|
logger.error("init wxMaService from db exception", e); |
|
|
|
} |
|
|
|
if (maServicesNew.size() > 0) { |
|
|
|
maServices = maServicesNew; |
|
|
|
if (appSet != null && result != null) { |
|
|
|
appSet.add(appKey, result); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|