From 42f8491a3904ec4fa7db0661e89634f18ffb1b56 Mon Sep 17 00:00:00 2001 From: "jy.hu" Date: Fri, 12 Dec 2014 10:51:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=8C=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=89=80=E5=A4=84=E8=A1=8C=E4=B8=9A=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E3=80=8D=E3=80=8C=E6=A8=A1=E6=9D=BF=E6=B6=88=E6=81=AF?= =?UTF-8?q?ID=E8=8E=B7=E5=8F=96=E3=80=8D=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++ weixin4j-mp/README.md | 6 +- weixin4j-mp/weixin4j-mp-api/README.md | 6 +- .../com/foxinmy/weixin4j/mp/WeixinProxy.java | 35 +++++++++++ .../com/foxinmy/weixin4j/mp/api/TmplApi.java | 57 +++++++++++++++++- .../foxinmy/weixin4j/mp/api/weixin.properties | 6 +- .../weixin4j/mp/type/IndustryType.java | 59 +++++++++++++++++++ .../weixin4j/mp/test/msg/TemplateMsgTest.java | 12 ++++ 8 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/type/IndustryType.java diff --git a/README.md b/README.md index 137ffe72..be861e2f 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,10 @@ netty的代码没有放到maven中心仓库,也没什么意义,因为最终需 + 重构了POM + * 2014-12-12 + + + **weixin4j-mp**: 新增设置`模板消息所处行业`、`获取模板消息ID`接口 + 接下来 ------ * 微信小店 diff --git a/weixin4j-mp/README.md b/weixin4j-mp/README.md index ff76c895..b75b70c2 100644 --- a/weixin4j-mp/README.md +++ b/weixin4j-mp/README.md @@ -138,4 +138,8 @@ weixin4j-mp + **weixin4j-mp-api**: 重新定义(手贱)了「被动消息」「客服消息」「群发消息」的传输实体 - + **weixin4j-mp-server**: `WeixinServerBootstrap`重命名为`WeixinMpServerBootstrap` \ No newline at end of file + + **weixin4j-mp-server**: `WeixinServerBootstrap`重命名为`WeixinMpServerBootstrap` + + * 2014-12-12 + + + **weixin4j-mp-api**: 新增设置`模板消息所处行业`、`获取模板消息ID`接口 \ No newline at end of file diff --git a/weixin4j-mp/weixin4j-mp-api/README.md b/weixin4j-mp/weixin4j-mp-api/README.md index 54c0352d..86aed50a 100644 --- a/weixin4j-mp/weixin4j-mp-api/README.md +++ b/weixin4j-mp/weixin4j-mp-api/README.md @@ -119,4 +119,8 @@ weixin.properties说明 * 2014-11-17 - + 新增`冲正`和`被扫支付`接口 \ No newline at end of file + + 新增`冲正`和`被扫支付`接口 + + * 2014-12-12 + + + 新增设置`模板消息所处行业`、`获取模板消息ID`接口 \ No newline at end of file diff --git a/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java b/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java index 9785d47f..74c817c5 100644 --- a/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java +++ b/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java @@ -39,6 +39,7 @@ import com.foxinmy.weixin4j.mp.payment.RefundResult; import com.foxinmy.weixin4j.mp.payment.v2.Order; import com.foxinmy.weixin4j.mp.type.BillType; import com.foxinmy.weixin4j.mp.type.IdQuery; +import com.foxinmy.weixin4j.mp.type.IndustryType; import com.foxinmy.weixin4j.msg.model.Base; import com.foxinmy.weixin4j.msg.model.MpArticle; import com.foxinmy.weixin4j.msg.model.Video; @@ -693,6 +694,40 @@ public class WeixinProxy { return qrApi.getQR(parameter); } + /** + * 设置所属行业(每月可修改行业1次,账号仅可使用所属行业中相关的模板) + * + * @param industryType + * 所处行业 目前不超过两个 + * @return 操作结果 + * @throws WeixinException + * @see com.foxinmy.weixin4j.mp.type.IndustryType + * @see com.foxinmy.weixin4j.mp.api.TmplApi + * @see 设置所处行业 + */ + public JsonResult setTmplIndustry(IndustryType... industryType) + throws WeixinException { + return tmplApi.setTmplIndustry(industryType); + } + + /** + * 获取模板ID + * + * @param shortId + * 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式 + * @return 模板ID + * @throws WeixinException + * @see 获得模板ID + * @see com.foxinmy.weixin4j.mp.api.TmplApi + */ + public String getTemplateId(String shortId) throws WeixinException { + return tmplApi.getTemplateId(shortId); + } + /** * 发送模板消息 * diff --git a/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/api/TmplApi.java b/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/api/TmplApi.java index 8f1037f1..e179af27 100644 --- a/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/api/TmplApi.java +++ b/weixin4j-mp/weixin4j-mp-api/src/main/java/com/foxinmy/weixin4j/mp/api/TmplApi.java @@ -1,11 +1,13 @@ package com.foxinmy.weixin4j.mp.api; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.http.JsonResult; import com.foxinmy.weixin4j.http.Response; import com.foxinmy.weixin4j.model.Token; import com.foxinmy.weixin4j.mp.message.TemplateMessage; +import com.foxinmy.weixin4j.mp.type.IndustryType; import com.foxinmy.weixin4j.token.TokenHolder; /** @@ -25,10 +27,60 @@ public class TmplApi extends MpApi { this.tokenHolder = tokenHolder; } + /** + * 设置所属行业(每月可修改行业1次,账号仅可使用所属行业中相关的模板) + * + * @param industryType + * 所处行业 目前不超过两个 + * @return 操作结果 + * @throws WeixinException + * @see com.foxinmy.weixin4j.mp.type.IndustryType + * @see 设置所处行业 + */ + public JsonResult setTmplIndustry(IndustryType... industryType) + throws WeixinException { + JSONObject obj = new JSONObject(); + for (int i = 0; i < industryType.length; i++) { + obj.put(String.format("industry_id%d", i + 1), + String.valueOf(industryType[i].getValue())); + } + Token token = tokenHolder.getToken(); + String template_set_industry_uri = getRequestUri("template_set_industry_uri"); + Response response = request.post(String.format( + template_set_industry_uri, token.getAccessToken()), obj + .toJSONString()); + + return response.getAsJsonResult(); + } + + /** + * 获取模板ID + * + * @param shortId + * 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式 + * @return 模板ID + * @throws WeixinException + * @see 获得模板ID + */ + public String getTemplateId(String shortId) throws WeixinException { + Token token = tokenHolder.getToken(); + String template_getid_uri = getRequestUri("template_getid_uri"); + Response response = request.post( + String.format(template_getid_uri, token.getAccessToken()), + String.format("{\"template_id_short\":\"%s\"}", shortId)); + + return response.getAsJson().getString("template_id"); + } + /** * 发送模板消息 * - * @param message 消息对象 + * @param message + * 消息对象 * @return 发送结果 * @throws WeixinException * @see 所处行业 + */ +public enum IndustryType { + ITKEJI_HULIANWANG$DIANZISHANGWU("IT科技", "互联网/电子商务", 1), ITKEJI_ITRUANJIANYUFUWU( + "IT科技", "IT软件与服务", 2), ITKEJI_ITYINGJIANYUSHEBEI("IT科技", "IT硬件与设备", + 3), ITKEJI_DIANZIJISHU("IT科技", "电子技术", 4), ITKEJI_TONGXINYUYUNYINGSHANG( + "IT科技", "通信与运营商", 5), ITKEJI_WANGLUOYOUXI("IT科技", "网络游戏", 6), JINRONGYE_YINXING( + "金融业", "银行", 7), JINRONGYE_JIJIN$LICAI$XINTUO("金融业", "基金|理财|信托", 8), JINRONGYE_BAOXIAN( + "金融业", "保险", 9), CANYIN_CANYIN("餐饮", "餐饮", 10), JIUDIANLUYOU_JIUDIAN( + "酒店旅游", "酒店", 11), JIUDIANLUYOU_LUYOU("酒店旅游", "旅游", 12), YUNSHUYUCANGCHU_KUAIDI( + "运输与仓储", "快递", 13), YUNSHUYUCANGCHU_WULIU("运输与仓储", "物流", 14), YUNSHUYUCANGCHU_CANGCHU( + "运输与仓储", "仓储", 15), JIAOYU_PEIXUN("教育", "培训", 16), JIAOYU_YUANXIAO( + "教育", "院校", 17), ZHENGFUYUGONGGONGSHIYE_XUESHUKEYAN("政府与公共事业", + "学术科研", 18), ZHENGFUYUGONGGONGSHIYE_JIAOJING("政府与公共事业", "交警", 19), ZHENGFUYUGONGGONGSHIYE_BOWUGUAN( + "政府与公共事业", "博物馆", 20), ZHENGFUYUGONGGONGSHIYE_GONGGONGSHIYE$FEIYINGLIJIGOU( + "政府与公共事业", "公共事业|非盈利机构", 21), YIYAOHULI_YIYAOYILIAO("医药护理", "医药医疗", + 22), YIYAOHULI_HULIMEIRONG("医药护理", "护理美容", 23), YIYAOHULI_BAOJIANYUWEISHENG( + "医药护理", "保健与卫生", 24), JIAOTONGGONGJU_QICHEXIANGGUAN("交通工具", "汽车相关", + 25), JIAOTONGGONGJU_MOTUOCHEXIANGGUAN("交通工具", "摩托车相关", 26), JIAOTONGGONGJU_HUOCHEXIANGGUAN( + "交通工具", "火车相关", 27), JIAOTONGGONGJU_FEIJIXIANGGUAN("交通工具", "飞机相关", + 28), FANGDICHAN_JIANZHU("房地产", "建筑", 29), FANGDICHAN_WUYE("房地产", + "物业", 30), XIAOFEIPIN_XIAOFEIPIN("消费品", "消费品", 31), SHANGYEFUWU_FALU( + "商业服务", "法律", 32), SHANGYEFUWU_HUIZHAN("商业服务", "会展", 33), SHANGYEFUWU_ZHONGJIEFUWU( + "商业服务", "中介服务", 34), SHANGYEFUWU_RENZHENG("商业服务", "认证", 35), SHANGYEFUWU_SHENJI( + "商业服务", "审计", 36), WENTIYULE_CHUANMEI("文体娱乐", "传媒", 37), WENTIYULE_TIYU( + "文体娱乐", "体育", 38), WENTIYULE_YULEXIUXIAN("文体娱乐", "娱乐休闲", 39), YINSHUA_YINSHUA( + "印刷", "印刷", 40), QITA_QITA("其它", "其它", 41); + private String main; + private String sub; + private int value; + + IndustryType(String main, String sub, int value) { + + this.value = value; + } + + public String getMain() { + return main; + } + + public String getSub() { + return sub; + } + + public int getValue() { + return value; + } +} diff --git a/weixin4j-mp/weixin4j-mp-api/src/test/java/com/foxinmy/weixin4j/mp/test/msg/TemplateMsgTest.java b/weixin4j-mp/weixin4j-mp-api/src/test/java/com/foxinmy/weixin4j/mp/test/msg/TemplateMsgTest.java index aec75632..6614e3c3 100644 --- a/weixin4j-mp/weixin4j-mp-api/src/test/java/com/foxinmy/weixin4j/mp/test/msg/TemplateMsgTest.java +++ b/weixin4j-mp/weixin4j-mp-api/src/test/java/com/foxinmy/weixin4j/mp/test/msg/TemplateMsgTest.java @@ -9,6 +9,7 @@ import com.foxinmy.weixin4j.http.JsonResult; import com.foxinmy.weixin4j.mp.api.TmplApi; import com.foxinmy.weixin4j.mp.message.TemplateMessage; import com.foxinmy.weixin4j.mp.test.TokenTest; +import com.foxinmy.weixin4j.mp.type.IndustryType; public class TemplateMsgTest extends TokenTest { private TmplApi tmplApi; @@ -18,6 +19,17 @@ public class TemplateMsgTest extends TokenTest { this.tmplApi = new TmplApi(tokenHolder); } + @Test + public void setIndustry() throws WeixinException { + System.out.println(tmplApi + .setTmplIndustry(IndustryType.ITKEJI_DIANZIJISHU)); + } + + @Test + public void getid() throws WeixinException { + System.out.println(tmplApi.getTemplateId("OPENTM201490080")); + } + @Test public void test() throws WeixinException { TemplateMessage tplMessage = new TemplateMessage("touser",