小程序开发流程(java后端)

时间:2024-02-15 15:27:55

一、准备工作

  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、新增一类来加载此配置文件并读取相关的内容到静态变量中;

  1 import org.apache.log4j.Logger;
  2 
  3 import java.util.ResourceBundle;
  4 
  5 /**
  6  * @author foreach03
  7  * @company 深圳市
  8  * @create 2018/1/10 13:31
  9  */
 10 public class WxConfigUtils {
 11     private static Logger log = Logger.getLogger(WxConfigUtils.class);
 12     private static String appid;
 13     private static String appsecret;
 14     private static String token;
 15     private static String aeskey;
 16     private static String templateId;
 17     private static String msgDataFormat;
 18     private static String domain;
 19     private static String detail;
 20 
 21     private WxConfigUtils(){
 22 
 23     }
 24     static {
 25         // 加载wxcofig配置文件
 26         ResourceBundle bundle = ResourceBundle.getBundle("properties/wxconfig");
 27         if (bundle == null) {
 28             throw new IllegalArgumentException(
 29                     "[wxcofig.properties] is not found!");
 30         }
 31         appid = bundle.getString("appid");
 32         appsecret = bundle.getString("appsecret");
 33         token = bundle.getString("token");
 34         aeskey = bundle.getString("aeskey");
 35         templateId = bundle.getString("templateId");
37 msgDataFormat = bundle.getString("msgDataFormat"); 38 domain = bundle.getString("domain"); 39 detail = bundle.getString("detail"); 40 41 42 log.info("appid is:" + appid); 43 log.info("appsecret is:" + appsecret); 44 log.info("token is:" + token); 45 log.info("aeskey is:" + aeskey); 46 log.info("templateId is:" + templateId); 47 log.info("msgDataFormat is:" + msgDataFormat); 48 log.info("domain is:" + domain); 49 log.info("detail is:" + detail); 50 } 51 52 public static String getAppid() { 53 return appid; 54 } 55 56 public static void setAppid(String appid) { 57 WxConfigUtils.appid = appid; 58 } 59 60 public static String getAppsecret() { 61 return appsecret; 62 } 63 64 public static void setAppsecret(String appsecret) { 65 WxConfigUtils.appsecret = appsecret; 66 } 67 68 public static String getToken() { 69 return token; 70 } 71 72 public static void setToken(String token) { 73 WxConfigUtils.token = token; 74 } 75 76 public static String getAeskey() { 77 return aeskey; 78 } 79 80 public static void setAeskey(String aeskey) { 81 WxConfigUtils.aeskey = aeskey; 82 } 83 84 public static String getTemplateId() { 85 return templateId; 86 } 87 88 public static void setTemplateId(String templateId) { 89 WxConfigUtils.templateId = templateId; 90 } 91 92 public static String getMsgDataFormat() { 93 return msgDataFormat; 94 } 95 96 public static void setMsgDataFormat(String msgDataFormat) { 97 WxConfigUtils.msgDataFormat = msgDataFormat; 98 } 99 100 public static String getDomain() { 101 return domain; 102 } 103 104 public static void setDomain(String domain) { 105 WxConfigUtils.domain = domain; 106 } 107 108 public static String getDetail() { 109 return detail; 110 } 111 112 public static void setDetail(String detail) { 113 WxConfigUtils.detail = detail; 114 } 115 116 }

3、新增一个常量类WXMPConfig,其实也可以不用此类。

 1 /**
 2  * @author foreach03
 3  * @company 深圳市
 4  * @create 2018/1/10 13:37
 5  */
 6 public class WXMPConfig {
 7     // 服务号常量定义
 8     public static final String APPID = WxConfigUtils.getAppid();//测试
 9     public static final String APPSECRET = WxConfigUtils.getAppsecret();
10     public static final String TOKEN =WxConfigUtils.getToken();
11     public static final String ENCODINGAESKEY =WxConfigUtils.getAeskey();
12     public static final String TEMPLATEID = WxConfigUtils.getTemplateId();
13     public static final String MSGDATAFORMAT = WxConfigUtils.getMsgDataFormat();
14     public static final String DOMAIN = WxConfigUtils.getDomain();
15     public static final String DETAIL = WxConfigUtils.getDetail();
16 
17     private WXMPConfig(){
18 
19     }
20 }

 

4、新建一个工厂类WxMpServiceFactory,用于初始化maservice

 1 import cn.binarywang.wx.miniapp.api.WxMaService;
 2 import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
 3 import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
 4 import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
 5 import com.***.config.constant.WXMPConfig;
 6 
 7 /**
 8  * @author 
 9  * @company 深圳市
10  * @create 2018/1/10 13:25
11  */
12 public class WxMpServiceFactory {
13     private static WxMaService instance;
14 
15     private static WxMaInMemoryConfig wxMaConfigStorage;
16     private static WxMaMessageRouter router = new WxMaMessageRouter(getInstance());
17     private WxMpServiceFactory() {
18         // ......
19     }
20 
21     // 产生WxmaService实例
22     public static WxMaService getInstance() {
23         if(wxMaConfigStorage == null){
24             wxMaConfigStorage = new WxMaInMemoryConfig();
25             wxMaConfigStorage.setAppid(WXMPConfig.APPID);
26             wxMaConfigStorage.setSecret(WXMPConfig.APPSECRET);
27             wxMaConfigStorage.setToken(WXMPConfig.TOKEN);
28             wxMaConfigStorage.setAesKey(WXMPConfig.ENCODINGAESKEY);
29             wxMaConfigStorage.setMsgDataFormat(WXMPConfig.MSGDATAFORMAT);
30         }
31 
32         if (instance == null) {
33             instance = new WxMaServiceImpl();
34             instance.setWxMaConfig(wxMaConfigStorage);
35         }
36 
37         return instance;
38     }
39 
40     public static WxMaInMemoryConfig getWxMaConfigStorage() {
41         return wxMaConfigStorage;
42     }
43 
44     public static void setWxMaConfigStorage(WxMaInMemoryConfig wxMpConfigStorage) {
45         WxMpServiceFactory.wxMaConfigStorage = wxMpConfigStorage;
46     }
47 
48     public static void setInstance(WxMaService instance) {
49         WxMpServiceFactory.instance = instance;
50     }
51 
52     public static WxMaMessageRouter getRouter() {
53         return router;
54     }
55 
56     public static void setRouter(WxMaMessageRouter router) {
57         WxMpServiceFactory.router = router;
58     }
59 
60 }

 5、这样在controller类上就可以通过

  private WxMaService wxService= WxMpServiceFactory.getInstance(); 来初始化;

 6、如需要发送模板消息的

      这里需要注意的是:formid只能提交的那openId才可以使用;

   如果想推送很多的消息的话,那就让前端更可能的收集多一些formId

1  wxService.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
2                         .templateId(自己配置的模板ID)
3                         .formId(前端提交的formId)
4                         .data(datalist)
5                         .toUser(要推送的openid)
6                          .page(就是域名+detail了)
7                         .build());

7、关于一些跳坑的案例,可以参考    http://blog.csdn.net/qq_38530880/article/details/72930341