//推送至微信端
//获取token
Admin admin = adminService.getCurrent();
Crop co=cropService.findByAdmin(admin.getId());
StringBuffer str = new StringBuffer();
str.append("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=");
str.append(co.getCropId());
str.append("&corpsecret=");
co.setSecret(co.getSecret());
str.append(co.getSecret());
String token=null;
try{
JSONObject jsontoken = JsonUtils.toJSONObject(HttpUtil.getInvoke(str.toString()));
int errcode=jsontoken.getInt("errcode");
if(errcode!=0){
addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
return "redirect:list_condition.jhtml?id=" + articleCategoryId;
}
token = jsontoken.getString("access_token");
}catch(JSONException e){
addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
return "redirect:list_condition.jhtml?id=" + articleCategoryId;
}
//上传临时素材
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
JSONObject jsonupload=WXUpload.upload(token, "image", basePath + "/resources/admin/images/youlanshan.jpg");
int errcode1=jsonupload.getInt("errcode");
if(errcode1!=0){
addFlashMessage(redirectAttributes, Message.error("上传临时素材失败"));
return "redirect:list_condition.jhtml?id=" + articleCategoryId;
}
String mediaId=jsonupload.getString("media_id");
//推送内容
String msgurl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
+ token;
JSONObject jsonre=new JSONObject();
jsonre.put("touser", "wxs-hsd");
jsonre.put("msgtype", "mpnews");
if(co.getAdmin()==9){
jsonre.put("agentid", 1000003);
}else{
jsonre.put("agentid", 0);
}
JSONObject json11=new JSONObject();
json11.put("title", article.getTitle());
json11.put("author", article.getAuthor());
json11.put("thumb_media_id", mediaId);
json11.put("content",article.getContent());
JSONArray ja=new JSONArray();
ja.add(json11);
JSONObject json1=new JSONObject();
json1.put("articles", ja);
jsonre.put("mpnews", json1);
JSONObject jsonback=JsonUtils.toJSONObject(HttpUtil
.post(msgurl,jsonre.toString()));
System.out.println(jsonback);
addFlashMessage(redirectAttributes, Message.success("推送成功"));
图文消息(mpnews)(企业微信api)
mpnews类型的图文消息,跟普通的图文消息一致,唯一的差异是图文内容存储在企业微信。
多次发送mpnews,会被认为是不同的图文,阅读、点赞的统计会被分开计算。
请求示例:
{
"touser" : "UserID1|UserID2|UserID3",
"toparty" : "PartyID1 | PartyID2",
"totag": "TagID1 | TagID2",
"msgtype" : "mpnews",
"agentid" : 1,
"mpnews" : {
"articles":[
{
"title": "Title",
"thumb_media_id": "MEDIA_ID",
"author": "Author",
"content_source_url": "URL",
"content": "Content",
"digest": "Digest description"
}
]
},
"safe":0
}
参数说明:
参数 | 是否必须 | 说明 |
---|---|---|
touser | 否 | 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送 |
toparty | 否 | 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数 |
totag | 否 | 标签ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数 |
msgtype | 是 | 消息类型,此时固定为:news |
agentid | 是 | 企业应用的id,整型。可在应用的设置页面查看 |
articles | 是 | 图文消息,一个图文消息支持1到8条图文 |
title | 是 | 标题,不超过128个字节,超过会自动截断 |
thumb_media_id | 是 | 图文消息缩略图的media_id, 可以在上传多媒体文件接口中获得。此处thumb_media_id即上传接口返回的media_id |
author | 否 | 图文消息的作者,不超过64个字节 |
content_source_url | 否 | 图文消息点击“阅读原文”之后的页面链接 |
content | 是 | 图文消息的内容,支持html标签,不超过666 K个字节 |
digest | 否 | 图文消息的描述,不超过512个字节,超过会自动截断 |
safe | 否 | 表示是否是保密消息,0表示否,1表示是,默认0 |