WechatWorkConfig.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.ydtech.config;
  2. import com.ydtech.config.properties.WechatCustomerProperties;
  3. import com.ydtech.config.properties.WechatOpenProperties;
  4. import me.chanjar.weixin.cp.api.WxCpKfService;
  5. import me.chanjar.weixin.cp.api.WxCpService;
  6. import me.chanjar.weixin.cp.api.impl.WxCpKfServiceImpl;
  7. import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
  8. import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
  9. import me.chanjar.weixin.mp.api.WxMpService;
  10. import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
  11. import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.Configuration;
  15. import javax.annotation.Resource;
  16. import java.util.Map;
  17. import java.util.stream.Collectors;
  18. /**
  19. * @version
  20. * @description: 微信相关配置
  21. * @author: wenks
  22. * @date: 2024/11/27 15:01
  23. **/
  24. @Configuration
  25. public class WechatWorkConfig {
  26. @Value("${wechat.cp.corp-id}")
  27. private String corpId;
  28. @Value("${wechat.cp.secret}")
  29. private String secret;
  30. @Value("${wechat.cp.agent-id}")
  31. private Integer agentId;
  32. @Resource
  33. private WechatOpenProperties wechatOpenProperties;
  34. @Resource
  35. private WechatCustomerProperties wechatCustomerProperties;
  36. @Bean
  37. public WxCpService wxCpService() {
  38. WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
  39. config.setCorpId(corpId);
  40. config.setCorpSecret(secret);
  41. config.setAgentId(agentId);
  42. WxCpServiceImpl wxCpService = new WxCpServiceImpl();
  43. wxCpService.setWxCpConfigStorage(config);
  44. return new WxCpServiceImpl();
  45. }
  46. @Bean
  47. public Map<String, WxMpService> wxMpService() {
  48. Map<String, WxMpDefaultConfigImpl> collect = wechatOpenProperties.getProperties().keySet()
  49. .stream()
  50. .collect(Collectors.toMap(x->x, x -> {
  51. WechatOpenProperties.PropertiesDto propertiesDto = wechatOpenProperties.getProperties().get(x);
  52. WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
  53. wxMpDefaultConfig.setAppId(propertiesDto.getComponentAppId());
  54. wxMpDefaultConfig.setSecret(propertiesDto.getComponentSecret());
  55. return wxMpDefaultConfig;
  56. }));
  57. return collect.keySet().stream().collect(Collectors.toMap(x -> x, x -> {
  58. WxMpServiceImpl wxMpService = new WxMpServiceImpl();
  59. WxMpDefaultConfigImpl wxMpDefaultConfig = collect.get(x);
  60. wxMpService.setWxMpConfigStorage(wxMpDefaultConfig);
  61. return wxMpService;
  62. }));
  63. }
  64. @Bean
  65. public WxCpKfService wxCpKfService(){
  66. WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
  67. config.setCorpId(corpId);
  68. config.setCorpSecret(wechatCustomerProperties.getSecret());
  69. config.setToken(wechatCustomerProperties.getToken());
  70. config.setAesKey(wechatCustomerProperties.getEncodingAesKey());
  71. WxCpServiceImpl wxCpService = new WxCpServiceImpl();
  72. wxCpService.setWxCpConfigStorage(config);
  73. return new WxCpKfServiceImpl(wxCpService());
  74. }
  75. }