Browse Source

优化 兼容appId代码

dev_shibei_match
jianjun 5 years ago
parent
commit
ab7bb9827a
  1. 99
      epmet-auth/src/main/java/com/epmet/redis/CustomerAppWxServiceUtil.java
  2. 55
      epmet-module/oper-crm/oper-crm-client/src/main/java/com/epmet/dto/CustomerAppRedisDTO.java
  3. 6
      epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerAppController.java
  4. 2
      epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppService.java
  5. 33
      epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppServiceImpl.java

99
epmet-auth/src/main/java/com/epmet/redis/CustomerAppWxServiceUtil.java

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

55
epmet-module/oper-crm/oper-crm-client/src/main/java/com/epmet/dto/CustomerAppRedisDTO.java

@ -0,0 +1,55 @@
/**
* Copyright 2018 人人开源 https://www.renren.io
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.epmet.dto;
import lombok.Data;
import java.io.Serializable;
/**
* 客户表 app redis 缓存对象
*
* @author generator generator@elink-cn.com
* @since v1.0.0 2020-07-27
*/
@Data
public class CustomerAppRedisDTO implements Serializable {
private static final long serialVersionUID = -1619187268905620766L;
/**
* 客户Id
*/
private String customerId;
/**
* 小程序的appId
*/
private String appId;
/**
* resiwork
*/
private String client;
/**
* app的secret
*/
private String secret;
}

6
epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerAppController.java

@ -25,7 +25,7 @@ import com.epmet.commons.tools.validator.group.DefaultGroup;
import com.epmet.commons.tools.validator.group.UpdateGroup; import com.epmet.commons.tools.validator.group.UpdateGroup;
import com.epmet.dto.CustomerAppDTO; import com.epmet.dto.CustomerAppDTO;
import com.epmet.dto.form.CustomerAppSecretFormDTO; import com.epmet.dto.form.CustomerAppSecretFormDTO;
import com.epmet.service.CustomerAppIdService; import com.epmet.service.CustomerAppService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -42,9 +42,9 @@ import java.util.Map;
@RestController @RestController
@RequestMapping("customerapp") @RequestMapping("customerapp")
public class CustomerAppController { public class CustomerAppController {
@Autowired @Autowired
private CustomerAppIdService customerAppIdService; private CustomerAppService customerAppIdService;
@GetMapping("page") @GetMapping("page")
public Result<PageData<CustomerAppDTO>> page(@RequestParam Map<String, Object> params){ public Result<PageData<CustomerAppDTO>> page(@RequestParam Map<String, Object> params){

2
epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppIdService.java → epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppService.java

@ -31,7 +31,7 @@ import java.util.Map;
* @author generator generator@elink-cn.com * @author generator generator@elink-cn.com
* @since v1.0.0 2020-07-27 * @since v1.0.0 2020-07-27
*/ */
public interface CustomerAppIdService extends BaseService<CustomerAppEntity> { public interface CustomerAppService extends BaseService<CustomerAppEntity> {
/** /**
* 默认分页 * 默认分页

33
epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppIdServiceImpl.java → epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppServiceImpl.java

@ -22,26 +22,53 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; import com.epmet.commons.mybatis.service.impl.BaseServiceImpl;
import com.epmet.commons.tools.constant.FieldConstant; import com.epmet.commons.tools.constant.FieldConstant;
import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.page.PageData;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.dao.CustomerAppDao; import com.epmet.dao.CustomerAppDao;
import com.epmet.dto.CustomerAppDTO; import com.epmet.dto.CustomerAppDTO;
import com.epmet.dto.CustomerAppRedisDTO;
import com.epmet.entity.CustomerAppEntity; import com.epmet.entity.CustomerAppEntity;
import com.epmet.service.CustomerAppIdService; import com.epmet.service.CustomerAppService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* 客户表 appId * 客户表 app表
* *
* @author generator generator@elink-cn.com * @author generator generator@elink-cn.com
* @since v1.0.0 2020-07-27 * @since v1.0.0 2020-07-27
*/ */
@Slf4j
@Service @Service
public class CustomerAppIdServiceImpl extends BaseServiceImpl<CustomerAppDao, CustomerAppEntity> implements CustomerAppIdService { public class CustomerAppServiceImpl extends BaseServiceImpl<CustomerAppDao, CustomerAppEntity> implements CustomerAppService {
@Autowired
private RedisTemplate redisTemplate;
@PostConstruct
private void initApp2Redis() {
SetOperations<String, CustomerAppRedisDTO> appSet = redisTemplate.opsForSet();
List<CustomerAppDTO> configAllApp = this.getConfigAllApp();
if (!CollectionUtils.isEmpty(configAllApp)) {
String appKey = RedisKeys.getCustomerAppKey();
List<CustomerAppRedisDTO> redisDTOS = ConvertUtils.sourceToTarget(configAllApp, CustomerAppRedisDTO.class);
if (CollectionUtils.isEmpty(redisDTOS)) {
log.error("initApp2Redis fail,convert return null");
return;
}
redisDTOS.forEach(app -> appSet.add(appKey, app));
log.info("initApp2Redis success");
}
}
@Override @Override
public PageData<CustomerAppDTO> page(Map<String, Object> params) { public PageData<CustomerAppDTO> page(Map<String, Object> params) {
Loading…
Cancel
Save