|
@@ -0,0 +1,85 @@
|
|
|
+package com.ghsc.partybuild.controller.wechat;
|
|
|
+
|
|
|
+import me.chanjar.weixin.cp.api.WxCpExternalContactService;
|
|
|
+import me.chanjar.weixin.cp.api.WxCpService;
|
|
|
+import me.chanjar.weixin.cp.api.impl.WxCpExternalContactServiceImpl;
|
|
|
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
|
|
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@ConditionalOnClass(WxCpService.class)
|
|
|
+public class MyWxCpConfig {
|
|
|
+ @Value("${wx.cp.configs.corpsecret}")
|
|
|
+ private String secret;
|
|
|
+ @Value("${wx.cp.configs.corpId}")
|
|
|
+ private String corpId;
|
|
|
+ @Value("${wx.cp.configs.agentId}")
|
|
|
+ private Integer agentId;
|
|
|
+ @Value("${wx.cp.configs.redirectUrl}")
|
|
|
+ private String redirectUrl;
|
|
|
+ /* @Value("${wx.cp.configs.chatId}")
|
|
|
+ private String chatId;*/
|
|
|
+ private static String APP_AGENT_PREFIX = "wx";
|
|
|
+
|
|
|
+ /*@Resource
|
|
|
+ private RedisService redisService;*/
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public WxCpService getWxCpService() {
|
|
|
+ WxCpService wxCpService = new WxCpServiceImpl();
|
|
|
+ WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
|
|
|
+ config.setAgentId(agentId);
|
|
|
+ config.setCorpSecret(secret);
|
|
|
+ config.setCorpId(corpId);
|
|
|
+ /*resetTokenAndJsApi(wxCpService, config);*/
|
|
|
+ wxCpService.setWxCpConfigStorage(config);
|
|
|
+ return wxCpService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public WxCpExternalContactService getwxCpExternalContactService() {
|
|
|
+ WxCpService wxCpService = new WxCpServiceImpl();
|
|
|
+ WxCpExternalContactService wxCpExternalContactService = new WxCpExternalContactServiceImpl(wxCpService);
|
|
|
+ WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
|
|
|
+ config.setAgentId(agentId);
|
|
|
+ config.setCorpSecret(secret);
|
|
|
+ config.setCorpId(corpId);
|
|
|
+ /*resetTokenAndJsApi(wxCpService, config);*/
|
|
|
+ wxCpService.setWxCpConfigStorage(config);
|
|
|
+ return wxCpExternalContactService;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*public void resetTokenAndJsApi(WxCpService wxCpService, WxCpDefaultConfigImpl wxCpDefaultConfig) {
|
|
|
+ wxCpService.setWxCpConfigStorage(wxCpDefaultConfig);
|
|
|
+ String wxAccessToken = APP_AGENT_PREFIX + this.agentId;
|
|
|
+ String json = redisService.getCacheObject(wxAccessToken);
|
|
|
+ if (!StringUtils.isEmpty(json)) {
|
|
|
+ wxCpDefaultConfig = JSON.parseObject(json, WxCpDefaultConfigImpl.class);
|
|
|
+ }
|
|
|
+ if (wxCpDefaultConfig.isAccessTokenExpired()) {
|
|
|
+ try {
|
|
|
+ String accessToken = null;
|
|
|
+ accessToken = wxCpService.getAccessToken(false);
|
|
|
+ wxCpDefaultConfig.setAccessToken(accessToken);
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(wxCpDefaultConfig.isJsapiTicketExpired()){
|
|
|
+ String jsApi = null;
|
|
|
+ try {
|
|
|
+ jsApi = wxCpService.getJsapiTicket();
|
|
|
+ wxCpDefaultConfig.setJsapiTicket(jsApi);
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ redisService.setCacheObject(wxAccessToken, JSON.toJSONString(wxCpDefaultConfig));
|
|
|
+ }*/
|
|
|
+}
|
|
|
+
|