From ab7bb9827acd9e44e7d1e7cd21494bbae9c1d118 Mon Sep 17 00:00:00 2001 From: jianjun Date: Tue, 28 Jul 2020 09:11:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E5=85=BC=E5=AE=B9appId?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/redis/CustomerAppWxServiceUtil.java | 99 +++++++++---------- .../com/epmet/dto/CustomerAppRedisDTO.java | 55 +++++++++++ .../controller/CustomerAppController.java | 6 +- ...IdService.java => CustomerAppService.java} | 2 +- ...eImpl.java => CustomerAppServiceImpl.java} | 33 ++++++- 5 files changed, 135 insertions(+), 60 deletions(-) create mode 100644 epmet-module/oper-crm/oper-crm-client/src/main/java/com/epmet/dto/CustomerAppRedisDTO.java rename epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/{CustomerAppIdService.java => CustomerAppService.java} (96%) rename epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/{CustomerAppIdServiceImpl.java => CustomerAppServiceImpl.java} (71%) diff --git a/epmet-auth/src/main/java/com/epmet/redis/CustomerAppWxServiceUtil.java b/epmet-auth/src/main/java/com/epmet/redis/CustomerAppWxServiceUtil.java index 9d74299267..a7f36c75f0 100644 --- a/epmet-auth/src/main/java/com/epmet/redis/CustomerAppWxServiceUtil.java +++ b/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 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 redisTemplate; + private RedisTemplate redisTemplate; @Autowired private OperCrmOpenFeignClient operCrmOpenFeignClient; - private static Map maServices = Maps.newHashMap(); + private static Map 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 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 maServicesNew = Maps.newConcurrentMap(); + SetOperations appSet = null; + List result = null; + String appKey = RedisKeys.getCustomerAppKey(); + try { + appSet = redisTemplate.opsForSet(); + Set 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> 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 appSet = redisTemplate.opsForSet(); - Set members = appSet.members(appKey); - - if ( !CollectionUtils.isEmpty(configAllAppResult.getData())) { - //if (!CollectionUtils.isEmpty(members) && CollectionUtils.isEmpty()){ - //todo - //} - maServices = configAllAppResult.getData().stream() - .map(a -> { - WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); - config.setAppid(a.getAppId()); - config.setSecret(a.getSecret()); - config.setMsgDataFormat(JSON_STR); + 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); - redisTemplate.opsForSet().add(appKey,a.getSecret()); - return service; - }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a)); - - - - } + WxMaService service = new WxMaServiceImpl(); + service.setWxMaConfig(config); + 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); + } } } } diff --git a/epmet-module/oper-crm/oper-crm-client/src/main/java/com/epmet/dto/CustomerAppRedisDTO.java b/epmet-module/oper-crm/oper-crm-client/src/main/java/com/epmet/dto/CustomerAppRedisDTO.java new file mode 100644 index 0000000000..21b46fc61c --- /dev/null +++ b/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 + *

+ * 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. + *

+ * 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. + *

+ * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +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; + + /** + * resi,work + */ + private String client; + + /** + * app的secret + */ + private String secret; + +} \ No newline at end of file diff --git a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerAppController.java b/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerAppController.java index 4403ea0120..8d6315e4aa 100644 --- a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerAppController.java +++ b/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.dto.CustomerAppDTO; 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.web.bind.annotation.*; @@ -42,9 +42,9 @@ import java.util.Map; @RestController @RequestMapping("customerapp") public class CustomerAppController { - + @Autowired - private CustomerAppIdService customerAppIdService; + private CustomerAppService customerAppIdService; @GetMapping("page") public Result> page(@RequestParam Map params){ diff --git a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppIdService.java b/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppService.java similarity index 96% rename from epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppIdService.java rename to epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppService.java index f8036db8cd..db6c5de4a3 100644 --- a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/CustomerAppIdService.java +++ b/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 * @since v1.0.0 2020-07-27 */ -public interface CustomerAppIdService extends BaseService { +public interface CustomerAppService extends BaseService { /** * 默认分页 diff --git a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppIdServiceImpl.java b/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppServiceImpl.java similarity index 71% rename from epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppIdServiceImpl.java rename to epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppServiceImpl.java index 86c5689b64..c245f3a06e 100644 --- a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/service/impl/CustomerAppIdServiceImpl.java +++ b/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.tools.constant.FieldConstant; import com.epmet.commons.tools.page.PageData; +import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.dao.CustomerAppDao; import com.epmet.dto.CustomerAppDTO; +import com.epmet.dto.CustomerAppRedisDTO; 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.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.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; +import javax.annotation.PostConstruct; import java.util.List; import java.util.Map; /** - * 客户表 appId表 + * 客户表 app表 * * @author generator generator@elink-cn.com * @since v1.0.0 2020-07-27 */ +@Slf4j @Service -public class CustomerAppIdServiceImpl extends BaseServiceImpl implements CustomerAppIdService { +public class CustomerAppServiceImpl extends BaseServiceImpl implements CustomerAppService { + @Autowired + private RedisTemplate redisTemplate; + + @PostConstruct + private void initApp2Redis() { + SetOperations appSet = redisTemplate.opsForSet(); + List configAllApp = this.getConfigAllApp(); + if (!CollectionUtils.isEmpty(configAllApp)) { + String appKey = RedisKeys.getCustomerAppKey(); + List 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 public PageData page(Map params) {