06-Menu configuration of WeChat official account

06-Menu configuration of WeChat official account

table of Contents

1.1. Abstract message information

1.2. Menu configuration


 

1. WeChat Official Account Menu

 

First, before configuring the menu of the WeChat official account, you need to understand how the WeChat platform defines the api of the menu.

The detailed information reference url is as follows:

 

https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html

The WeChat request to be called is as follows:

 

https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

Among them, the parameter access_token has been obtained in Section 05, and we will use the access_token without timeout to make the request call

 

Click and view request examples (for other types, please refer to official documents)

{
    "button": [
        {
            "key": "1",
            "name": "一级菜单1",
            "type": "click"
        },
        {
            "name": "一级菜单2",
            "type": "view",
            "url": "http://www.baidu.com"
        },
        {
            "name": "菜单",
            "sub_button": [
                {
                    "name": "二级菜单",
                    "type": "view",
                    "url": "http://fanyi.youdao.com/"
                }
            ]
        }
    ]
}

The request for creating a catalog can be divided into 2 parts

  1. Abstract message information (objectification of message information)
  2. Send a request to modify the directory structure of the official account

1.1. Abstract message information

Before abstracting the message information, we need to analyze the message

First of all, whether the type is click or view, there is a name, so we can extract the public.

//这里讲下我为什么把这个类设置成抽象类
//当节点1(一级菜单1)和节点2(一级菜单2)是两种不同的类型。
//但是呢button中需要有两种不同的类型,所以就做了抽象类进行处理
public abstract class AbstractMenu {
    // 抽取出来的公共参数
    public String name;
    // get set方法省略
}

 

Then we can abstract one by one according to the situation from the inside to the outside.

 

  • First, we first create the bottom-level basic objects, which are view and click objects (if different types are set, please refer to the detailed documentation of the WeChat official account)

 

// 由于是测试 所以方法的命名是不符合规范的
public class ClickButten extends AbstractMenu {

    private String type= "click";

    private String key;
    
    // 省略get set方法
    
    public ClickButten(String name,String key) {
        super(name);
        this.key = key;
    }
}
public class ViewButten extends AbstractMenu {

    private String type = "view";

    private String url;
    
    // 省略get set方法
    
    public ViewButten(String name,String url) {
        super(name);
        this.url = url;
    }
}
  • Then we are creating the submenu object
public class SubButten extends AbstractMenu {

    List<AbstractMenu> sub_button = new ArrayList<AbstractMenu>();

    // 省略 get set方法
    
    public SubButten(String name) {
        super(name);
    }
}
  • After the submenu is created, the largest menu is created

 

public class Button {

    List<AbstractMenu> button = new ArrayList<AbstractMenu>();
    // 省略 get set方法
}
  • The final verification process
@Test
void TestMenu(){
    Button button = new Button();
    button.getButton().add(new ClickButten("一级菜单1","1"));
    button.getButton().add(new ViewButten("一级菜单2","http://www.baidu.com"));
    SubButten  subButten = new SubButten("菜单");
    subButten.getSub_button().add(new ViewButten("二级菜单","http://fanyi.youdao.com/"));
    button.getButton().add(subButten);
    //这块被坑了 使用的api是 net.sf.json.JSONObject  我一直以为使用的是org.json(原生的json包)
    JSONObject jsonObject = JSONObject.fromObject(button);
    System.out.println(jsonObject.toString());
}

Use postman to call the modification, and the effect diagram is shown below.

 

1.2. Menu configuration

Able to assemble the request message, and then call to send an intercession.

void  Testpost() throws Exception {
        // 菜单的url进行拼接
        Button button = new Button();
        button.getButton().add(new ClickButten("一级菜单1","1"));
        button.getButton().add(new ViewButten("一级菜单2","http://www.baidu.com"));
        SubButten  subButten = new SubButten("菜单");
        subButten.getSub_button().add(new ViewButten("二级菜单","http://fanyi.youdao.com/"));
        button.getButton().add(subButten);
        //这块被坑了 使用的api是 net.sf.json.JSONObject  我一直以为使用的是org.json(原生的json包)
        JSONObject jsonObject = JSONObject.fromObject(button);

        String ACCESS_TOKEN ="42_lh4kVHJDHUTQtHfUxrjGqGfKTJKcpqO7jxGmhIwdncmOb9NH8YJ_GhNTDDxzMb1EBGqm
        SckUqs7mL6ozYr-f1JNnc3J7PmaGbnbXDl0BL6oBf9OMyGY3yensACdpnoF1zoMf2QxZArQUbVXOKYPbAEARFW";

        URL url = new URL(MENU_URL.replace("ACCESS_TOKEN",ACCESS_TOKEN));
        URLConnection urlConnection = url.openConnection();
       //设置为可以发送数据的状态
        urlConnection.setDoOutput(true);
        //获取输出流
        OutputStream outputStream = urlConnection.getOutputStream();
        // 这个地方要特别的注意,在发送给微信中含有中文的时候编码需要设置成utf-8否则会报错
        outputStream.write(jsonObject.toString().getBytes("utf-8"));
        outputStream.close();
        InputStream is = urlConnection.getInputStream();
        byte [] b = new  byte[1024];
        int len;
        StringBuilder sb = new StringBuilder();
        while((len=is.read(b))!= -1){
            sb.append(new String(b,0,len));
        }
        System.out.println(sb.toString());
    }

 

Note: After the menu is configured, every time the user clicks on the menu, a message will be sent to the background server.

The following figure shows the message sent to the background when I clicked the button. We need to process it according to the returned information MsgType and EventKey.

{CreateTime=1614671191, EventKey=1, Event=CLICK, ToUserName=gh_7c94dc7e2130, FromUserName=orxnH5wNRLvz2DfNTopTGcL4y9ic, MsgType=event}

Note: The information sent is parsed by me, the actual data sent is in xml format

Guess you like

Origin blog.csdn.net/baidu_31572291/article/details/114337924