-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangqisheng
committed
Aug 9, 2018
1 parent
0ffeefb
commit 141671c
Showing
9 changed files
with
540 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.ansitech</groupId> | ||
<artifactId>weixin4j-example-web</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.9.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.weixin4j</groupId> | ||
<artifactId>weixin4j</artifactId> | ||
<version>0.1.1</version> | ||
</dependency> | ||
</dependencies> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>2.6</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<!--指定main入口--> | ||
<mainClass>com.ansitech.weixin4j.example.Application</mainClass> | ||
<addClasspath>true</addClasspath> | ||
<classpathPrefix>lib/</classpathPrefix> | ||
</manifest> | ||
<manifestEntries> | ||
<Class-Path>./</Class-Path> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<configuration> | ||
<!-- not append assembly id in release file name --> | ||
<appendAssemblyId>false</appendAssemblyId> | ||
<descriptors> | ||
<!--打包的详细描述,需要配置额外文件--> | ||
<descriptor>src/main/resources/package.xml</descriptor> | ||
</descriptors> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>make-assembly</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
19 changes: 19 additions & 0 deletions
19
weixin4j-example-web/src/main/java/com/ansitech/weixin4j/example/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ansitech.weixin4j.example; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.ComponentScan; | ||
|
||
/** | ||
* 程序入口 | ||
* | ||
* @author yangqisheng | ||
*/ | ||
@SpringBootApplication | ||
@ComponentScan | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...ple-web/src/main/java/com/ansitech/weixin4j/example/controller/WeixinJieruController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.ansitech.weixin4j.example.controller; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletInputStream; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.weixin4j.WeixinException; | ||
import org.weixin4j.spi.HandlerFactory; | ||
import org.weixin4j.spi.IMessageHandler; | ||
import org.weixin4j.util.TokenUtil; | ||
|
||
/** | ||
* 微信开发者接入 | ||
* | ||
* @author yangqisheng | ||
*/ | ||
@Controller | ||
@RequestMapping("/weixin/jieru") | ||
public class WeixinJieruController { | ||
|
||
//开发者接入验证 | ||
@RequestMapping(method = RequestMethod.GET) | ||
public void get(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
//消息来源可靠性验证 | ||
String signature = request.getParameter("signature");// 微信加密签名 | ||
String timestamp = request.getParameter("timestamp");// 时间戳 | ||
String nonce = request.getParameter("nonce"); // 随机数 | ||
//Token为weixin4j.properties中配置的Token | ||
String token = TokenUtil.get(); | ||
//1.验证消息真实性 | ||
//http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性 | ||
//成为开发者验证 | ||
String echostr = request.getParameter("echostr"); | ||
//确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败 | ||
if (TokenUtil.checkSignature(token, signature, timestamp, nonce)) { | ||
response.getWriter().write(echostr); | ||
} | ||
} | ||
|
||
//接收微信消息 | ||
@RequestMapping(method = RequestMethod.POST) | ||
public void post(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
//消息来源可靠性验证 | ||
String signature = request.getParameter("signature");// 微信加密签名 | ||
String timestamp = request.getParameter("timestamp");// 时间戳 | ||
String nonce = request.getParameter("nonce"); // 随机数 | ||
//Token为weixin4j.properties中配置的Token | ||
String token = TokenUtil.get(); | ||
//确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败 | ||
if (!TokenUtil.checkSignature(token, signature, timestamp, nonce)) { | ||
//消息不可靠,直接返回 | ||
response.getWriter().write(""); | ||
return; | ||
} | ||
//用户每次向公众号发送消息、或者产生自定义菜单点击事件时,响应URL将得到推送 | ||
try { | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setContentType("text/xml"); | ||
//获取POST流 | ||
ServletInputStream in = request.getInputStream(); | ||
//非注解方式,依然采用消息处理工厂模式调用 | ||
IMessageHandler messageHandler = HandlerFactory.getMessageHandler(); | ||
//处理输入消息,返回结果 | ||
String xml = messageHandler.invoke(in); | ||
//返回结果 | ||
response.getWriter().write(xml); | ||
} catch (IOException | WeixinException ex) { | ||
response.getWriter().write(""); | ||
} | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...ample-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsEventMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.ansitech.weixin4j.example.handler; | ||
|
||
import org.weixin4j.model.message.OutputMessage; | ||
import org.weixin4j.model.message.event.ClickEventMessage; | ||
import org.weixin4j.model.message.event.LocationEventMessage; | ||
import org.weixin4j.model.message.event.LocationSelectEventMessage; | ||
import org.weixin4j.model.message.event.PicPhotoOrAlbumEventMessage; | ||
import org.weixin4j.model.message.event.PicSysPhotoEventMessage; | ||
import org.weixin4j.model.message.event.PicWeixinEventMessage; | ||
import org.weixin4j.model.message.event.QrsceneScanEventMessage; | ||
import org.weixin4j.model.message.event.QrsceneSubscribeEventMessage; | ||
import org.weixin4j.model.message.event.ScanCodePushEventMessage; | ||
import org.weixin4j.model.message.event.ScanCodeWaitMsgEventMessage; | ||
import org.weixin4j.model.message.event.SubscribeEventMessage; | ||
import org.weixin4j.model.message.event.UnSubscribeEventMessage; | ||
import org.weixin4j.model.message.event.ViewEventMessage; | ||
import org.weixin4j.model.message.output.TextOutputMessage; | ||
import org.weixin4j.spi.IEventMessageHandler; | ||
|
||
/** | ||
* 自定义事件消息处理器 | ||
* | ||
* @author yangqisheng | ||
*/ | ||
public class AtsEventMessageHandler implements IEventMessageHandler { | ||
|
||
@Override | ||
public OutputMessage subscribe(SubscribeEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("感谢您的关注!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage unSubscribe(UnSubscribeEventMessage msg) { | ||
//取消关注 | ||
return null; | ||
} | ||
|
||
@Override | ||
public OutputMessage qrsceneSubscribe(QrsceneSubscribeEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("感谢您的关注!,来源:" + msg.getEventKey()); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage qrsceneScan(QrsceneScanEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage location(LocationEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage click(ClickEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("点击了菜单!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage view(ViewEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("点击了链接!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage scanCodePush(ScanCodePushEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("扫码!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage scanCodeWaitMsg(ScanCodeWaitMsgEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("扫码等待中!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage picSysPhoto(PicSysPhotoEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("发起拍照!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage picPhotoOrAlbum(PicPhotoOrAlbumEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("选择相册!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage picWeixin(PicWeixinEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("上次图片!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage locationSelect(LocationSelectEventMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("选择地理位置!"); | ||
return out; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...mple-web/src/main/java/com/ansitech/weixin4j/example/handler/AtsNormalMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.ansitech.weixin4j.example.handler; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.weixin4j.model.message.OutputMessage; | ||
import org.weixin4j.model.message.normal.ImageInputMessage; | ||
import org.weixin4j.model.message.normal.LinkInputMessage; | ||
import org.weixin4j.model.message.normal.LocationInputMessage; | ||
import org.weixin4j.model.message.normal.ShortVideoInputMessage; | ||
import org.weixin4j.model.message.normal.TextInputMessage; | ||
import org.weixin4j.model.message.normal.VideoInputMessage; | ||
import org.weixin4j.model.message.normal.VoiceInputMessage; | ||
import org.weixin4j.model.message.output.TextOutputMessage; | ||
import org.weixin4j.spi.INormalMessageHandler; | ||
|
||
/** | ||
* 自定义普通消息处理器 | ||
* | ||
* @author yangqisheng | ||
*/ | ||
public class AtsNormalMessageHandler implements INormalMessageHandler { | ||
|
||
protected final Logger LOG = LoggerFactory.getLogger(AtsNormalMessageHandler.class); | ||
|
||
@Override | ||
public OutputMessage textTypeMsg(TextInputMessage msg) { | ||
LOG.debug("文本消息:" + msg.getContent()); | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("您发的消息是:" + msg.getContent()); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage imageTypeMsg(ImageInputMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage voiceTypeMsg(VoiceInputMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage videoTypeMsg(VideoInputMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage shortvideoTypeMsg(ShortVideoInputMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage locationTypeMsg(LocationInputMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
@Override | ||
public OutputMessage linkTypeMsg(LinkInputMessage msg) { | ||
TextOutputMessage out = new TextOutputMessage(); | ||
out.setContent("你的消息已经收到!"); | ||
return out; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
server: | ||
port: 8080 |
Oops, something went wrong.