时间:2017年08月11日星期五
说明:本文部份内容均来自慕课网。@慕课网:http://www.imooc.com
教学源码:https://github.com/zccodere/s...
学习源码:https://github.com/zccodere/s...html
面向人群前端
对微信公众号开发感兴趣 有必定Java开发经验或基础
课程介绍java
介绍本套课程学习内容、开发语言等 微信公众号介绍、申请以及后台设置详解 编辑模式下的消息回复、菜单创建、素材管理等 开发前的环境搭建以及工具准备 开发者模式的切换、设置以及消息的接收与响应 百度BEA服务器的搭建、代码上传
公众号与微信区别git
公众号类型介绍github
最新公众号类型web
企业号与服务号、订阅号的区别spring
公众号申请apache
微信公众平台:https://mp.weixin.qq.com/
微信公众平台介绍编程
微信公众平台是腾讯为了让用户申请和管理微信公众帐号而推出的一个WEB平台
本章内容服务器
在编辑模式下实现消息自动回复、菜单建立;以及在微信公众号平台推送消息给关注用户。
单图文与多图文
自定义菜单有两种类型
发送信息:click类型 跳转到网页:view类型
开发环境准备
1.一个微信公众号 2.外网映射工具(开发调试) 与微信对接的url要具有如下条件: --在公网上可以访问 --端口只支持80端口
映射工具
ngrok能够将内网映射到公网上面,这样就能够在公网访问你的本地网络服务
ngrok用法
1.下载ngrok.xex可执行程序,而后在DOS输入如下命令 2.ngrok http 8080 3.ngrok –config ngrok.cfg –subdomain example 8080 帮助文档:https://natapp.cn/
开发模式
数据交互原理
接入指南
地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319
项目搭建
建立一个名为wxdevaccess的maven项目,POM文件以下
<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.myimooc</groupId> <artifactId>wxdevaccess</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>wxdevaccess</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
说明:因为条件限制,此项目代码均没有进行测试,这里只是显示大概开发过程。
代码演示:
1.编写AccressReqVo类
package com.myimooc.wxdevaccess.domain; /** * 服务器验证请求Vo * @author ZhangCheng on 2017-08-11 * */ public class AccressReqVo { private String signature; private String timestamp; private String nonce; private String echostr; @Override public String toString() { return "AccressReqVo [signature=" + signature + ", timestamp=" + timestamp + ", nonce=" + nonce + ", echostr=" + echostr + "]"; } public String getSignature() { return signature; } public void setSignature(String signature) { this.signature = signature; } public String getTimestamp() { return timestamp; } public void setTimestamp(String timestamp) { this.timestamp = timestamp; } public String getNonce() { return nonce; } public void setNonce(String nonce) { this.nonce = nonce; } public String getEchostr() { return echostr; } public void setEchostr(String echostr) { this.echostr = echostr; } }
2.编写AccessRest类
package com.myimooc.wxdevaccess.rest; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.myimooc.wxdevaccess.domain.AccressReqVo; import com.myimooc.wxdevaccess.util.CheckUtils; /** * 开发模式接入 * @author ZhangCheng on 2017-08-11 * */ @RestController public class AccessRest { /** * 接收微信服务器发送的GET验证请求 */ @GetMapping("wx") public String access(AccressReqVo vo){ if(CheckUtils.checkSignature(vo)){ return vo.getEchostr(); } return "error"; } }
3.编写CheckUtils类
package com.myimooc.wxdevaccess.util; import java.security.MessageDigest; import java.util.Arrays; import java.util.Objects; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.myimooc.wxdevaccess.domain.AccressReqVo; /** * 开发模式接入-服务器验证-校验工具类 * @author ZhangCheng on 2017-08-11 * */ public class CheckUtils { private static final Logger logger = LoggerFactory.getLogger(CheckUtils.class); /** * 开发者模式-开发者本身填写的 token (令牌) */ private static final String token = "myimooc"; /** * 功能:验证消息的确来自微信服务器 */ public static boolean checkSignature(AccressReqVo vo){ logger.info("验证:{}",vo.toString()); String[] arr = new String[]{token,vo.getTimestamp(),vo.getNonce()}; // 排序 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()); logger.info("sha1 微信传入密文:{}",vo.getSignature()); return Objects.equals(vo.getSignature(), temp); }; /** * 经过 JDK 实现SHA-1 加密 */ public static String getSha1(String str){ logger.info("sha1 加密前字符串:{}",str); try { MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(str.getBytes()); String result = DigestUtils.sha1Hex(md.digest()); logger.info("sha1 加密后字符串:{}",result); return result; } catch (Exception e) { logger.info("sha1 加密异常:{}",e); e.printStackTrace(); } return null; } }
消息类型
文本消息:text 图片消息:image 语音消息:voice 视频消息:video 连接消息:link 地理位置:location 事件推送:event --关注:subscribe --取消关注:unsubscribe --菜单点击:CLICK、VIEW
代码演示:
1.编写TextMessage类
package com.myimooc.wxdevaccess.domain; /** * 文本消息-向微信接口发起请求Vo * @author ZhangCheng on 2017-08-11 * */ public class TextMessage { private String ToUserName; private String FromUserName; private Long CreateTime; private String MsgType; private String Content; private String MsgId; @Override public String toString() { return "TextMessage [ToUserName=" + ToUserName + ", FromUserName=" + FromUserName + ", CreateTime=" + CreateTime + ", MsgType=" + MsgType + ", Content=" + Content + ", MsgId=" + MsgId + "]"; } 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 String getContent() { return Content; } public void setContent(String content) { Content = content; } public String getMsgId() { return MsgId; } public void setMsgId(String msgId) { MsgId = msgId; } }
2.编写EventMessage类
package com.myimooc.wxdevaccess.domain; /** * 事件推送-向微信接口发起请求Vo * @author ZhangCheng on 2017-08-11 * */ public class EventMessage { private String ToUserName; private String FromUserName; private Long CreateTime; private String MsgType; private String Event; @Override public String toString() { return "EventMessage [ToUserName=" + ToUserName + ", FromUserName=" + FromUserName + ", CreateTime=" + CreateTime + ", MsgType=" + MsgType + ", Event=" + Event + "]"; } 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 String getEvent() { return Event; } public void setEvent(String event) { Event = event; } }
3.编写MessageUtils类
package com.myimooc.wxdevaccess.util; import java.util.Date; import com.myimooc.wxdevaccess.domain.TextMessage; /** * 消息类型及工具类 * @author ZhangCheng on 2017-08-11 * */ public class MessageUtils { 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"; public static TextMessage initText(String toUserName,String fromUserName,String content){ TextMessage text = new TextMessage(); text.setFromUserName(toUserName); text.setToUserName(fromUserName); text.setMsgType(MessageUtils.MESSAGE_TEXT); text.setCreateTime(new Date().getTime()); text.setContent(content); return text; } /** * 主菜单 */ public static String menuText(){ StringBuffer sb = new StringBuffer(); sb.append("欢迎您的关注,请按照菜单提高进行操做:\n\n"); sb.append("一、课程介绍\n"); sb.append("二、慕课网介绍\n\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技术。" + "慕课网课程涵盖前端开发、PHP、Html五、Android、iOS、Swift等IT前沿技术语言," + "包括基础课程、实用案例、高级分享三大类型,适合不一样阶段的学习人群。" + "以纯干货、短视频的形式为平台特色,为在校学生、职场白领提供了一个迅速提高技能、共同分享进步的学习平台。"); return sb.toString(); } }
4.编写MessageRest类
package com.myimooc.wxdevaccess.rest; import java.util.Date; import java.util.Objects; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import com.myimooc.wxdevaccess.domain.EventMessage; import com.myimooc.wxdevaccess.domain.TextMessage; import com.myimooc.wxdevaccess.util.MessageUtils; /** * 处理消息请求与响应 * @author ZhangCheng on 2017-08-11 * */ @RestController public class MessageRest { /** * 接收微信服务器发送的POST请求 */ @PostMapping("textmessage") public Object textmessage(TextMessage msg){ // 文本消息 if(Objects.equals(MessageUtils.MESSAGE_TEXT, msg.getMsgType())){ TextMessage textMessage = new TextMessage(); // 关键字 1 if(Objects.equals("1", msg.getContent())){ textMessage = MessageUtils.initText(msg.getToUserName(), msg.getFromUserName(), MessageUtils.firstMenu()); return textMessage; } // 关键字 2 if(Objects.equals("2", msg.getContent())){ textMessage = MessageUtils.initText(msg.getToUserName(), msg.getFromUserName(), MessageUtils.secondMenu()); return textMessage; } // 关键字 ?? 调出菜单 if(Objects.equals("?", msg.getContent()) || Objects.equals("?", msg.getContent())){ textMessage = MessageUtils.initText(msg.getToUserName(), msg.getFromUserName(), MessageUtils.menuText()); return textMessage; } // 非关键字 textMessage.setFromUserName(msg.getToUserName()); textMessage.setToUserName(msg.getFromUserName()); textMessage.setMsgType(MessageUtils.MESSAGE_TEXT); textMessage.setCreateTime(new Date().getTime()); textMessage.setContent("您发送的消息是:" + msg.getContent()); return textMessage; } return null; } /** * 接收微信服务器发送的POST请求 */ @PostMapping("eventmessage") public Object eventmessage(EventMessage msg){ // 事件推送 if(Objects.equals(MessageUtils.MESSAGE_EVENT, msg.getMsgType())){ // 关注 if(Objects.equals(MessageUtils.MESSAGE_SUBSCRIBE, msg.getEvent())){ TextMessage text = new TextMessage(); text = MessageUtils.initText(msg.getToUserName(), msg.getFromUserName(), MessageUtils.menuText()); return text; } } return null; } }
百度BAE介绍
百度应用引擎(BAE)是百度推出的网络应用开发平台,开发者不须要进行服务器的维护、设置等繁琐的操做,只须要简单的上传本身的应用便可在网络上访问。
百度开放服务平台
地址:http://developer.baidu.com/ 搜索BAE(应用引擎) 地址:https://cloud.baidu.com/product/bae.html 使用BAE部署项目war包