package com.ydtech.config; import com.ydtech.config.properties.WechatCustomerProperties; import com.ydtech.config.properties.WechatOpenProperties; import me.chanjar.weixin.cp.api.WxCpKfService; import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.impl.WxCpKfServiceImpl; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; import java.util.Map; import java.util.stream.Collectors; /** * @version * @description: 微信相关配置 * @author: wenks * @date: 2024/11/27 15:01 **/ @Configuration public class WechatWorkConfig { @Value("${wechat.cp.corp-id}") private String corpId; @Value("${wechat.cp.secret}") private String secret; @Value("${wechat.cp.agent-id}") private Integer agentId; @Resource private WechatOpenProperties wechatOpenProperties; @Resource private WechatCustomerProperties wechatCustomerProperties; @Bean public WxCpService wxCpService() { WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl(); config.setCorpId(corpId); config.setCorpSecret(secret); config.setAgentId(agentId); WxCpServiceImpl wxCpService = new WxCpServiceImpl(); wxCpService.setWxCpConfigStorage(config); return new WxCpServiceImpl(); } @Bean public Map wxMpService() { Map collect = wechatOpenProperties.getProperties().keySet() .stream() .collect(Collectors.toMap(x->x, x -> { WechatOpenProperties.PropertiesDto propertiesDto = wechatOpenProperties.getProperties().get(x); WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl(); wxMpDefaultConfig.setAppId(propertiesDto.getComponentAppId()); wxMpDefaultConfig.setSecret(propertiesDto.getComponentSecret()); return wxMpDefaultConfig; })); return collect.keySet().stream().collect(Collectors.toMap(x -> x, x -> { WxMpServiceImpl wxMpService = new WxMpServiceImpl(); WxMpDefaultConfigImpl wxMpDefaultConfig = collect.get(x); wxMpService.setWxMpConfigStorage(wxMpDefaultConfig); return wxMpService; })); } @Bean public WxCpKfService wxCpKfService(){ WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl(); config.setCorpId(corpId); config.setCorpSecret(wechatCustomerProperties.getSecret()); config.setToken(wechatCustomerProperties.getToken()); config.setAesKey(wechatCustomerProperties.getEncodingAesKey()); WxCpServiceImpl wxCpService = new WxCpServiceImpl(); wxCpService.setWxCpConfigStorage(config); return new WxCpKfServiceImpl(wxCpService()); } }