目录
1 开发中遇到的问题汇总
首先是在测试中,遇到最多的就是41003->appid错误,然后就是40165,说什么page和pagepath,其实就是线上和体验的问题,反正不管怎么改发出去就行,跳转到正确的页面即可,但是大问题是:公众号的文档然后里面发送模板消息是小程序的,然后一直就是appid错误,然后突然发现还有个文档,修改body后测试成功.按照我的方式是肯定成功的,因为有的博客都是从官方文档抄一遍测都不测.
3.1 微信公众号消息模板
3.1.1 基本信息
-
接口状态:
开发中
-
接口URL:
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=49_pWR_MJBhGBi97IMi0pzXqkxphglh9YzKPs8Wu962QjtVeVL1MbnTLHTL11mUMPFrhJMjPTB6OGvmzcs9w1MJG8xQVbhFalmh5K7WtOHLR7pQDhupwLCKkQGrqsb8sy-OIAkDr2CzWtQVlBOIRTNdAGAEGA
-
请求方式:
POST
-
Content-Type:
application/json
3.2 请求参数
3.2.1 Query参数及说明
参数名 | 示例值 | 参数类型 | 是否必填 | 参数描述 |
---|---|---|---|---|
access_token | 49_pWR_MJBhGBi97IMi0pzXqkxphglh9YzKPs8Wu962QjtVeVL1MbnTLHTL11mUMPFrhJMjPTB6OGvmzcs9w1MJG8xQVbhFalmh5K7WtOHLR7pQDhupwLCKkQGrqsb8sy-OIAkDr2CzWtQVlBOIRTNdAGAEGA | 是 | 暂无描述 |
3.2.2 body参数及说明
{
"touser": "oUtXT6JTkW8oeQbRb-wBe9CMucyU",
"template_id": "Eeh7tc4SiJ9W49QSyjTeuCgscRTjBq_DEmf1BNmq_VU",
"url": "http://weixin.qq.com/download",
"topcolor": "#FF0000",
"data": {
"first": {
"value": "恭喜你购买成功!",
"color": "#173177"
},
"keyword1": {
"value": "巧克力",
"color": "#173177"
},
"keyword2": {
"value": "39.8元",
"color": "#173177"
},
"keyword3": {
"value": "2014年9月22日",
"color": "#173177"
},
"remark": {
"value": "欢迎再次购买!",
"color": "#173177"
}
}
}
4 测试结果
其实开发过程中并不是很复杂,只是文档有一些问题,容易歧义
// 获取token
String token = saveAndFlushAccessTokenUtil.getToken();
String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser", "发送到用户的openid"); // openid
jsonObject.put("template_id", "你的模板id");
jsonObject.put("url", "http://www.baidu.com");
JSONObject data = new JSONObject();
JSONObject first = new JSONObject();
first.put("value", "hello");
first.put("color", "#173177");
JSONObject keyword1 = new JSONObject();
keyword1.put("value", "hello");
keyword1.put("color", "#173177");
JSONObject keyword2 = new JSONObject();
keyword2.put("value", "hello");
keyword2.put("color", "#173177");
JSONObject keyword3 = new JSONObject();
keyword3.put("value", "hello");
keyword3.put("color", "#173177");
JSONObject remark = new JSONObject();
remark.put("value", "hello");
remark.put("color", "#173177");
data.put("first",first);
data.put("keyword1",keyword1);
data.put("keyword2",keyword2);
data.put("keyword3",keyword3);
data.put("remark",remark);
jsonObject.put("data", data);
String string = HttpClientUtils.sendPostJsonStr(postUrl, jsonObject.toJSONString());
JSONObject result = JSON.parseObject(string);
int errcode = result.getIntValue("errcode");
if(errcode == 0){
// 发送成功
System.out.println("发送成功");
} else {
// 发送失败
System.out.println("发送失败");
}
6 常见问题
有时候你会发现你的jsonobject中进行转字符串会被转义,然后发出去的模板消息读不到内容,可能是因为你放入的Json字符串,之后又进行了一次jsonObject.toJSONString(),之后就被转义了,建议就是把所有的Json放入到一个jsonObject,
最后合并时在进行jsonObject.toJSONString().
以下为测试代码:最终的话被转义的内容是空白!
@ApiOperation(value = "测试发送微信公众模板消息", notes = "测试发送微信公众模板消息")
@GetMapping("/sendMsg")
public ResponseEntity sendMsg() throws ApiException {
com.alibaba.fastjson.JSONObject jsonObject=new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject jsonObjectValue = new com.alibaba.fastjson.JSONObject();
jsonObjectValue.put("value","oldlu");
jsonObjectValue.put("color","#173177");
com.alibaba.fastjson.JSONObject firstValue = new com.alibaba.fastjson.JSONObject();
firstValue.put("value","oldlu");
firstValue.put("color","#173177");
jsonObject.put("first",firstValue.toJSONString());
com.alibaba.fastjson.JSONObject keyword1Value = new com.alibaba.fastjson.JSONObject();
keyword1Value.put("value","oldlu");
keyword1Value.put("color","#173177");
jsonObject.put("keyword1",keyword1Value.toString());
com.alibaba.fastjson.JSONObject keyword2Value = new com.alibaba.fastjson.JSONObject();
keyword2Value.put("value","oldlu");
keyword2Value.put("color","#173177");
jsonObject.put("keyword2",keyword2Value);
com.alibaba.fastjson.JSONObject keyword3Value = new com.alibaba.fastjson.JSONObject();
keyword3Value.put("value","oldlu");
keyword3Value.put("color","#173177");
jsonObject.put("keyword3",keyword3Value);
com.alibaba.fastjson.JSONObject remarkValue = new com.alibaba.fastjson.JSONObject();
remarkValue.put("value","oldlu");
remarkValue.put("color","#173177");
jsonObject.put("remark",remarkValue);
JSONObject jsonResponse= WeChatMpUtil.sendTemplate("oUtXT6DbS3tA_eeMaUVZvN66ezTs", "Eeh7tc4SiJ9W49QSyjTeuCgscRTjBq_DEmf1BNmq_VU", jsonObject, "http://www.baidu.com/");
return ResponseEntity.ok(jsonResponse);
}
toString和toJsonString其实是一样(源码):