微信企业代码(对接微信已成为企业绕不开的功能)(1)

前言源码微信开发者配置

微信企业代码(对接微信已成为企业绕不开的功能)(2)

开发者工具

微信企业代码(对接微信已成为企业绕不开的功能)(3)

微信企业代码(对接微信已成为企业绕不开的功能)(4)

消息消息通知

微信企业代码(对接微信已成为企业绕不开的功能)(5)

微信企业代码(对接微信已成为企业绕不开的功能)(6)

微信企业代码(对接微信已成为企业绕不开的功能)(7)

请求方式

url

参数

POST

api.weixin.QQ.com/cgi-bin/mes…

json

{ "touser": "openid", "template_id": "templateId", "url": "url "miniprogram": {}, "data": {} } 复制代码

参数

是否必填

说明

touser

接收者openid

template_id

模板ID

url

模板跳转链接(海外帐号没有跳转能力)

miniprogram

跳小程序所需数据,不需跳小程序可不用传该数据

appid

所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系,暂不支持小游戏)

pagepath

所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar),要求该小程序已发布,暂不支持小游戏

data

模板数据

color

模板内容字体颜色,不填默认为黑色

代码实现

private Integer sendMsgBaseOpenId(String openId, String templateId, List<MsgData> dataList) { if (StringUtils.isEmpty(templateId)) { throw new RuntimeException("未检测到消息模版,无法发送message"); } Map<String, Object> map = new HashMap(); map.put("touser", openId);//你要发送给某个用户的openid 前提是已关注该公众号,该openid是对应该公众号的,不是普通的openid map.put("template_id", templateId); Map<String,Object> dataMap = new HashMap(); if (CollectionUtils.isNotEmpty(dataList)) { for (MsgData msgData : dataList) { dataMap.put(msgData.getName(), msgData); } } map.put("data", dataMap); String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" this.tokenService.accessAndGetDingDingToken(); HttpUtil.post(msgUrl, JSON.toJSONString(map)); return 1; } 复制代码

微信企业代码(对接微信已成为企业绕不开的功能)(8)

客服消息

微信企业代码(对接微信已成为企业绕不开的功能)(9)

请求方式

url

参数

POST

api.weixin.qq.com/cgi-bin/mes…

json

public class Message { //文本消息 protected static final String MESSAGE_TEXT="text"; //图片消息 protected static final String MESSAGE_IMAGE="image"; //语音消息 protected static final String MESSAGE_VOICE="voice"; //音乐 protected static final String MESSAGE_MUSIC="music"; //图文 protected static final String MESSAGE_NEWS="news"; //文件 protected static final String MESSAGE_FILE="file"; //链接消息 protected static final String MESSAGE_LINK="link"; //markdown消息 protected static final String MESSAGE_MARKDOWN="markdown"; //卡片消息 protected static final String MESSAGE_CARD="action_card"; private String type; public Message(String type) { this.type = type; } public Map<String, Object> initMap() { Map<String, Object> map = new HashMap<>(); map.put("msgtype", this.type); return map; } } 复制代码

public class ImageMessage extends Message { private String mediaId; public ImageMessage(String mediaId) { super(MESSAGE_IMAGE); this.mediaId = mediaId; } @Override public String toString() { Map<String, Object> map = initMap(); Map<String, Object> childMap = new HashMap<>(); childMap.put("media_id", this.mediaId); map.put(getType(), childMap); return JSON.toJSONString(map); } } 复制代码

{ "touser":"OPENID", "msgtype":"image", "image": { "media_id":"MEDIA_ID" } } 复制代码

素材上传

微信企业代码(对接微信已成为企业绕不开的功能)(10)

微信企业代码(对接微信已成为企业绕不开的功能)(11)

请求方式

url

参数

POST

api.weixin.qq.com/cgi-bin/med…

json

@Data public class Meterial { //图片 //钉钉 :图片,图片最大1MB。支持上传jpg、gif、png、bmp格式 //微信 :10M,支持PNG\JPEG\JPG\GIF格式 protected static final String METERIAL_IMAGE="image"; //语音 //钉钉 : 语音,语音文件最大2MB。支持上传amr、mp3、wav格式 //微信 : 2M,播放长度不超过60s,支持AMR\MP3格式 protected static final String METERIAL_VOICE="voice"; //视频 //钉钉: 视频,视频最大10MB。支持上传mp4格式。 // 10MB,支持MP4格式 protected static final String METERIAL_VIDEO="video"; //文件 //仅钉钉支持 : 普通文件,最大10MB。支持上传doc、docx、xls、xlsx、ppt、pptx、zip、pdf、rar格式 protected static final String METERIAL_FILE="file"; //缩略图 //仅微信支持 : 64KB,支持JPG格式 protected static final String METERIAL_THUMB="thumb"; private String type; public Meterial(String type) { this.type = type; } } 复制代码

public static String upload(InputStream inputStream , String fileName,String urlLink) throws IOException { String result = StringUtils.EMPTY; URL url=new URL(urlLink); HttpsURLConnection conn=(HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST");//以POST方式提交表单 conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false);//POST方式不能使用缓存 //设置请求头信息 conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); //设置边界 String BOUNDARY="----------" System.currentTimeMillis(); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" BOUNDARY); //请求正文信息 //第一部分 StringBuilder sb=new StringBuilder(); sb.append("--");//必须多两条道 sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"media\"; filename=\"" fileName "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); System.out.println("sb:" sb); //获得输出流 OutputStream out=new DataOutputStream(conn.getOutputStream()); //输出表头 out.write(sb.toString().getBytes("UTF-8")); //文件正文部分 //把文件以流的方式 推送道URL中 DataInputStream din=new DataInputStream(inputStream); int bytes=0; byte[] buffer=new byte[1024]; while((bytes=din.read(buffer))!=-1){ out.write(buffer,0,bytes); } din.close(); //结尾部分 byte[] foot=("\r\n--" BOUNDARY "--\r\n").getBytes("UTF-8");//定义数据最后分割线 out.write(foot); out.flush(); out.close(); if(HttpsURLConnection.HTTP_OK==conn.getResponseCode()){ StringBuffer strbuffer=null; BufferedReader reader=null; try { strbuffer=new StringBuffer(); reader=new BufferedReader(new InputStreamReader(conn.getInputStream())); String lineString=null; while((lineString=reader.readLine())!=null){ strbuffer.append(lineString); } if(StringUtils.isEmpty(result)){ result=strbuffer.toString(); System.out.println("result:" result); } } catch (IOException e) { System.out.println("发送POST请求出现异常!" e); e.printStackTrace(); }finally{ if(reader!=null){ reader.close(); } } } return result; } 复制代码

测试用例

微信企业代码(对接微信已成为企业绕不开的功能)(12)

授权登陆

微信企业代码(对接微信已成为企业绕不开的功能)(13)

微信企业代码(对接微信已成为企业绕不开的功能)(14)

代码测试

来源:https://juejin.cn/post/7124120052114128909

,