1. 程式人生 > >【微信】 小程式 推送模板訊息 (java)

【微信】 小程式 推送模板訊息 (java)

大致流程圖:

Created with Raphaël 2.1.2從微信小程式後臺建立模板獲取access_token拼接微信teamplatedata傳送到微信推送訊息

收集form_id

有時我們需要A使用者操作後對B使用者推送模板訊息這是就需要前臺收集form_id以便儲存用直接用來做推送(有效期詳見微信api),由前臺收集後臺將資料存入資料庫,以便將來使用

String formid = getPara("form_id");
WeixinUser weixinUser = getweixinUser();
if (weixinUser==null) {
    fail("儲存失敗,使用者未登入");
    return;
}
String[] split = formid.split(",");
for (int i = 0; i < split.length; i++) {
    WeixinTemplate weixinTemplate = new WeixinTemplate();
    weixinTemplate.setOpenId(weixinUser.getOpenid());
    weixinTemplate.setCreateTime(new Date());
    weixinTemplate.setFormId(split[i]);
    weixinTemplate.setStatus(true);
    weixinTemplate.save();
}
success("儲存成功");

獲取token

    private static final String APPID = "XXXXXXX";
    private static final String SECRET = "XXXXXX";
    private static final String ACCESS_TOKEN = "weixinAPP:access_token";
    /**
     * 獲取access_token
     */
    public static String getAccess_Token(){
    String access_token = CacheUtils.get(ACCESS_TOKEN);
    if(access_token==null){
        String post = HttpKit.post("https://api.weixin.qq.com/cgi-bin/token?appid="+APPID+"&secret="+SECRET+"&grant_type=client_credential","");
        JSONObject jsonObject = (JSONObject)JSON.parse(post);
        access_token = (String)jsonObject.get("access_token");
        CacheUtils.set(ACCESS_TOKEN, access_token, 60*60*2);
    }
    return access_token;
}

編輯訊息模板

public class WeiXinTeamplateMsg implements Serializable{

    private static final long serialVersionUID = 8038149984818112449L;
    private String touser; //接收者的openId
    private String template_id; //模板id
    private String form_id;  
    private TemplateItem data; //資料
    private String page; //跳轉連結
    public String getPage() {
        return page;
    }
    public void setPage(String page) {
        this.page = page;
    }
    public static WeiXinTeamplateMsg New() {
        return new WeiXinTeamplateMsg();
    }
    private WeiXinTeamplateMsg() {
        this.data = new TemplateItem();
    }
    public String getTouser() {
        return touser;
    }
    public WeiXinTeamplateMsg setTouser(String touser) {
        this.touser = touser;
        return this;
    }
    public String getTemplate_id() {
        return template_id;
    }
    public WeiXinTeamplateMsg setTemplate_id(String template_id) {
        this.template_id = template_id;
        return this;
    }
    public String getForm_id() {
        return form_id;
    }

    public WeiXinTeamplateMsg setForm_id(String url) {
        this.form_id = url;
        return this;
    }
    public TemplateItem getData() {
        return data;
    }
    public WeiXinTeamplateMsg add(String key, String value, String color){
        data.put(key, new Item(value, color));
        return this;
    }
    public WeiXinTeamplateMsg add(String key, String value){
        data.put(key, new Item(value));
        return this;
    }
    public String build() {
        return JsonUtils.toJson(this);
    }
    public class TemplateItem extends HashMap<String, Item> {
        private static final long serialVersionUID = -3728490424738325020L;
        public TemplateItem() {}
        public TemplateItem(String key, Item item) {
            this.put(key, item);
        }
    }
    public class Item {
        private Object value;
        private String color;
        public Object getValue() {
            return value;
        }
        public void setValue(Object value) {
            this.value = value;
        }
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
        public Item(Object value) {
            this(value, "#999");
        }
        public Item(Object value, String color) {
            this.value = value;
            this.color = color;
        }
    }
}

傳送拼接資料的資料

public static JSONObject sendWXTemplateMsg(WeiXinTeamplateMsg templateData){
    String json = templateData.build();
    String post = HttpKit.post("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+getAccess_Token(), json);
    return JSONObject.parseObject(post);
}