4、微信订阅号之自定义菜单和点击事件消息回复
前言
1、controlelr
@RequestMapping("menu")
@ResponseBody
public ResponseBean todayYouHui(){
try {
return ResponseBean.buildSuccess(menuService.byControllerSetMenu());
}catch (AppException e){
return ResponseBean.buildFailure(e.getCode(),e.getMessage());
} catch (Exception e) {
return ResponseBean.buildFailure(e.getMessage());
}
}
2、service
@Override
public String byControllerSetMenu() {
MenuUrl todayYouHuiMenuUrl = menuUrlRepository.findByMenuNameEquals(WechatMenuParams.YOUHUI_TODAY);
ViewButton todayYouHui = new ViewButton();
todayYouHui.setName(todayYouHuiMenuUrl.getMenuName());
todayYouHui.setUrl(todayYouHuiMenuUrl.getUrl());
todayYouHui.setType(WechatMenuParams.VIEW);
// ClickButton _618DaCu = new ClickButton();
// _618DaCu.setName("618大促销");
// _618DaCu.setType(WechatMenuParams.CLICK);
// _618DaCu.setKey("618");
// BasicButton youhuiTodayYouHui = new BasicButton();
// youhuiTodayYouHui.setName(todayYouHuiMenuUrl.getMenuName());
// youhuiTodayYouHui.setSub_button(new BasicButton[]{todayYouHui,_618DaCu});
MenuUrl findYouHuiMenuUrl = menuUrlRepository.findByMenuNameEquals(WechatMenuParams.YOUHIU_FIND);
ViewButton findYouHui = new ViewButton();
findYouHui.setName(findYouHuiMenuUrl.getMenuName());
findYouHui.setUrl(findYouHuiMenuUrl.getUrl());
findYouHui.setType(WechatMenuParams.VIEW);
ClickButton shike = new ClickButton();
shike.setName("应用试客");
shike.setType(WechatMenuParams.CLICK);
shike.setKey("shikeErWeiMa");
MenuUrl xianLaingMiaoShaMenuUrl = menuUrlRepository.findByMenuNameEquals(WechatMenuParams.YOUHUI_XIANGLIANG_MIAOSHA);
ViewButton xianLaingMiaoSha = new ViewButton();
xianLaingMiaoSha.setName(xianLaingMiaoShaMenuUrl.getMenuName());
xianLaingMiaoSha.setUrl(xianLaingMiaoShaMenuUrl.getUrl());
xianLaingMiaoSha.setType(WechatMenuParams.VIEW);
// 二级菜单的配置
BasicButton youhuiXianLaingMiaoSha = new BasicButton();
youhuiXianLaingMiaoSha.setName(WechatMenuParams.YOUHUI_XIANGLIANG_MIAOSHA);
youhuiXianLaingMiaoSha.setSub_button(new BasicButton[]{xianLaingMiaoSha,shike});
/**
* 总菜单分布
*/
Menu menu = new Menu();
menu.setButton(new BasicButton[]{todayYouHui,findYouHui,youhuiXianLaingMiaoSha});
String menuJson = JSONObject.fromObject(menu).toString();
log.info(menuJson);
//此处改为自己想要的结构体,替换即可
String access_token= AccessToakeUtil.getAccessToaken();
String createMenuUrl = WechatMenuParams.CREATE_MENU_URL+access_token;
String body = SdkHttpHelper.doJsonPost(createMenuUrl,menuJson);
log.info("创建自定义菜单 返回结果"+body);
String result = JSONObject.fromObject(body).getString("errmsg");
if(StringUtils.equals(result,"ok")){
log.info("创建自定义菜单成功");
return result;
}else{
log.error("创建创建自定义菜单失败自定义菜单失败");
}
return "创建失败";
}
3、自定义菜单实体类
/**
* @Desc: 自定义菜单总体
* @Author HealerJean
* @Date 2018/5/25 上午9:56.
*/
@Data
public class Menu {
private BasicButton [] button;
}
/**
* @Desc: view 菜单 (url菜单)
* @Author HealerJean
* @Date 2018/5/25 上午9:57.
*/
@Data
public class ViewButton extends BasicButton {
private String url ;
}
/**
* @Desc:
* @Author HealerJean
* @Date 2018/5/25 上午10:11.
*/
@Data
public class ClickButton extends BasicButton{
private String key;
}
@Data
public class BasicButton {
private String name;
private String type ;
/**
* 二级菜单的数组标签 为 sub_button
*/
private BasicButton []sub_button;
}
3、点击事件消息回复
上面的自定义菜单中我们添加了点击事件
ClickButton shike = new ClickButton();
shike.setName("应用试客");
shike.setType(WechatMenuParams.CLICK);
shike.setKey("shikeErWeiMa");
3.1、针对来自用户的时间类型获取消息,然后再获取key的值进行匹配
public Object judgeMessageByMsgType(Map<String, String> map){
// 消息类型
String msgType = map.get("MsgType");
String openId = map.get("FromUserName");
AccessCallPerson(openId); //接入客服
if (WeChatMessageParams.MESSAGE_TEXT.equals(msgType)) {// 文本消息
return getTestMessage(map,true);
// 接收消息推送
}else if(WeChatMessageParams.MESSAGE_EVENT.equals(msgType)) {
//来自点击事件的event值
String event = map.get("Event");
//如果是点击事件,判断key的值
if(StringUtils.equals(WeChatMessageParams.MESSAGE_EVENT_CLICK,event)){//点击事件
if(StringUtils.equals(map.get("EventKey"),"shikeErWeiMa")){
return getImageMessage(map);
}
//用户关注微信公众号事件
}else if(StringUtils.equals(WeChatMessageParams.MESSAGE_EVENT_SUBSCRIBE,event)){
return getSubscribe(map);
//用户取关微信订阅号
}else if(StringUtils.equals(WeChatMessageParams.MESSAGE_EVENT_UNSUBSCRIBE,event)){
unSubScribe(map);
}
else {
return null;
}
}
else { //目前只支持文本消息
return getTestMessage(map,false);
}
return null;
}
如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,添加博主微信哦, 请下方留言吧。可与博主自由讨论哦
支付包 | 微信 | 微信公众号 |
---|---|---|