您的位置:首页 > 移动开发 > 微信开发

小程序开发流程(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文件,用于配置

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: