Home  >  Article  >  WeChat Applet  >  WeChat development custom menu--weixin-java-tools

WeChat development custom menu--weixin-java-tools

高洛峰
高洛峰Original
2017-03-08 16:46:101947browse

1. Foreword

Usually in the process of developing WeChat, we will definitely design the use of WeChat-related menus. This time I will introduce to you how to use weixin-java-tools to manage the menu

2. Custom menu classification

1. Click: Click push event After the user clicks the click type button, the WeChat server will push the message type event structure to the developer through the message interface (refer to the message interface Guide), and bring the key value filled in by the developer in the button. The developer can interact with the user through the customized key value;

 2. View: Jump URL After the user clicks the view type button, WeChat The client will open the web page URL filled in by the developer in the button, which can be combined with the web page authorization interface to obtain basic user information to obtain basic user information.

 3. scancode_push: After the user clicks the button in the scan code push event, the WeChat client will launch the scan tool. After completing the scan code operation, the scan result will be displayed (if it is a URL, the URL will be entered), and the The result of scanning the code is sent to the developer, and the developer can send a message.

4. scancode_waitmsg: Scan the code to push the event and pop up the "Message Receiving" prompt box. After the user clicks the button, the WeChat client will activate the scan tool. After completing the code scanning operation, the scan code results will be displayed. Pass it to the developer, put away the scanning tool at the same time, and then the "Message Receiving" prompt box will pop up, and you may receive a message from the developer.

 5. pic_sysphoto: After the user clicks the button to take pictures and send pictures from the system, the WeChat client will call up the system camera. After completing the picture taking operation, the taken pictures will be sent to the developer and the event will be pushed to the developer. , and put away the system camera at the same time, and then you may receive a message from the developer.

 6. pic_photo_or_album: After the user clicks the button to take a photo or send a picture to an album, the WeChat client will pop up a selector for the user to choose "take a photo" or "select from the mobile phone album". After the user chooses, he will go through the other two processes.

7. pic_weixin: After the user clicks the button of the pop-up WeChat photo album sender, the WeChat client will call up the WeChat photo album. After completing the selection operation, the selected photos will be sent to the developer's server and the event will be pushed to Developer, close the photo album at the same time, and you may receive a message from the developer later.

8. Location_select: After the user clicks the button of the pop-up geographical location selector, the WeChat client will call up the geographical location selection tool. After completing the selection operation, the selected geographical location will be sent to the developer's server and collected at the same time. Start the location selection tool, and then you may receive a message from the developer.

9. media_id: Send messages (except text messages). After the user clicks the media_id type button, the WeChat server will send the material corresponding to the permanent material ID filled in by the developer to the user. The permanent material type can be a picture. , audio, video, graphic messages. Please note: the permanent material ID must be a legal ID obtained after uploading through the "Material Management/Add Permanent Material" interface.

 10. view_limited: Jump to the graphic message URL. After the user clicks the view_limited type button, the WeChat client will open the graphic message URL corresponding to the permanent material ID filled in by the developer in the button. The permanent material type only supports Graphic messages. Please note: the permanent material ID must be a legal ID obtained after uploading through the "Material Management/Add Permanent Material" interface.

 Please note that all events from 3 to 8 only support WeChat iPhone 5.4.1 or above, and WeChat users with Android 5.4 or above. WeChat users with older versions will not respond after clicking. , developers cannot receive event push normally. 9 and 10 are event types specially prepared for subscription accounts of third-party platforms that have not been certified by WeChat (specifically, those who have failed to pass the qualification certification). They do not have event push and their capabilities are relatively limited. Other types of public accounts No need to use.

3. Calling interface address: http Request method: POST (please use https protocol) https://api.weixin.qq.com/ cgi-bin/menu/create?access_token=ACCESS_TOKEN

4. Request example

{
     "button":[
     {	
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "name":"菜单",
           "sub_button":[
           {	
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }

5. Parameter description

参数 是否必须 说明
button 一级菜单数组,个数应为1~3个
sub_button 二级菜单数组,个数应为1~5个
type 菜单的响应动作类型
name 菜单标题,不超过16个字节,子菜单不超过40个字节
key click等点击类型必须 菜单KEY值,用于消息接口推送,不超过128字节
url view类型必须 网页链接,用户点击菜单可打开链接,不超过1024字节
media_id media_id类型和view_limited类型必须 调用新增永久素材接口返回的合法media_id

 

 

 

 

 

 

 

 

 

 

 

 

六、代码实现:

  

package com.weixin.menu;

import java.util.ArrayList;
import java.util.List;

import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;

public class WeixinMenuService {

	public static void main(String[] args) {
		
		//创建菜单
		//创建一级菜单
		WxMenuButton button1=new WxMenuButton();
		button1.setType("click"); //点击事件按钮
		button1.setName("点击菜单");
		button1.setKey("key1"); //根据标志获取点击菜单
		
		//创建一个复合菜单
		WxMenuButton button2=new WxMenuButton();
		button2.setName("多级菜单");
		
		WxMenuButton button2_1=new WxMenuButton();
		button2_1.setType("click"); //点击事件按钮
		button2_1.setName("子菜单一");
		button2_1.setKey("key2"); //根据标志获取点击菜单
		
		WxMenuButton button2_2=new WxMenuButton();
		button2_2.setType("click"); //点击事件按钮
		button2_2.setName("子菜单二");
		button2_2.setKey("key3"); //根据标志获取点击菜单
		
		
		WxMenuButton button3=new WxMenuButton();
		button3.setName("url菜单");
		button3.setType("view");
		button3.setUrl("http://www.baidu.com");  //必须添加http
		
		
		List<WxMenuButton> subButtons=new ArrayList<WxMenuButton>();
		subButtons.add(button2_1);
		subButtons.add(button2_2);
		button2.setSubButtons(subButtons);
		
		List<WxMenuButton> buttons=new ArrayList<WxMenuButton>();
		buttons.add(button1);
		buttons.add(button2);
		buttons.add(button3);
		
		WxMenu menu=new WxMenu();
		menu.setButtons(buttons);
		
		
		//发送请求 创建菜单
		WxMpService service=new WxMpServiceImpl();	
		WxMpInMemoryConfigStorage wxConfigProvider=new WxMpInMemoryConfigStorage();
		wxConfigProvider.setAppId("wx60a8f1c3a95b0b9c");
		wxConfigProvider.setSecret("5b0e8613b538da5ac4bbc610998f10ba");
		service.setWxMpConfigStorage(wxConfigProvider);
		try {
			service.menuCreate(menu);
		} catch (WxErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}

七、点击菜单事件推送

  7.1 消息格式

  

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[CLICK]]></Event>
<EventKey><![CDATA[EVENTKEY]]></EventKey>
</xml>

  

  7.2 参数说明

  

参数 描述
ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType 消息类型,event
Event 事件类型,CLICK
EventKey 事件KEY值,与自定义菜单接口中KEY值对应

  

 

 

 

 

 

 

  7.3 消息处理

  在接收消息的post中判断消息类型 并做相关处理。

//获取消息流
		WxMpXmlMessage message=WxMpXmlMessage.fromXml(request.getInputStream());
		
		if(message.getMsgType().equals("event")){ //事件
			
			//判断event
			if(message.getEvent().equals("click") && message.getEventKey().equals("key1")){
				//do something
				
			}
			
			
		}

  

The above is the detailed content of WeChat development custom menu--weixin-java-tools. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn