目录
1、公众号路径
微信公众号平台:
https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=795093844w
微信公众号测试平台:
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
账号密码:新微信号
2、搭建步骤
2.1 参考路径
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319
2.2 外网映射工具
windows环境运行
运行 natapp -authtoken=a021a8fe913ea048
http://mtmayikt.natapp1.cc/wx/portal/ wx6f8ce0ccadf3afff
微信公众号框架 https://github.com/Wechat-Group/WxJava
2.3 项目配置
pom.xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
<exclusions>
<exclusion>
<artifactId>xstream</artifactId>
<groupId>com.thoughtworks.xstream</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
<exclusions>
<exclusion>
<artifactId>xstream</artifactId>
<groupId>com.thoughtworks.xstream</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>xstream</artifactId>
<groupId>com.thoughtworks.xstream</groupId>
<version>1.4.10</version>
</dependency>
application.yml
logging:
level:
org.springframework.web: INFO
com.github.binarywang.demo.wx.mp: DEBUG
me.chanjar.weixin: DEBUG
wx:
mp:
configs:
- appId: wx6f8ce0ccadf3afff #(一个公众号的appid)
secret: cf2b0b4908a84d92b38b939a6fbabe09#(公众号的appsecret)
token: mayikt #(接口配置里的Token值)
mayikt:
weixin:
registration:
code:
###微信注册码消息
message: 您的注册码为:registrationCodeMessage,请关注<a href="https://ke.qq.com/course/273548">腾讯课堂免费公开课</a>,欢迎观看97后架构师余老师的精品课程讲解。官方QQ群:<a href='https://jq.qq.com/?_wv=1027&k=5TVfAMF'>193086273</a>,期待你的加入,感谢!
###默认提示消息
default:
registration:
code:
message: 您的消息,我们已经收到,会及时回复给您的!
微信公众号项目demo
@Component
public class MsgHandler extends AbstractHandler {
// 用户发送手机验证码提示
@Value("${mayikt.weixin.registration.code.message}")
private String registrationCodeMessage;
// 默认用户发送验证码提示
@Value("${mayikt.weixin.default.registration.code.message}")
private String defaultRegistrationCodeMessage;
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService,
WxSessionManager sessionManager) {
if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
// TODO 可以选择将消息保存到本地
}
// 当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
try {
if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
&& weixinService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) {
return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())
.toUser(wxMessage.getFromUser()).build();
}
} catch (WxErrorException e) {
e.printStackTrace();
}
// TODO 组装回复消息
// 1.验证关键字是否为手机号码类型
String fromMsg = wxMessage.getContent();
if (RegexUtils.checkMobile(fromMsg)) {
// 如果发送消息为手机号码类型,则发送短信验证码
int registCode = registCode();
String retContext = registrationCodeMessage.replaceAll("registrationCodeMessage", registCode + "");
return new TextBuilder().build(retContext, wxMessage, weixinService);
}
return new TextBuilder().build(defaultRegistrationCodeMessage, wxMessage, weixinService);
}
// 获取注册码
private int registCode() {
int registCode = (int) (Math.random() * 9000 + 1000);
return registCode;
}
}
关注公众号,在公众号里输入手机,返回验证码,之后验证码放入redis