转:http://blog.csdn.net/omsvip/article/details/39397059
企业号简介:
企业号是公众平台为企业客户提供的微信移动应用入口。它帮助企业建立与员工、上下游供应链及企业应用间的连接。利用企业号,企业或第三方合作伙伴可以帮助企业快速、低成本的实现移动轻应用的部署与应用,实现生产、管理、协作、运营的移动化。
当你成功申领一个企业号后,你可以登录企业号的管理页面,导入通讯录,配置应用,邀请成员关注该企业号,也可以通过应用向成员发送文本、图文、文件、视频、音频等多媒体消息。通过简单的配置,你就可以自动并回复成员发送的消息,实现公告通知、知识管理、企业文化建设、手机企业通讯录等基本的企业应用。
你还可以通过本接口文档所描述的接口,建立企业号同企业应用间的连接,实现更多丰富且个性化的企业移动应用。
建立连接:
企业应用同微信企业号间的连接有以下三种方式:
1、企业应用调用企业号提供的接口,管理或查询企业号后台所管理的资源、或给成员发送消息等,以下称主动调用模式。
2、企业号把用户发送的消息或用户触发的事件推送给企业应用,由企业应用处理,以下称回调模式。
3、用户在微信中阅读企业应用下发的H5页面,该页面可以调用微信提供的原生接口,使用微信开放的终端能力,以下称JSAPI模式。
通过这三种连接方式的结合,企业可以在微信企业号中建立功能强大的移动轻应用,并依托微信数亿活跃用户,帮助企业方便、快捷地实现应用的部署,并确保应用的活跃度。
每一种连接的实现机制不同,以下各节分别描述三种连接机制。
接入(回调模式):
具体实现方法(Java):
首先下载腾讯提供的加密解密包,地址是:微信加密解密地址
Java:http://qydev.weixin.qq.com/java.zip
php:http://qydev.weixin.qq.com/php.zip
C#:http://qydev.weixin.qq.com/csharp.zip
把下次好的文件解压并添加到项目中:
记得添加:commons-codec-1.9.jar
编写核心接入类代码如下:
- <span style="font-size:14px;">package org.oms.qiye.web;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.oms.qiye.aes.AesException;
- import org.oms.qiye.aes.WXBizMsgCrypt;
- import org.oms.qiye.service.CoreService;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- /**
- * 注解方式打开链接
- *
- * @author Sunlight
- *
- */
- @Controller
- public class CoreController {
- private String token = "sunlight";
- private String encodingAESKey = "s8vFF4f6AWay3uAdJh79WD6imaam4BV6Kl4eL4UzgfM";
- private String corpId = "xxxxxxxxxx"; //你的企业号ID
- @RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.GET)
- public void coreJoinGet(HttpServletRequest request,
- HttpServletResponse response) throws IOException {
- // 微信加密签名
- String msg_signature = request.getParameter("msg_signature");
- // 时间戳
- String timestamp = request.getParameter("timestamp");
- // 随机数
- String nonce = request.getParameter("nonce");
- // 随机字符串
- String echostr = request.getParameter("echostr");
- System.out.println("request=" + request.getRequestURL());
- PrintWriter out = response.getWriter();
- // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
- String result = null;
- try {
- WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey,
- corpId);
- result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
- } catch (AesException e) {
- e.printStackTrace();
- }
- if (result == null) {
- result = token;
- }
- out.print(result);
- out.close();
- out = null;
- }
- @RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.POST)
- public void coreJoinPost(HttpServletRequest request,
- HttpServletResponse response) throws IOException {
- // 调用核心业务类接收消息、处理消息
- String respMessage = CoreService.processRequest(request);
- System.out.println("respMessage=" + respMessage);
- // 响应消息
- PrintWriter out = response.getWriter();
- out.print(respMessage);
- out.close();
- }
- }
- </span>
<span style="font-size:14px;">package org.oms.qiye.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.oms.qiye.aes.AesException;
import org.oms.qiye.aes.WXBizMsgCrypt;
import org.oms.qiye.service.CoreService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 注解方式打开链接
*
* @author Sunlight
*
*/
@Controller
public class CoreController {
private String token = "sunlight";
private String encodingAESKey = "s8vFF4f6AWay3uAdJh79WD6imaam4BV6Kl4eL4UzgfM";
private String corpId = "xxxxxxxxxx"; //你的企业号ID
@RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.GET)
public void coreJoinGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// 微信加密签名
String msg_signature = request.getParameter("msg_signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
System.out.println("request=" + request.getRequestURL());
PrintWriter out = response.getWriter();
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
String result = null;
try {
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey,
corpId);
result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
} catch (AesException e) {
e.printStackTrace();
}
if (result == null) {
result = token;
}
out.print(result);
out.close();
out = null;
}
@RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.POST)
public void coreJoinPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// 调用核心业务类接收消息、处理消息
String respMessage = CoreService.processRequest(request);
System.out.println("respMessage=" + respMessage);
// 响应消息
PrintWriter out = response.getWriter();
out.print(respMessage);
out.close();
}
}
</span>
CoreService.java 此类目前没有内容,后面编写消息回复功能!
web.xml文件配置:
- <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>QiyeProject</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!-- 不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml -->
- <param-value>/WEB-INF/mvc-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- 表单处理乱码 -->
- <filter>
- <filter-name>CharacterFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app></span>
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>QiyeProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml -->
<param-value>/WEB-INF/mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 表单处理乱码 -->
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app></span>
mvc-servlet.xml 文件配置:
- <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
- <context:component-scan base-package="org.oms.qiye.web"></context:component-scan>
- <mvc:annotation-driven />
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- <!-- 静态文件处理 -->
- <mvc:resources mapping="/resource/images/**" location="/resource/images/" />
- <mvc:resources mapping="/resource/js/**" location="/resource/js/" />
- <mvc:resources mapping="/resource/css/**" location="/resource/css/" />
- </beans></span>
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="org.oms.qiye.web"></context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 静态文件处理 -->
<mvc:resources mapping="/resource/images/**" location="/resource/images/" />
<mvc:resources mapping="/resource/js/**" location="/resource/js/" />
<mvc:resources mapping="/resource/css/**" location="/resource/css/" />
</beans></span>
此处接入需要注意:
1.com\qq\weixin\mp\aes目录下是用户需要用到的接入企业微信的接口,其中WXBizMsgCrypt.java文件提供的WXBizMsgCrypt类封装了用户接入企业微信的三个接口,其它的类文件用户用于实现加解密,用户无须关心。sample.java文件提供了接口的使用示例。
2.WXBizMsgCrypt封装了VerifyURL, DecryptMsg, EncryptMsg三个接口,分别用于开发者验证回调url、接收消息的解密以及开发者回复消息的加密过程。使用方法可以参考Sample.java文件。
3.请开发者使用jdk1.5以上的版本。针对org.apache.commons.codec.binary.Base64,需要导入jar包commons-codec-1.9(或comm ons-codec-1.8等其他版本),我们有提供,官方下载地址:
http://commons.apache.org/proper/commons-codec/download_codec.cgi
****请特别注意******
4.异常java.security.InvalidKeyException:illegal Key Size的解决方案:
在官方网站下载JCE无限制权限策略文件(JDK7的下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt。如果安装了JRE,将两个jar文件放到%JRE_HOME% \lib\security目录下覆盖原来的文件,如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件。
接入结果:
微信企业号开发交流加QQ群:89714226
转载请注明出处