Browse Source

企业微信接口

pengjing 7 months ago
parent
commit
98ab66c87e

+ 12 - 0
pom.xml

@@ -212,6 +212,18 @@
             <artifactId>fastjson</artifactId>
             <artifactId>fastjson</artifactId>
             <version>1.2.47</version>
             <version>1.2.47</version>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>weixin-java-common</artifactId>
+            <version>4.6.0</version>
+        </dependency>
+        <!-- 企业微信 -->
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>weixin-java-cp</artifactId>
+            <version>4.6.0</version>
+        </dependency>
+
     </dependencies>
     </dependencies>
 
 
     <build>
     <build>

+ 85 - 0
src/main/java/com/ghsc/partybuild/controller/wechat/MyWxCpConfig.java

@@ -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));
+    }*/
+}
+

+ 42 - 0
src/main/java/com/ghsc/partybuild/controller/wechat/WxCpController.java

@@ -0,0 +1,42 @@
+package com.ghsc.partybuild.controller.wechat;
+
+import com.ghsc.partybuild.filter.exception.BaseResponse;
+import com.ghsc.partybuild.filter.exception.RespGenerstor;
+import com.ghsc.partybuild.service.wechat.WechatCpService;
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+
+/**
+ * 企业微信对接接口
+ */
+@RestController
+@RequestMapping(value = "/wxapi/cp")
+@Slf4j
+public class WxCpController {
+
+    @Autowired
+    private WechatCpService wechatCpService;
+
+    @GetMapping("getOAuthUrl")
+    public BaseResponse<String> getOAuthUrl(String redirectUrl) throws UnsupportedEncodingException {
+
+        String url = wechatCpService.getOAuthUrl(URLDecoder.decode(redirectUrl, "UTF-8"));
+
+        log.info("getOAuthUrl:" + url);
+
+        return RespGenerstor.success(url);
+    }
+
+    @GetMapping("getWxCpUserId")
+    public BaseResponse<String> getWxCpUserId(String code) throws WxErrorException {
+        return RespGenerstor.success(wechatCpService.getWxCpUserId(code));
+    }
+
+}

+ 12 - 0
src/main/java/com/ghsc/partybuild/service/wechat/WechatCpService.java

@@ -0,0 +1,12 @@
+package com.ghsc.partybuild.service.wechat;
+
+import me.chanjar.weixin.common.error.WxErrorException;
+
+/**
+ * 企业微信
+ */
+public interface WechatCpService {
+    String getOAuthUrl(String redirectUrl);
+
+    String getWxCpUserId(String code) throws WxErrorException;
+}

+ 48 - 0
src/main/java/com/ghsc/partybuild/service/wechat/WechatCpServiceImpl.java

@@ -0,0 +1,48 @@
+package com.ghsc.partybuild.service.wechat;
+
+import com.ghsc.partybuild.util.DateUtils;
+import com.ghsc.partybuild.util.StringUtils;
+import com.ghsc.partybuild.util.UrlUtils;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.api.WxCpChatService;
+import me.chanjar.weixin.cp.api.WxCpService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * 企业微信
+ */
+@Service("WechatCpService")
+public class WechatCpServiceImpl implements WechatCpService {
+    @Autowired
+    private StringUtils stringUtils;
+
+    @Autowired
+    private DateUtils dateUtils;
+
+    @Autowired
+    private UrlUtils urlUtils;
+
+    @Autowired
+    private WxCpService wxCpService;
+
+    private final Logger logger;
+
+    public WechatCpServiceImpl() {
+        logger = LoggerFactory.getLogger(this.getClass());
+    }
+
+    public String getOAuthUrl(String redirectUrl) {
+        return wxCpService.getOauth2Service().buildAuthorizationUrl(redirectUrl, "");
+    }
+
+    @Override
+    public String getWxCpUserId(String code) throws WxErrorException {
+        String userId = wxCpService.getOauth2Service().getAuthUserInfo(code).getUserId();
+        logger.info("getWxCpUserId:code=" + code + ",userId=" + userId);
+        return userId;
+    }
+
+}

+ 1 - 0
src/main/java/com/ghsc/partybuild/shiro/ShiroConfiguration.java

@@ -119,6 +119,7 @@ public class ShiroConfiguration {
         filterChainDefinitionMap.put("/wxapi/pay/OAuth", "anon");
         filterChainDefinitionMap.put("/wxapi/pay/OAuth", "anon");
         filterChainDefinitionMap.put("/wxapi/pay/getOpenID", "anon");
         filterChainDefinitionMap.put("/wxapi/pay/getOpenID", "anon");
         filterChainDefinitionMap.put("/appApi/fileMgr/showDoc/**", "anon");
         filterChainDefinitionMap.put("/appApi/fileMgr/showDoc/**", "anon");
+        filterChainDefinitionMap.put("/wxapi/cp/**", "anon");
         filterChainDefinitionMap.put("/**", "authc");
         filterChainDefinitionMap.put("/**", "authc");
 
 
         // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
         // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面

+ 34 - 0
src/main/java/com/ghsc/partybuild/util/UrlUtils.java

@@ -0,0 +1,34 @@
+package com.ghsc.partybuild.util;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class UrlUtils {
+
+    @Autowired
+    private StringUtils stringUtils;
+
+    public String getUrl(String url, Map<String, String> params) {
+        if (stringUtils.IsNullOrEmpty(url))
+            return "";
+
+        String fullUrl = url;
+
+        if (fullUrl.indexOf("?") < 0)
+            fullUrl = fullUrl + "?";
+
+        for (Map.Entry<String, String> param : params.entrySet()) {
+            if (!fullUrl.endsWith("?"))
+                fullUrl += "&";
+
+            fullUrl += (param.getKey() + "=" + param.getValue());
+        }
+
+
+        return fullUrl;
+    }
+
+}

+ 8 - 0
src/main/resources/application.yml

@@ -158,3 +158,11 @@ wechat-pay-config:
   #微信授权回调地址
   #微信授权回调地址
   redirectURI: 'http://www.bowintek.com/hbPartyMobile/index.html'
   redirectURI: 'http://www.bowintek.com/hbPartyMobile/index.html'
   accessScope: 'snsapi_base'
   accessScope: 'snsapi_base'
+wx:
+  cp:
+    configs:
+      corpId: wwb4941d05cf8473e4
+      corpsecret: corpsecretid
+      agentId: 1000004
+      redirectUrl: www.bowintek.com/ghsc/mobile
+