All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.riversoft.weixin.mp.template.Templates Maven / Gradle / Ivy

There is a newer version: 0.9.8
Show newest version
package com.riversoft.weixin.mp.template;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.riversoft.weixin.common.WxClient;
import com.riversoft.weixin.common.exception.WxRuntimeException;
import com.riversoft.weixin.common.util.JsonMapper;
import com.riversoft.weixin.mp.MpWxClientFactory;
import com.riversoft.weixin.mp.base.AppSetting;
import com.riversoft.weixin.mp.base.WxEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 模板消息API
 *
 * Created by exizhai on 12/16/2015.
 */
public class Templates {

    private static Logger logger = LoggerFactory.getLogger(Templates.class);

    private WxClient wxClient;

    public static Templates defaultTemplates() {
        return with(AppSetting.defaultSettings());
    }

    public static Templates with(AppSetting appSetting) {
        Templates templates = new Templates();
        templates.setWxClient(MpWxClientFactory.getInstance().with(appSetting));
        return templates;
    }

    public void setWxClient(WxClient wxClient) {
        this.wxClient = wxClient;
    }

    /**
     * 设置所属行业
     * @param primary 众号模板消息所属行业编号1(主业)
     * @param secondary 众号模板消息所属行业编号2(副业)
     */
    public void setIndustries(String primary, String secondary) {
        String url = WxEndpoint.get("url.template.industry.set");
        String json = String.format("{\"industry_id1\":\"%s\", \"industry_id2\":\"%s\"}", primary, secondary);

        logger.debug("template message, set industry: {}", json);
        wxClient.post(url, json);
    }

    /**
     * 获取所属行业
     */
    public Industry getIndustries() {
        String url = WxEndpoint.get("url.template.industry.get");

        logger.debug("template message, get industry.");
        String response = wxClient.get(url);
        IndustryWrapper industryWrapper = JsonMapper.defaultMapper().fromJson(response, IndustryWrapper.class);
        return new Industry(industryWrapper.getPrimary().toString(), industryWrapper.getSecondary().toString());
    }

    /**
     * 获取模板, 容易被微信的文档误导,这里其实是获取模板到服务号的管理端,并不单纯是获取ID那么简单
     * 对于相同的code,每调用一次微信后台会生成一条新的记录
     * @param code  模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
     * @return
     */
    public String fetch(String code) {
        String url = WxEndpoint.get("url.template.get");
        String json = String.format("{\"template_id_short\":\"%s\"}", code);

        logger.debug("template message, get template id: {}", json);
        String response = wxClient.post(url, json);
        Map result = JsonMapper.defaultMapper().json2Map(response);

        if(result.containsKey("template_id")) {
            return result.get("template_id").toString();
        } else {
            throw new WxRuntimeException(999, "fetch template id failed.");
        }
    }

    /**
     * 获取所有的模板
     * @return
     */
    public List