我是靠谱客的博主 鳗鱼黄豆,这篇文章主要介绍公众号回复消息 以及消息模板,现在分享给大家,希望可以做个参考。

MsgHandler

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import com.clina.matron.component.bulid.ImageBuilder; import com.clina.matron.component.bulid.TextBuilder; import com.clina.matron.component.mp.WxMpConfiguration; import com.clina.matron.model.entity.GzhReplaySetting; import com.clina.matron.model.entity.WxApp; import com.clina.matron.model.entity.WxUser; import com.clina.matron.service.GzhReplaySettingService; import com.clina.matron.service.WxAppService; import com.clina.matron.service.WxUserService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.session.WxSessionManager; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import java.util.Map; import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType; /** * Created by Clint * Date: 2020-08-17 21:43 * Description: */ @Slf4j @Component @AllArgsConstructor public class MsgHandler extends AbstractHandler { private final WxAppService wxAppService; private final WxUserService wxUserService; private final GzhReplaySettingService gzhReplaySettingService; @Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) { //组装回复消息 if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) { WxMpXmlOutMessage rs; //TODO 可以选择将消息保存到本地 WxApp wxApp = wxAppService.findByWeixinSign(wxMessage.getToUser()); WxUser wxUser = wxUserService.getByOpenId(wxApp.getId(), wxMessage.getFromUser()); /** * 根据关键词回复消息(精确) */ if (StringUtils.isNotEmpty(wxMessage.getContent())) { GzhReplaySetting param = new GzhReplaySetting(); param.setGzhId(wxApp.getId()); param.setReplyType("message"); param.setKeyWord(wxMessage.getContent()); GzhReplaySetting replay = gzhReplaySettingService.getOneForQuery(param); if (replay != null && StringUtils.isNotEmpty(replay.getReplyContent())) { // 响应消息 return senfMessageByReplay(replay,wxMessage,wxMpService); } } } return null; } public WxMpXmlOutMessage senfMessageByReplay(GzhReplaySetting replay, WxMpXmlMessage wxMessage, WxMpService weixinService1){ if(replay.getMessageType().equals("text")){ log.info("回复文本消息..."); return new TextBuilder().build(replay.getReplyContent(), wxMessage,weixinService1); }else if(replay.getMessageType().equals("image")){ log.info("回复图片消息..."); return new ImageBuilder().build(replay.getReplyImage(), wxMessage,weixinService1); }else if(replay.getMessageType().equals("news")){ //发送文本消息 log.info("回复图文消息..."); WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item(); item.setDescription(replay.getReplyContent()); item.setPicUrl(replay.getReplyImage()); item.setTitle(replay.getReplyTitle()); item.setUrl(replay.getReplyUrl()); WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS() .fromUser(wxMessage.getToUser()) .toUser(wxMessage.getFromUser()) .addArticle(item) .build(); return m; } return null; } }

AbstractBuilder

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
package com.clina.matron.component.bulid; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class AbstractBuilder { protected final Logger logger = LoggerFactory.getLogger(getClass()); public abstract WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage, WxMpService weixinService); }

SubscribeHandler

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.clina.matron.component.handler; import cn.hutool.json.JSONUtil; import com.clina.matron.common.WeChatConstants; import com.clina.matron.component.bulid.ImageBuilder; import com.clina.matron.component.bulid.TextBuilder; import com.clina.matron.component.mp.WxMpConfiguration; import com.clina.matron.model.entity.GzhReplaySetting; import com.clina.matron.model.entity.WxApp; import com.clina.matron.model.entity.WxUser; import com.clina.matron.service.GzhReplaySettingService; import com.clina.matron.service.WxAppService; import com.clina.matron.service.WxUserService; import com.clina.matron.util.DateTimeUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.session.WxSessionManager; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpTemplateMsgService; import me.chanjar.weixin.mp.bean.message.*; import me.chanjar.weixin.mp.bean.result.WxMpUser; import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.Date; import java.util.Map; /** * Created by Clint * Date: 2020-08-17 21:43 * Description: 用户关注 */ @Slf4j @Component @AllArgsConstructor public class SubscribeHandler extends AbstractHandler { private final WxAppService wxAppService; private final WxUserService wxUserService; private final GzhReplaySettingService gzhReplaySettingService; @Autowired private MsgHandler msgHandler; @Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) { log.info("新关注用户 OPENID: " + wxMessage.getFromUser()); String respMessage = null; // 获取微信用户基本信息 try { WxMpUser userWxInfo = weixinService.getUserService() .userInfo(wxMessage.getFromUser(), null); if (userWxInfo != null) { // TODO 添加关注用户到本地数据库 WxApp wxApp = wxAppService.findByWeixinSign(wxMessage.getToUser()); if (wxApp != null) { WxUser wxUser = wxUserService.getByOpenId(wxApp.getId(), userWxInfo.getOpenId()); if (wxUser == null) {//第一次关注 wxUser = new WxUser(); wxUser.setSubscribeNum(1); assemble(wxApp, wxUser, userWxInfo); wxUserService.save(wxUser); } else {//曾经关注过 wxUser.setSubscribeNum(wxUser.getSubscribeNum() + 1); assemble(wxApp, wxUser, userWxInfo); wxUserService.updateById(wxUser); } /** * 发送关注消息 */ GzhReplaySetting param = new GzhReplaySetting(); param.setGzhId(wxApp.getId()); param.setReplyType("subscribe"); GzhReplaySetting replay = gzhReplaySettingService.getOneForQuery(param); if(replay != null){ // 响应消息 return msgHandler.senfMessageByReplay(replay,wxMessage,weixinService); } return null; } } } catch (Exception e) { log.error("用户关注出错:" + e.getMessage()); } return null; } public static void assemble(WxApp wxApp, WxUser wxUser, WxMpUser userWxInfo) { wxUser.setAppId(wxApp.getId()); wxUser.setSubscribe(WeChatConstants.SUBSCRIBE_TYPE_YES); wxUser.setSubscribeScene(userWxInfo.getSubscribeScene()); wxUser.setSubscribeTime(DateTimeUtils.long2Datetime(userWxInfo.getSubscribeTime() * 1000)); wxUser.setOpenId(userWxInfo.getOpenId()); wxUser.setNickName(userWxInfo.getNickname()); wxUser.setSex(String.valueOf(userWxInfo.getSex())); wxUser.setCity(userWxInfo.getCity()); wxUser.setCountry(userWxInfo.getCountry()); wxUser.setProvince(userWxInfo.getProvince()); wxUser.setLanguage(userWxInfo.getLanguage()); wxUser.setRemark(userWxInfo.getRemark()); wxUser.setHeadimgUrl(userWxInfo.getHeadImgUrl()); wxUser.setUnionId(userWxInfo.getUnionId()); wxUser.setGroupId(JSONUtil.toJsonStr(userWxInfo.getGroupId())); wxUser.setTagidList(userWxInfo.getTagIds()); wxUser.setQrSceneStr(userWxInfo.getQrSceneStr()); wxUser.setUpdateTime(LocalDateTime.now()); } }

ImageBuilder

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.clina.matron.component.bulid; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutImageMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; public class ImageBuilder extends AbstractBuilder { @Override public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage, WxMpService weixinService) { WxMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId(content) .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) .build(); return m; } }

TextBuilder

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.clina.matron.component.bulid; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage; public class TextBuilder extends AbstractBuilder { @Override public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage, WxMpService weixinService) { WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content) .fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) .build(); return m; } }

发送模板消息

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@Override public void sendMsg(String id) { List<WxMpTemplateData> dataList = new ArrayList<>(); WxUserTemplateDto dto = this.detailById(id); WxMpService wxMpService = WxMpConfiguration.getMpService(dto.getWxAppId()); //组装一下参数 if(dto.getParams()!=null && dto.getParams().size()>0) { dto.getParams().forEach(param -> { dataList.add(new WxMpTemplateData(param.getParamName(), param.getParamValue(), param.getParamColor())); }); } //循环发送不同的人 if(StringUtils.isNotEmpty(dto.getSendUser())){ List<Map> openIds = wxUserTemplateMapper.selectOpenIds(dto); if(openIds != null){ openIds.forEach(map->{ WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() //模版id .toUser((String)map.get("openId")) //接受人opendId .templateId(dto.getWxTemplateId()) .build(); templateMessage.setData(dataList); try { wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); } catch (WxErrorException e) { log.error("发送模板消息失败", e); } }); } } } @Override public void sendMsgByTitle(String title) { WxUserTemplate template = wxUserTemplateMapper.selectOneByTitle(title); if(template!= null){ sendMsg(template.getId()); } }

WxMpConfiguration

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.clina.matron.component.mp; import com.clina.matron.common.Constants; import com.clina.matron.common.MatronException; import com.clina.matron.component.handler.*; import com.clina.matron.component.open.WxOpenConfiguration; import com.clina.matron.model.entity.WxApp; import com.clina.matron.service.WxAppService; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.api.WxConsts.EventType; import me.chanjar.weixin.common.api.WxConsts.MenuButtonType; import me.chanjar.weixin.common.api.WxConsts.XmlMsgType; import me.chanjar.weixin.mp.api.WxMpMessageRouter; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.constant.WxMpEventConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.RedisTemplate; /** * Created by Clint * Date: 2020-08-17 21:43 * Description: 公众号Configuration */ @Slf4j @Configuration public class WxMpConfiguration { private static RedisTemplate redisTemplate; private static WxAppService wxAppService; private static LogHandler logHandler; private static NullHandler nullHandler; private static KfSessionHandler kfSessionHandler; private static StoreCheckNotifyHandler storeCheckNotifyHandler; private static LocationHandler locationHandler; private static MenuHandler menuHandler; private static MsgHandler msgHandler; private static UnSubscribeHandler unSubscribeHandler; private static SubscribeHandler subscribeHandler; private static MassMsgHandler massMsgHandler; private static UserGetCardHandler userGetCardHandler; private static UserDelCardHandler userDelCardHandler; private static UserActivateCardHandler userActivateCardHandler; @Autowired public WxMpConfiguration(LogHandler logHandler, NullHandler nullHandler , KfSessionHandler kfSessionHandler, StoreCheckNotifyHandler storeCheckNotifyHandler , LocationHandler locationHandler, MenuHandler menuHandler , MsgHandler msgHandler, UnSubscribeHandler unSubscribeHandler , SubscribeHandler subscribeHandler, MassMsgHandler massMsgHandler , UserGetCardHandler userGetCardHandler, UserDelCardHandler userDelCardHandler , UserActivateCardHandler userActivateCardHandler , RedisTemplate redisTemplate, WxAppService wxAppService) { this.logHandler = logHandler; this.nullHandler = nullHandler; this.kfSessionHandler = kfSessionHandler; this.storeCheckNotifyHandler = storeCheckNotifyHandler; this.locationHandler = locationHandler; this.menuHandler = menuHandler; this.msgHandler = msgHandler; this.unSubscribeHandler = unSubscribeHandler; this.subscribeHandler = subscribeHandler; this.massMsgHandler = massMsgHandler; this.userGetCardHandler = userGetCardHandler; this.userDelCardHandler = userDelCardHandler; this.userActivateCardHandler = userActivateCardHandler; this.redisTemplate = redisTemplate; this.wxAppService = wxAppService; } /** * 获取WxMpService * * @param appId * @return */ public static WxMpService getMpService(String appId) { WxMpService wxMpService = null; WxApp wxApp = wxAppService.findByAppId(appId); if (wxApp != null) { if (wxApp.getIsComponent()) {//第三方授权账号 wxMpService = WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMpServiceByAppid(appId); } else { WxMpInRedisConfigStorage configStorage = new WxMpInRedisConfigStorage(redisTemplate); configStorage.setAppId(wxApp.getId()); configStorage.setSecret(wxApp.getSecret()); configStorage.setToken(wxApp.getToken()); configStorage.setAesKey(wxApp.getAesKey()); wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(configStorage); } } else { throw new MatronException(String.format("微信公众号[%s]未配置", appId)); } return wxMpService; } /** * 获取全局缓存WxMpMessageRouter * * @param appId * @return */ public static WxMpMessageRouter getWxMpMessageRouter(String appId) { WxMpMessageRouter wxMpMessageRouter = newRouter(getMpService(appId)); return wxMpMessageRouter; } private static WxMpMessageRouter newRouter(WxMpService wxMpService) { final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService); // 记录所有事件的日志 (异步执行) newRouter.rule().handler(logHandler).next(); // 接收客服会话管理事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(WxMpEventConstants.CustomerService.KF_CREATE_SESSION) .handler(kfSessionHandler).end(); newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(WxMpEventConstants.CustomerService.KF_CLOSE_SESSION) .handler(kfSessionHandler) .end(); newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(WxMpEventConstants.CustomerService.KF_SWITCH_SESSION) .handler(kfSessionHandler).end(); // 门店审核事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(WxMpEventConstants.POI_CHECK_NOTIFY) .handler(storeCheckNotifyHandler).end(); // 自定义菜单事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(MenuButtonType.CLICK).handler(menuHandler).end(); // 点击菜单连接事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(MenuButtonType.VIEW).handler(menuHandler).end(); // 扫码事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.SCANCODE_WAITMSG).handler(menuHandler).end(); // 关注事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.SUBSCRIBE).handler(subscribeHandler) .end(); // 取消关注事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.UNSUBSCRIBE) .handler(unSubscribeHandler).end(); // 上报地理位置事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.LOCATION).handler(locationHandler) .end(); // 卡券领取事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.CARD_USER_GET_CARD).handler(userGetCardHandler).end(); // 卡券删除事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.CARD_USER_DEL_CARD).handler(userDelCardHandler).end(); // 卡券激活事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.CARD_SUBMIT_MEMBERCARD_USER_INFO).handler(userActivateCardHandler).end(); // 群发回调事件 newRouter.rule().async(false).msgType(XmlMsgType.EVENT) .event(EventType.MASS_SEND_JOB_FINISH).handler(massMsgHandler).end(); // 默认 newRouter.rule().async(false).handler(msgHandler).end(); return newRouter; } }

WxMpInRedisConfigStorage

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.clina.matron.component.mp; import me.chanjar.weixin.common.enums.TicketType; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import org.springframework.data.redis.core.RedisTemplate; import java.util.concurrent.TimeUnit; /** * 基于Redis的微信配置provider. * * @author */ @SuppressWarnings("serial") public class WxMpInRedisConfigStorage extends WxMpDefaultConfigImpl { public static final String ACCESS_TOKEN_KEY = "wx:mp:access_token:"; public final static String JSAPI_TICKET_KEY = "wx:mp:jsapi_ticket:"; public final static String CARDAPI_TICKET_KEY = "wx:mp:cardapi_ticket:"; private final RedisTemplate<String, String> redisTemplate; public WxMpInRedisConfigStorage(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } private String accessTokenKey; private String jsapiTicketKey; private String cardapiTicketKey; /** * 每个公众号生成独有的存储key. */ @Override public void setAppId(String appId) { super.setAppId(appId); this.accessTokenKey = ACCESS_TOKEN_KEY.concat(appId); this.jsapiTicketKey = JSAPI_TICKET_KEY.concat(appId); this.cardapiTicketKey = CARDAPI_TICKET_KEY.concat(appId); } /** * * @param type * @return */ private String getTicketRedisKey(TicketType type) { return String.format("wx:mp:ticket:key:%s:%s", this.appId, type.getCode()); } /** * * @return */ @Override public String getAccessToken() { return redisTemplate.opsForValue().get(this.accessTokenKey); } /** * * @return */ @Override public boolean isAccessTokenExpired() { return redisTemplate.getExpire(accessTokenKey) < 2; } /** * * @param accessToken * @param expiresInSeconds */ @Override public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { redisTemplate.opsForValue().set(this.accessTokenKey, accessToken, expiresInSeconds - 200, TimeUnit.SECONDS); } /** * */ @Override public void expireAccessToken() { redisTemplate.expire(this.accessTokenKey, 0, TimeUnit.SECONDS); } /** * * @param type * @return */ @Override public String getTicket(TicketType type) { return redisTemplate.opsForValue().get(this.getTicketRedisKey(type)); } /** * * @param type * @return */ @Override public boolean isTicketExpired(TicketType type) { return redisTemplate.getExpire(this.getTicketRedisKey(type)) < 2; } /** * * @param type * @param jsapiTicket * @param expiresInSeconds */ @Override public synchronized void updateTicket(TicketType type, String jsapiTicket, int expiresInSeconds) { redisTemplate.opsForValue().set(this.getTicketRedisKey(type), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS); } /** * * @param type */ @Override public void expireTicket(TicketType type) { redisTemplate.expire(this.getTicketRedisKey(type), 0, TimeUnit.SECONDS); } /** * * @return */ @Override public String getJsapiTicket() { return redisTemplate.opsForValue().get(this.jsapiTicketKey); } /** * * @return */ @Override public String getCardApiTicket() { return redisTemplate.opsForValue().get(cardapiTicketKey); } }

上传永久素材

1.controller

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@ApiOperation(value = "上传永久素材图片(GzhReplaySetting.replyImage)") @SysLog("上传永久素材图片") @PostMapping("/materialFileUpload") @PreAuthorize("@ato.hasAuthority('sys:gzhreplay:materialFileUpload')") public WxMpMaterialUploadResult materialFileUpload(@RequestParam String gzhId, @RequestParam MultipartFile file) throws Exception { WxMpService wxMpService = WxMpConfiguration.getMpService(gzhId); WxMpMaterial mpMaterial = new WxMpMaterial(); File newFile =MultipartFileToFile.multipartFileToFile(file); mpMaterial.setFile(newFile); WxMpMaterialUploadResult result = wxMpMaterialService.materialFileUpload("image",mpMaterial,wxMpService); newFile.delete(); return result; }

2.service

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.clina.matron.service; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.material.*; /** * <pre> * Created by Binary Wang on 2016/7/21. * 素材管理的相关接口,包括媒体管理的接口, * 即以https://api.weixin.qq.com/cgi-bin/material * 和 https://api.weixin.qq.com/cgi-bin/media开头的接口 * </pre> * * @author Binary Wang */ public interface WxMpMaterialService1 { /** * <pre> * 新增非图文永久素材 * 通过POST表单来调用接口,表单id为media,包含需要上传的素材内容,有filename、filelength、content-type等信息。请注意:图片素材将进入公众平台官网素材管理模块中的默认分组。 * 新增永久视频素材需特别注意: * 在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息,内容格式为JSON,格式如下: * { "title":VIDEO_TITLE, "introduction":INTRODUCTION } * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">新增永久素材</a> * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE * * 除了3天就会失效的临时素材外,开发者有时需要永久保存一些素材,届时就可以通过本接口新增永久素材。 * 永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用,图片将被屏蔽)。 * 请注意: * 1、新增的永久素材也可以在公众平台官网素材管理模块中看到 * 2、永久素材的数量是有上限的,请谨慎新增。图文消息素材和图片素材的上限为5000,其他类型为1000 * 3、素材的格式大小等要求与公众平台官网一致。具体是,图片大小不超过2M,支持bmp/png/jpeg/jpg/gif格式,语音大小不超过5M,长度不超过60秒,支持mp3/wma/wav/amr格式 * 4、调用该接口需https协议 * </pre> * * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} * @param material 上传的素材, 请看{@link WxMpMaterial} */ WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material, WxMpService wxMpService) throws WxErrorException; /** * <pre> * 新增永久图文素材 * * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">新增永久素材</a> * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN * * 除了3天就会失效的临时素材外,开发者有时需要永久保存一些素材,届时就可以通过本接口新增永久素材。 * 永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用,图片将被屏蔽)。 * 请注意: * 1、新增的永久素材也可以在公众平台官网素材管理模块中看到 * 2、永久素材的数量是有上限的,请谨慎新增。图文消息素材和图片素材的上限为5000,其他类型为1000 * 3、素材的格式大小等要求与公众平台官网一致。具体是,图片大小不超过2M,支持bmp/png/jpeg/jpg/gif格式,语音大小不超过5M,长度不超过60秒,支持mp3/wma/wav/amr格式 * 4、调用该接口需https协议 * </pre> * * @param news 上传的图文消息, 请看{@link WxMpMaterialNews} */ WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news,WxMpService wxMpService) throws WxErrorException; /** * <pre> * 获取视频永久素材的信息和下载地址 * * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">获取永久素材</a> * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN * </pre> * * @param mediaId 永久素材的id */ WxMpMaterialVideoInfoResult materialVideoInfo(String mediaId,WxMpService wxMpService) throws WxErrorException; /** * <pre> * 删除永久素材 * 在新增了永久素材后,开发者可以根据本接口来删除不再需要的永久素材,节省空间。 * 请注意: * 1、请谨慎操作本接口,因为它可以删除公众号在公众平台官网素材管理模块中新建的图文消息、语音、视频等素材(但需要先通过获取素材列表来获知素材的media_id) * 2、临时素材无法通过本接口删除 * 3、调用该接口需https协议 * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738731&token=&lang=zh_CN">删除永久素材</a> * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=ACCESS_TOKEN * </pre> * * @param mediaId 永久素材的id */ boolean materialDelete(String mediaId,WxMpService wxMpService) throws WxErrorException; }

3.serviceImpl

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.clina.matron.service.impl; import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import me.chanjar.weixin.common.error.WxErrorException; import com.clina.matron.service.WxMpMaterialService1; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.material.*; import me.chanjar.weixin.mp.util.requestexecuter.material.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Material.*; /** * Created by Binary Wang on 2016/7/21. * * @author Binary Wang */ @AllArgsConstructor @Service public class WxMpMaterialServiceImpl implements WxMpMaterialService1 { @Override public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material,WxMpService wxMpService) throws WxErrorException { String url = String.format(MATERIAL_ADD_URL.getUrl(wxMpService.getWxMpConfigStorage()), mediaType); return wxMpService.execute(MaterialUploadRequestExecutor.create(wxMpService.getRequestHttp()), url, material); } @Override public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news,WxMpService wxMpService) throws WxErrorException { if (news == null || news.isEmpty()) { throw new IllegalArgumentException("news is empty!"); } String responseContent = wxMpService.post(NEWS_ADD_URL, news.toJson()); return WxMpMaterialUploadResult.fromJson(responseContent); } @Override public WxMpMaterialVideoInfoResult materialVideoInfo(String mediaId,WxMpService wxMpService) throws WxErrorException { return wxMpService.execute(MaterialVideoInfoRequestExecutor.create(wxMpService.getRequestHttp()), MATERIAL_GET_URL, mediaId); } @Override public boolean materialDelete(String mediaId,WxMpService wxMpService) throws WxErrorException { return wxMpService.execute(MaterialDeleteRequestExecutor.create(wxMpService.getRequestHttp()), MATERIAL_DEL_URL, mediaId); } }

PS:matron 月嫂项目

最后

以上就是鳗鱼黄豆最近收集整理的关于公众号回复消息 以及消息模板的全部内容,更多相关公众号回复消息内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部