小程序开发流程(java后端)
2018-02-02 15:09
555 查看
一、准备工作
1、参考https://mp.weixin.qq.com/debug/wxadoc/dev/?t=1476434677599 先注册账号;
2、下载官网前端开发工具 https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=1476434678461
3、其他的接口及文档请参考 https://mp.weixin.qq.com/debug/wxadoc/introduction/index.html?t=2018125
二、java后端
1、先去github下载sdk https://github.com/Wechat-Group/weixin-java-tools
2、demo参考 https://github.com/binarywang/weixin-java-miniapp-demo 注: 此demo是采用了jdk8和spring boot开发的。
三、本人折腾过程
我本地是采用jdk7,spring mvc和mybatis的搭建框架;
1、在resources/properties新增wxconfig.properties文件,用于配置
2、新增一类来加载此配置文件并读取相关的内容到静态变量中;
3、新增一个常量类WXMPConfig,其实也可以不用此类。
4、新建一个工厂类WxMpServiceFactory,用于初始化maservice
5、这样在controller类上就可以通过
private WxMaService wxService= WxMpServiceFactory.getInstance(); 来初始化;
6、如需要发送模板消息的
这里需要注意的是:formid只能提交的那openId才可以使用;
如果想推送很多的消息的话,那就让前端更可能的收集多一些formId
7、关于一些跳坑的案例,可以参考 http://blog.csdn.net/qq_38530880/article/details/72930341
1、参考https://mp.weixin.qq.com/debug/wxadoc/dev/?t=1476434677599 先注册账号;
2、下载官网前端开发工具 https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=1476434678461
3、其他的接口及文档请参考 https://mp.weixin.qq.com/debug/wxadoc/introduction/index.html?t=2018125
二、java后端
1、先去github下载sdk https://github.com/Wechat-Group/weixin-java-tools
2、demo参考 https://github.com/binarywang/weixin-java-miniapp-demo 注: 此demo是采用了jdk8和spring boot开发的。
三、本人折腾过程
我本地是采用jdk7,spring mvc和mybatis的搭建框架;
1、在resources/properties新增wxconfig.properties文件,用于配置
appid=填写自己注册的小程序 appsecret=密钥(通过获取或是重置方式取到,在客服消息转发需要用) token=自己定义 aeskey=加密 templateId=推送消息模块ID,也是自己在小程序后台-->模板消息中获取; msgDataFormat=JSON (传送的格式还有或者是XML) domain=推送模板消息时,如需要点详情时(因为开发小程序有可能是有开发或线上二个域名的) detail=详情的链接,和在app.json中配置的一样
2、新增一类来加载此配置文件并读取相关的内容到静态变量中;
import org.apache.log4j.Logger; import java.util.ResourceBundle; /** * @author foreach03 * @company 深圳市 * @create 2018/1/10 13:31 */ public class WxConfigUtils { private static Logger log = Logger.getLogger(WxConfigUtils.class); private static String appid; private static String appsecret; private static String token; private static String aeskey; private static String templateId; private static String msgDataFormat; private static String domain; private static String detail; private WxConfigUtils(){ } static { // 加载wxcofig配置文件 ResourceBundle bundle = ResourceBundle.getBundle("properties/wxconfig"); if (bundle == null) { throw new IllegalArgumentException( "[wxcofig.properties] is not found!"); } appid = bundle.getString("appid"); appsecret = bundle.getString("appsecret"); token = bundle.getString("token"); aeskey = bundle.getString("aeskey"); templateId = bundle.getString("templateId"); msgDataFormat = bundle.getString("msgDataFormat"); domain = bundle.getString("domain"); detail = bundle.getString("detail"); log.info("appid is:" + appid); log.info("appsecret is:" + appsecret); log.info("token is:" + token); log.info("aeskey is:" + aeskey); log.info("templateId is:" + templateId); log.info("msgDataFormat is:" + msgDataFormat); log.info("domain is:" + domain); log.info("detail is:" + detail); } public static String getAppid() { return appid; } public static void setAppid(String appid) { WxConfigUtils.appid = appid; } public static String getAppsecret() { return appsecret; } public static void setAppsecret(String appsecret) { WxConfigUtils.appsecret = appsecret; } public static String getToken() { return token; } public static void setToken(String token) { WxConfigUtils.token = token; } public static String getAeskey() { return aeskey; } public static void setAeskey(String aeskey) { WxConfigUtils.aeskey = aeskey; } public static String getTemplateId() { return templateId; } public static void setTemplateId(String templateId) { WxConfigUtils.templateId = templateId; } public static String getMsgDataFormat() { return msgDataFormat; } public static void setMsgDataFormat(String msgDataFormat) { WxConfigUtils.msgDataFormat = msgDataFormat; } public static String getDomain() { return domain; } public static void setDomain(String domain) { WxConfigUtils.domain = domain; } public static String getDetail() { return detail; } public static void setDetail(String detail) { WxConfigUtils.detail = detail; } }
3、新增一个常量类WXMPConfig,其实也可以不用此类。
/** * @author foreach03 * @company 深圳市 * @create 2018/1/10 13:37 */ public class WXMPConfig { // 服务号常量定义 public static final String APPID = WxConfigUtils.getAppid();//测试 public static final String APPSECRET = WxConfigUtils.getAppsecret(); public static final String TOKEN =WxConfigUtils.getToken(); public static final String ENCODINGAESKEY =WxConfigUtils.getAeskey(); public static final String TEMPLATEID = WxConfigUtils.getTemplateId(); public static final String MSGDATAFORMAT = WxConfigUtils.getMsgDataFormat(); public static final String DOMAIN = WxConfigUtils.getDomain(); public static final String DETAIL = WxConfigUtils.getDetail(); private WXMPConfig(){ } }
4、新建一个工厂类WxMpServiceFactory,用于初始化maservice
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; import cn.binarywang.wx.miniapp.message.WxMaMessageRouter; import com.***.config.constant.WXMPConfig; /** * @author * @company 深圳市 * @create 2018/1/10 13:25 */ public class WxMpServiceFactory { private static WxMaService instance; private static WxMaInMemoryConfig wxMaConfigStorage; private static WxMaMessageRouter router = new WxMaMessageRouter(getInstance()); private WxMpServiceFactory() { // ...... } // 产生WxmaService实例 public static WxMaService getInstance() { if(wxMaConfigStorage == null){ wxMaConfigStorage = new WxMaInMemoryConfig(); wxMaConfigStorage.setAppid(WXMPConfig.APPID); wxMaConfigStorage.setSecret(WXMPConfig.APPSECRET); wxMaConfigStorage.setToken(WXMPConfig.TOKEN); wxMaConfigStorage.setAesKey(WXMPConfig.ENCODINGAESKEY); wxMaConfigStorage.setMsgDataFormat(WXMPConfig.MSGDATAFORMAT); } if (instance == null) { instance = new WxMaServiceImpl(); instance.setWxMaConfig(wxMaConfigStorage); } return instance; } public static WxMaInMemoryConfig getWxMaConfigStorage() { return wxMaConfigStorage; } public static void setWxMaConfigStorage(WxMaInMemoryConfig wxMpConfigStorage) { WxMpServiceFactory.wxMaConfigStorage = wxMpConfigStorage; } public static void setInstance(WxMaService instance) { WxMpServiceFactory.instance = instance; } public static WxMaMessageRouter getRouter() { return router; } public static void setRouter(WxMaMessageRouter router) { WxMpServiceFactory.router = router; } }
5、这样在controller类上就可以通过
private WxMaService wxService= WxMpServiceFactory.getInstance(); 来初始化;
6、如需要发送模板消息的
这里需要注意的是:formid只能提交的那openId才可以使用;
如果想推送很多的消息的话,那就让前端更可能的收集多一些formId
wxService.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() .templateId(自己配置的模板ID) .formId(前端提交的formId) .data(datalist) .toUser(要推送的openid) .page(就是域名+detail了) .build());
7、关于一些跳坑的案例,可以参考 http://blog.csdn.net/qq_38530880/article/details/72930341
相关文章推荐
- [置顶] java后端 三方登录之微信登录 开发详细流程以及遇到过的坑 总结(非常详细) 2017-12-26
- (十三)Core Java GUI(图形化界面小程序开发,事件监听机制流程,以及可运行jar包的配置实现,) (109)
- android java开发 第二天 之熟悉 程序开发流程
- JAVA_SE基础——3.Java程序的开发流程
- Java程序开发流程(图文解说版)
- 面向对象技术JAVA(NO.0004)-JAVA程序的开发、执行流程
- 用JBuilder高效率开发Java程序
- 程序开发流程
- 用ANT 开发 Java 程序
- JAVA之第3章 控制程序流程
- [开发技巧] Java调用可执行程序和批处理命令
- JAVA 主要用来开发什么样类型的程序比较多?
- 在Redhat9 Linux下安装java开发环境,并编写“Hello world”程序
- 用netbeans快速开发java桌面程序(一) 新建JFrame
- 从Java谈软件开发流程
- 用netbeans快速开发java桌面程序(四) 让组件自动调解大小
- 用netbeans快速开发java桌面程序(五) 添加事件
- JAVA程序开发小经验 - 使用ObjectStream会出现的问题
- 用editplus 或者ultraedit开发java程序
- 毫无废话: 从0开始一点一滴用java开发自己的B/S模式程序---5.2