您的位置:首页 > 移动开发 > 微信开发

Java微信公众平台开发(九)--微信自定义菜单的创建实现

2019-05-28 19:37 1826 查看

  自定义菜单这个功能在我们普通的编辑模式下是可以直接在后台编辑的,但是一旦我们进入开发模式之后我们的自定义菜单就需要自己用代码实现,所以对于刚开始接触的人来说可能存在一定的疑惑,这里我说下平时我们在开发模式下常用的两种自定义菜单的实现方式:①不用写实现代码,直接用网页测试工具Post json字符串生成菜单;②就是在我们的开发中用代码实现菜单生成!(参考文档:http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html  )在自定义菜单中菜单的类型分为两种,一种为view的视图菜单,点击之后直接跳转到url页面;还有一种是click类型的点击型,后端通过点击事件类型给与不同的相应;后面新增了各种特色功能的菜单其本质都还是Click类型的菜单,所以生成的规则都是一样的,其生成菜单的方式都是向微信服务器post json字符串生成菜单,下面讲述菜单生成的方法和规则!(参考文档:http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html )在自定义菜单中菜单的类型分为两种,一种为view的视图菜单,点击之后直接跳转到url页面;还有一种是click类型的点击型,后端通过点击事件类型给与不同的相应;后面新增了各种特色功能的菜单其本质都还是Click类型的菜单,所以生成的规则都是一样的,其生成菜单的方式都是向微信服务器post json字符串生成菜单,下面讲述菜单生成的方法和规则!

(一)使用网页调试工具生成菜单

我们通过连接(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95&form=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95%E5%88%9B%E5%BB%BA%E6%8E%A5%E5%8F%A3%20/menu/create  )进入到网页调试工具,如下图:

在这里我们生成菜单的时候只需要我们账号的有效token和json字符串即可,这里的json字符串可以参照文档中的案例做出修改得到,我这里给出的一个案例如下:

{
"button": [
{
"name": "博客",
"type": "view",
"url": "https://www.cnblogs.com/gede"
},
{
"name": "菜单",
"sub_button": [
{
"key": "text",
"name": "回复图文",
"type": "click"
},
{
"name": "博客",
"type": "view",
"url": "https://www.cnblogs.com/gede"
}
]
},
{
"key": "text",
"name": "回复图文",
"type": "click"
}
]
}

我们填入响应的token,点击检查问题如果返回结果Ok就可以了。到这里我们采用web测试工具生成菜单的方式就完成了,下面接着介绍用代码生成菜单!

(二)采用代码实现菜单的生成

前面我们有说道在菜单中有view和click两种类型的事件,这里我们首先在代码中建立两种类型对应的java实体,view类型建立实体ViewButton.java如下:

package com.gede.wechat.menu;
/**
* @author gede
* @version date:2019年5月28日 下午7:02:43
* @description :
*/
public class ViewButton {
private String type;
private String name;
private String url;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}

同样的建立click的实体ClickButton.java如下:

package com.gede.wechat.menu;
/**
* @author gede
* @version date:2019年5月28日 下午7:03:02
* @description :
*/
public class ClickButton {
private String type;
private String name;
private String key;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

}

这里创建两个实体也是为了方便我们在自定义菜单中对json的封装,在这里我用代码的形式封装了上面给出的同样的json格式,并调用生成自定义菜单的接口发送到微信服务器,简单代码如下:

package com.gede.wechat.menu;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;

import com.gede.web.util.GlobalConstants;
import com.gede.wechat.util.HttpUtils;

/**
* @author gede
* @version date:2019年5月28日 下午7:03:24
* @description :
*/
@Component
public class MenuMain {

public void createMenu(){
ClickButton cbt=new ClickButton();
cbt.setKey("image");
cbt.setName("回复图片");
cbt.setType("click");

ViewButton vbt=new ViewButton();
vbt.setUrl("https://www.cnblogs.com/gede");
vbt.setName("博客");
vbt.setType("view");

JSONArray sub_button=new JSONArray();
sub_button.add(cbt);
sub_button.add(vbt);

JSONObject buttonOne=new JSONObject();
buttonOne.put("name", "菜单");
buttonOne.put("sub_button", sub_button);

JSONArray button=new JSONArray();
button.add(vbt);
button.add(buttonOne);
button.add(cbt);

JSONObject menujson=new JSONObject();
menujson.put("button", button);
System.out.println(menujson);

//这里为请求接口的url   +号后面的是token,这里就不做过多对token获取的方法解释
String url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+GlobalConstants.getInterfaceUrl("access_token");

try{
String rs=HttpUtils.sendPostBuffer(url, menujson.toJSONString());
System.out.println(rs);
}catch(Exception e){
System.out.println("请求错误!");
}
}
}

这里面加了@Component组件,可以被扫为spring 的一个bean自动装配。我们在写一个简单的jsp实现响应这里的createMenue方法。

添加了Component组件扫描后,我们在applicationContext.xml中添加组件扫描包 :<context:component-scan base-package="com.gede.wechat.menu"></context:component-scan>

如果配置文件提示报错,我们添加content上下文:点开NameSpaces 如下图:

写到这里,我们就差控制器来调用我们的createMenue方法了。我们创建MenueController类,简单代码如下:

package com.gede.wechat.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gede.wechat.menu.MenuMain;
import com.gede.wechat.util.SignUtil;

/**
* @author gede
* @version date:2019年5月28日 下午7:20:08
* @description :
*/
@Controller
@RequestMapping("/menue")
public class MenueController {

private MenuMain menue;
@Autowired
public MenueController(MenuMain menue) {
super();
this.menue = menue;
}

@RequestMapping(value="/",method=GET)
public String home(){
menue.createMenu();
}

}

现在运行我们的项目,项目跑起来后,在浏览器地址栏输入:.自己的服务器地址./mychat/menue/create/ 。返回控制台,查看,效果如下:

再来看微信端的效果:

 

关于文中涉及到的Spring相关知识,大家可以去看我关于Spring的博客。有不懂的也可以留言。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: