微信服务号开发小项目总结

 这三个礼拜一直在忙着做微信服务号的后端开发,要求做对微信用户的登录注册绑定,以及各种消息的回复。

后面再GitHub网站上搜到了不错的微信开发源码,地址:git clone https://github.com/Wechat-Group/weixin-java-tools-springmvc.git

这是微信团队的开源项目代码,封装的不错,运用了观察者模式,可以好好学习研究。

可以看到里面有涉及到微信登录授权的接口调用以及各种消息的方法封装。

对于维信授权获取用户信息之前的随笔有提到过,介绍下微信回复消息的调用以及创建微信订阅号的自定义菜单:

一下的抽象类是消息回复处理的父类

它 实现了处理微信推送消息的接口:

然后可以通过继承 AbstractHandler来实现消息的回复设置,具体代码根据业务需求而来;

对于公众号的自定义菜单创建:

代码进行了很好的封装,在MainConfig类中定义好自己公众号的APPID、APPsecret等属性:

@Configuration
public class MainConfig{

@Value("${wx.appid}")
private String appid;
@Value("${wx.appsecret}")
private String appsecret;
@Value("${wx.token}")
private String token;
@Value("${wx.aeskey}")
private String aesKey;
@Value("${wx.partener_id}")
private String partenerId;
@Value("${wx.partener_key}")
private String partenerKey;

public MainConfig(){};
/**
* 为了生成自定义菜单使用的构造函数,其他情况Spring框架可以直接注入
*
* @param appid
* @param appsecret
* @param token
* @param aesKey
*/
public MainConfig(String appid, String appsecret, String token, String aesKey) {
this.appid = appid;
this.appsecret = appsecret;
this.token = token;
this.aesKey = aesKey;
}

@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
configStorage.setAppId(this.appid);
configStorage.setSecret(this.appsecret);
configStorage.setToken(this.token);
configStorage.setAesKey(this.aesKey);
configStorage.setPartnerId(this.partenerId);
configStorage.setPartnerKey(this.partenerKey);
return configStorage;
}

@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}

}

然后在MenuConfig类中自己定义菜单的结构组成;

最后还有一个模板消息接口的调用,首先前提是要公众号开通模板消息功能,申请需要两三天时间;

然后就可以调用方法来控制模板消息的发送:

原文地址:https://www.cnblogs.com/yzf666/p/6688188.html