烟台政务云平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.3 KiB

5 years ago
/**
* Copyright (c) 2018 人人开源 All rights reserved.
* <p>
* https://www.renren.io
* <p>
* 版权所有侵权必究
*/
package com.epmet.redis;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
5 years ago
import com.alibaba.fastjson.JSON;
5 years ago
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.utils.ConvertUtils;
5 years ago
import com.epmet.commons.tools.utils.Result;
import com.epmet.dto.CustomerAppDTO;
import com.epmet.dto.CustomerAppRedisDTO;
5 years ago
import com.epmet.feign.OperCrmOpenFeignClient;
import com.google.common.collect.Maps;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
5 years ago
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
5 years ago
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
5 years ago
import java.util.Set;
5 years ago
/**
* 客户app Redis
*
* @author Mark sunlightcs@gmail.com
* @since 1.0.0
*/
@Component
public class CustomerAppWxServiceUtil implements ApplicationRunner {
5 years ago
private static Logger logger = LogManager.getLogger(CustomerAppWxServiceUtil.class);
5 years ago
/**
* 过期时长为30分钟单位
*/
private final static long MINUTE_THIRTY_EXPIRE = 60 * 60 * 24 * 7L;
private final static String JSON_STR = "JSON";
@Autowired
private RedisTemplate redisTemplate;
5 years ago
@Autowired
5 years ago
private OperCrmOpenFeignClient operCrmOpenFeignClient;
private static Map<String, WxMaService> maServices = Maps.newConcurrentMap();
5 years ago
public static WxMaService getWxMaService(String appId) {
5 years ago
WxMaService wxMaService = maServices.get(appId);
if (wxMaService == null) {
logger.error("getMaService appId:{} is not config from customer_app", appId);
5 years ago
}
return wxMaService;
}
@Override
5 years ago
public void run(ApplicationArguments args) {
initWxMaService();
}
public Set<String> initWxMaService() {
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);
}
5 years ago
try {
Result<List<CustomerAppDTO>> configAllAppResult = operCrmOpenFeignClient.getConfigAllApp();
logger.info("wxMaService operCrmOpenFeignClient.getConfigAllApp result:{}", JSON.toJSONString(configAllAppResult));
if (configAllAppResult == null || !configAllAppResult.success()) {
logger.info("wxMaService operCrmOpenFeignClient.getConfigAllApp fail");
return maServicesNew.keySet();
5 years ago
}
result = configAllAppResult.getData();
result.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);
});
5 years ago
} catch (Exception e) {
logger.error("init wxMaService from db exception", e);
}
if (maServicesNew.size() > 0) {
maServices = maServicesNew;
if (appSet != null && result != null) {
for (CustomerAppDTO app : result) {
CustomerAppRedisDTO appRedis = ConvertUtils.sourceToTarget(app, CustomerAppRedisDTO.class);
if(appRedis == null){
logger.warn("init CustomerAppRedis fail,convert return null,appDB:{}",JSON.toJSONString(app));
continue;
}
appSet.add(appKey, appRedis);
}
}
}
return maServicesNew.keySet();
}
5 years ago
}