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

lodsve.wechat.api.template.WeChatTemplateService Maven / Gradle / Ivy

There is a newer version: 2.7.5-RELEASE
Show newest version
/*
 * Copyright (C) 2018  Sun.Hao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package lodsve.wechat.api.template;

import com.fasterxml.jackson.core.type.TypeReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lodsve.core.utils.StringUtils;
import lodsve.wechat.beans.Industry;
import lodsve.wechat.beans.Template;
import lodsve.wechat.beans.TemplateData;
import lodsve.wechat.core.WeChat;
import lodsve.wechat.core.WeChatRequest;
import lodsve.wechat.core.WeChatUrl;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * 发送模板消息.
 *
 * @author sunhao([email protected])
 * @version V1.0, 16/2/24 下午1:39
 */
@Component
public class WeChatTemplateService {
    /**
     * 设置所属行业,相关代码编号请参考微信公众号开发文档

* 模板消息接口 - 微信公众平台开发者文档 * * @param industryId1 公众号模板消息所属行业编号 * @param industryId2 公众号模板消息所属行业编号 */ public void setIndustry(int industryId1, int industryId2) { Map industry = new HashMap<>(2); industry.put("industry_id1", industryId1); industry.put("industry_id2", industryId2); WeChatRequest.post(String.format(WeChatUrl.API_SET_INDUSTRY, WeChat.accessToken()), industry, new TypeReference() { }); } /** * 获取设置的行业信息 * * @return 行业信息 */ public Industry getIndustry() { return WeChatRequest.get(String.format(WeChatUrl.API_GET_INDUSTRY, WeChat.accessToken()), new TypeReference() { }); } /** * 根据模板短名称获取模板,即在OpenAPI中可以使用 * * @param templateShortId 模板短名称 * @return 模板id */ public String getTemplateId(String templateShortId) { Assert.hasText(templateShortId); Map params = new HashMap<>(1); params.put("template_id_short", templateShortId); Map result = WeChatRequest.post(String.format(WeChatUrl.API_GET_TEMPLATE, WeChat.accessToken()), params, new TypeReference>() { }); return MapUtils.isNotEmpty(result) ? result.get("template_id") : StringUtils.EMPTY; } /** * 获取模板列表 * * @return 模板id */ public List