|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
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.form.CustomerAppSecretFormDTO;
|
|
|
|
import com.epmet.feign.OperCrmOpenFeignClient;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
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;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 客户app Redis
|
|
|
|
*
|
|
|
|
* @author Mark sunlightcs@gmail.com
|
|
|
|
* @since 1.0.0
|
|
|
|
*/
|
|
|
|
@Component
|
|
|
|
public class CustomerAppWxServiceUtil implements ApplicationRunner {
|
|
|
|
private Logger logger = LogManager.getLogger(CustomerAppWxServiceUtil.class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 过期时长为30分钟,单位:秒
|
|
|
|
*/
|
|
|
|
private final static long MINUTE_THIRTY_EXPIRE = 60 * 60 * 24 * 7L;
|
|
|
|
private final static String JSON_STR = "JSON";
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private RedisUtils redisUtils;
|
|
|
|
@Autowired
|
|
|
|
private OperCrmOpenFeignClient operCrmOpenFeignClient;
|
|
|
|
|
|
|
|
private static Map<String, WxMaService> maServices = Maps.newHashMap();
|
|
|
|
|
|
|
|
private void initWxMa() {
|
|
|
|
|
|
|
|
}
|
|
|
|
public WxMaService getWxMaService(String appId) {
|
|
|
|
WxMaService wxMaService = maServices.get(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) throws Exception {
|
|
|
|
Result<List<CustomerAppDTO>> configAllAppResult = operCrmOpenFeignClient.getConfigAllApp();
|
|
|
|
if (configAllAppResult != null && configAllAppResult.success() && !CollectionUtils.isEmpty(configAllAppResult.getData())) {
|
|
|
|
maServices = configAllAppResult.getData().stream()
|
|
|
|
.map(a -> {
|
|
|
|
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
|
|
|
config.setAppid(a.getAppId());
|
|
|
|
config.setSecret(a.getSecret());
|
|
|
|
config.setMsgDataFormat(JSON_STR);
|
|
|
|
|
|
|
|
WxMaService service = new WxMaServiceImpl();
|
|
|
|
service.setWxMaConfig(config);
|
|
|
|
return service;
|
|
|
|
}).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a));
|
|
|
|
}
|
|
|
|
logger.info("initWxMa success");
|
|
|
|
}
|
|
|
|
}
|