-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
dc767cf
commit d388b30
Showing
6 changed files
with
335 additions
and
1 deletion.
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
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,58 @@ | ||
package Utils; | ||
|
||
import java.security.MessageDigest; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by zzg on 2018/2/4. | ||
*/ | ||
public class CheckUtil { | ||
|
||
private static final String token = "iotzzg"; | ||
public static boolean checkSignature(String signature,String timestamp,String nonce){ | ||
String[] arr = new String[]{token,timestamp,nonce}; | ||
//排序 | ||
Arrays.sort(arr); | ||
//生成字符串 | ||
StringBuffer content = new StringBuffer(); | ||
for (int i = 0;i<arr.length;i++){ | ||
content.append(arr[i]); | ||
} | ||
|
||
//sha1加密 | ||
String temp = getSha1(content.toString()); | ||
return temp.equals(signature); | ||
} | ||
|
||
|
||
/** | ||
* SHA1加密算法 | ||
* | ||
* @param str | ||
* @return | ||
*/ | ||
public static String getSha1(String str) { | ||
if (str == null || str.length() == 0) { | ||
return null; | ||
} | ||
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | ||
'a', 'b', 'c', 'd', 'e', 'f'}; | ||
try { | ||
MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); | ||
mdTemp.update(str.getBytes("UTF-8")); | ||
|
||
byte[] md = mdTemp.digest(); | ||
int j = md.length; | ||
char buf[] = new char[j * 2]; | ||
int k = 0; | ||
for (int i = 0; i < j; i++) { | ||
byte byte0 = md[i]; | ||
buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; | ||
buf[k++] = hexDigits[byte0 & 0xf]; | ||
} | ||
return new String(buf); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} |
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,109 @@ | ||
package Utils; | ||
|
||
import com.example.wechar.model.TextMessage; | ||
import com.thoughtworks.xstream.XStream; | ||
import org.dom4j.Document; | ||
import org.dom4j.Element; | ||
import org.dom4j.io.SAXReader; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.InputStream; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by zzg on 2018/2/4. | ||
*/ | ||
public class MessageUtil { | ||
|
||
public static final String MESSAGE_TEXT = "text"; | ||
public static final String MESSAGE_IMAGE = "image"; | ||
public static final String MESSAGE_VOICE = "voice"; | ||
public static final String MESSAGE_VIDEO = "video"; | ||
public static final String MESSAGE_LINK = "link"; | ||
public static final String MESSAGE_LOCATION = "location"; | ||
public static final String MESSAGE_EVENT = "event"; | ||
public static final String MESSAGE_SUBSCRIBE = "subscribe"; | ||
public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe"; | ||
public static final String MESSAGE_CLICK = "CLICK"; | ||
public static final String MESSAGE_VIEW = "VIEW"; | ||
|
||
|
||
/** | ||
* xml转map对象 | ||
* @param request | ||
* @return | ||
*/ | ||
public static Map<String, String> xmlToMap(HttpServletRequest request) throws Exception{ | ||
|
||
// 新建Map对象,用来存储信息 | ||
Map<String, String> map = new HashMap<>(); | ||
// 获取输入流 | ||
InputStream inputStream = request.getInputStream(); | ||
System.out.println(inputStream.toString()); | ||
// 将输入流转为Dom对象 | ||
SAXReader reader = new SAXReader(); | ||
Document document = reader.read(inputStream); | ||
|
||
// 得到xml根元素 | ||
Element root = document.getRootElement(); | ||
// 得到根元素的所有子节点 | ||
List<Element> elementList = root.elements(); | ||
|
||
// 遍历所有子节点 | ||
for (Element e : elementList) | ||
map.put(e.getName(), e.getText()); | ||
|
||
// 释放资源 | ||
inputStream.close(); | ||
return map; | ||
} | ||
|
||
/** | ||
* 对象转文本xml | ||
* @param textMessage | ||
* @return | ||
*/ | ||
public static String textMessageToXml(TextMessage textMessage) { | ||
XStream xStream = new XStream(); | ||
xStream.alias("xml",textMessage.getClass()); | ||
return xStream.toXML(textMessage); | ||
} | ||
|
||
public static String initText(String toUserName,String fromUserName,String content){ | ||
TextMessage text = new TextMessage(); | ||
text.setFromUserName(toUserName); | ||
text.setToUserName(fromUserName); | ||
text.setMsgType(MessageUtil.MESSAGE_TEXT); | ||
text.setCreateTime(new Date().getTime()); | ||
text.setContent(content); | ||
return textMessageToXml(text); | ||
} | ||
/** | ||
* 主菜单 | ||
* | ||
* @return | ||
*/ | ||
public static String menuText() { | ||
StringBuffer sb = new StringBuffer(); | ||
sb.append("欢迎你的关注,请按照菜单提示操作:\n\n"); | ||
sb.append("1、课程介绍\n"); | ||
sb.append("2、慕课网介绍\n"); | ||
sb.append("回复?调出此菜单。"); | ||
return sb.toString(); | ||
} | ||
|
||
public static String firstMenu(){ | ||
StringBuffer sb = new StringBuffer(); | ||
sb.append("本套教程介绍微信公众号开发,主要涉及微信公众号介绍,编辑模式介绍,开发模式介绍等"); | ||
return sb.toString(); | ||
} | ||
|
||
public static String secondMenu(){ | ||
StringBuffer sb = new StringBuffer(); | ||
sb.append("慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好的互联网技术牛人," + | ||
"也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。"); | ||
return sb.toString(); | ||
} | ||
} |
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,80 @@ | ||
package com.example.wechar; | ||
|
||
import Utils.CheckUtil; | ||
import Utils.MessageUtil; | ||
import com.example.wechar.model.TextMessage; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by zzg on 2018/2/4. | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/wechar") | ||
public class CheckOutController { | ||
|
||
@GetMapping(value = "/checkout") | ||
public String checkout(@RequestParam(value = "signature", required = false, defaultValue = "1") String signature, | ||
@RequestParam(value = "timestamp", required = false, defaultValue = "1") String timestamp, | ||
@RequestParam(value = "nonce", required = false, defaultValue = "1") String nonce, | ||
@RequestParam(value = "echostr", required = false, defaultValue = "1") String echostr) { | ||
|
||
if (CheckUtil.checkSignature(signature, timestamp, nonce)) { | ||
System.out.println("接入成功"); | ||
return echostr; | ||
} else { | ||
System.out.println("接入失败"); | ||
return "fall"; | ||
} | ||
} | ||
|
||
/** | ||
* 处理微信请求信息 | ||
* @param request | ||
* @return | ||
*/ | ||
@PostMapping(value = "/checkout") | ||
public String handleMessage(HttpServletRequest request) { | ||
|
||
try { | ||
Map<String, String> map = MessageUtil.xmlToMap(request); | ||
String fromUserName = map.get("FromUserName"); | ||
String toUserName = map.get("ToUserName"); | ||
String msgType = map.get("MsgType"); | ||
String content = map.get("Content"); | ||
String s=null; | ||
if (MessageUtil.MESSAGE_TEXT.equals(msgType)){ | ||
if ("1".equals(content)) { | ||
s = MessageUtil.initText(toUserName,fromUserName,MessageUtil.firstMenu()); | ||
} else if ("2".equals(content)) { | ||
s = MessageUtil.initText(toUserName,fromUserName,MessageUtil.secondMenu()); | ||
} else if ("?".equals(content) || "?".equals(content)) { | ||
s = MessageUtil.initText(toUserName,fromUserName,MessageUtil.menuText()); | ||
} | ||
// TextMessage textMessage = new TextMessage(); | ||
// textMessage.setToUserName(fromUserName); | ||
// textMessage.setFromUserName(toUserName); | ||
// textMessage.setMsgType("text"); | ||
// textMessage.setCreateTime(new Date().getTime()); | ||
// textMessage.setContent("您发送的消息为:"+content); | ||
// s = MessageUtil.textMessageToXml(textMessage); | ||
} else if (MessageUtil.MESSAGE_EVENT.equals(msgType)){ | ||
String evenType = map.get("Event"); | ||
if (MessageUtil.MESSAGE_SUBSCRIBE.equals(evenType)){ | ||
s = MessageUtil.initText(toUserName,fromUserName,MessageUtil.menuText()); | ||
} | ||
} | ||
System.out.println(s); | ||
return s; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
|
||
} | ||
|
||
|
||
} |
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,69 @@ | ||
package com.example.wechar.model; | ||
|
||
/** | ||
* Created by zzg on 2018/2/5. | ||
*/ | ||
public class TextMessage { | ||
// 开发者微信号 | ||
private String ToUserName; | ||
// 发送方帐号(一个OpenID) | ||
private String FromUserName; | ||
// 消息创建时间 (整型) | ||
private long CreateTime; | ||
// 消息类型(text/image/location/link) | ||
private String MsgType; | ||
|
||
// 文本消息内容 | ||
private String Content; | ||
|
||
// 消息id,64位整型 | ||
private long MsgId; | ||
|
||
public String getContent() { | ||
return Content; | ||
} | ||
|
||
public void setContent(String content) { | ||
Content = content; | ||
} | ||
|
||
public String getToUserName() { | ||
return ToUserName; | ||
} | ||
|
||
public void setToUserName(String toUserName) { | ||
ToUserName = toUserName; | ||
} | ||
|
||
public String getFromUserName() { | ||
return FromUserName; | ||
} | ||
|
||
public void setFromUserName(String fromUserName) { | ||
FromUserName = fromUserName; | ||
} | ||
|
||
public long getCreateTime() { | ||
return CreateTime; | ||
} | ||
|
||
public void setCreateTime(long createTime) { | ||
CreateTime = createTime; | ||
} | ||
|
||
public String getMsgType() { | ||
return MsgType; | ||
} | ||
|
||
public void setMsgType(String msgType) { | ||
MsgType = msgType; | ||
} | ||
|
||
public long getMsgId() { | ||
return MsgId; | ||
} | ||
|
||
public void setMsgId(long msgId) { | ||
MsgId = msgId; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
server: | ||
port: 8671 | ||
port: 8080 |