MyWxCpConfig.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.ghsc.partybuild.controller.wechat;
  2. import me.chanjar.weixin.cp.api.WxCpExternalContactService;
  3. import me.chanjar.weixin.cp.api.WxCpService;
  4. import me.chanjar.weixin.cp.api.impl.WxCpExternalContactServiceImpl;
  5. import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
  6. import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.beans.factory.annotation.Value;
  11. @Configuration
  12. @ConditionalOnClass(WxCpService.class)
  13. public class MyWxCpConfig {
  14. @Value("${wx.cp.configs.corpsecret}")
  15. private String secret;
  16. @Value("${wx.cp.configs.corpId}")
  17. private String corpId;
  18. @Value("${wx.cp.configs.agentId}")
  19. private Integer agentId;
  20. @Value("${wx.cp.configs.redirectUrl}")
  21. private String redirectUrl;
  22. /* @Value("${wx.cp.configs.chatId}")
  23. private String chatId;*/
  24. private static String APP_AGENT_PREFIX = "wx";
  25. /*@Resource
  26. private RedisService redisService;*/
  27. @Bean
  28. public WxCpService getWxCpService() {
  29. WxCpService wxCpService = new WxCpServiceImpl();
  30. WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
  31. config.setAgentId(agentId);
  32. config.setCorpSecret(secret);
  33. config.setCorpId(corpId);
  34. /*resetTokenAndJsApi(wxCpService, config);*/
  35. wxCpService.setWxCpConfigStorage(config);
  36. return wxCpService;
  37. }
  38. @Bean
  39. public WxCpExternalContactService getwxCpExternalContactService() {
  40. WxCpService wxCpService = new WxCpServiceImpl();
  41. WxCpExternalContactService wxCpExternalContactService = new WxCpExternalContactServiceImpl(wxCpService);
  42. WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
  43. config.setAgentId(agentId);
  44. config.setCorpSecret(secret);
  45. config.setCorpId(corpId);
  46. /*resetTokenAndJsApi(wxCpService, config);*/
  47. wxCpService.setWxCpConfigStorage(config);
  48. return wxCpExternalContactService;
  49. }
  50. /*public void resetTokenAndJsApi(WxCpService wxCpService, WxCpDefaultConfigImpl wxCpDefaultConfig) {
  51. wxCpService.setWxCpConfigStorage(wxCpDefaultConfig);
  52. String wxAccessToken = APP_AGENT_PREFIX + this.agentId;
  53. String json = redisService.getCacheObject(wxAccessToken);
  54. if (!StringUtils.isEmpty(json)) {
  55. wxCpDefaultConfig = JSON.parseObject(json, WxCpDefaultConfigImpl.class);
  56. }
  57. if (wxCpDefaultConfig.isAccessTokenExpired()) {
  58. try {
  59. String accessToken = null;
  60. accessToken = wxCpService.getAccessToken(false);
  61. wxCpDefaultConfig.setAccessToken(accessToken);
  62. } catch (WxErrorException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. if(wxCpDefaultConfig.isJsapiTicketExpired()){
  67. String jsApi = null;
  68. try {
  69. jsApi = wxCpService.getJsapiTicket();
  70. wxCpDefaultConfig.setJsapiTicket(jsApi);
  71. } catch (WxErrorException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. redisService.setCacheObject(wxAccessToken, JSON.toJSONString(wxCpDefaultConfig));
  76. }*/
  77. }