jeesite Fastweixin 开发公众号基础

首先在pom.xml中引入fastweixin包

		<dependency>
			<groupId>com.github.sd4324530</groupId>
			<artifactId>fastweixin</artifactId>
			<version>1.3.15</version>
		</dependency>

然后创建这么一个类.注意替换成自己的appid、TOKEN 、APP_SECRET 

package com.thinkgem.jeesite.test.web;

import com.github.sd4324530.fastweixin.api.MenuAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.entity.Menu;
import com.github.sd4324530.fastweixin.api.entity.MenuButton;
import com.github.sd4324530.fastweixin.api.enums.MenuType;
import com.github.sd4324530.fastweixin.api.enums.ResultType;
import com.github.sd4324530.fastweixin.message.Article;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.NewsMsg;
import com.github.sd4324530.fastweixin.message.TextMsg;
import com.github.sd4324530.fastweixin.message.req.BaseEvent;
import com.github.sd4324530.fastweixin.message.req.MenuEvent;
import com.github.sd4324530.fastweixin.message.req.TextReqMsg;
import com.github.sd4324530.fastweixin.servlet.WeixinControllerSupport;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@Controller
@RequestMapping(value = "/weixin")
public class TestWeiXinController extends WeixinControllerSupport {
    private static final String TOKEN = "TOKEN ";
    private String APP_ID = "appId";
    //    private static final org.slf4j.Logger log = LoggerFactory.getLogger(TestWeiXinController.class);

    private String APP_SECRET = "APP_SECRET ";

    @Override
    protected String getToken() {
        return TOKEN;
    }
    // 获取 access token: http://localhost:8080/weixin/access-token
    @RequestMapping(value = "access-token")
    @ResponseBody
    public String getAccessToken() {
        System.out.println("access-token");
        ApiConfig config = new ApiConfig(APP_ID, APP_SECRET);
        return config.getAccessToken();
    }
    //使用安全模式时设置:APPID
    //不再强制重写,有加密需要时自行重写该方法
    @Override
    protected String getAppId() {
        return null;
    }
//    //使用安全模式时设置:密钥
//    //不再强制重写,有加密需要时自行重写该方法
//    @Override
//    protected String getAESKey() {
//        return null;
//    }
    //重写父类方法,处理对应的微信消息
    @Override
    protected BaseMsg handleTextMsg(TextReqMsg msg) {
        String content = msg.getContent(); // content 是用户输入的信息
        if(content.toUpperCase().equals("URL")){
            // 消息中有链接
            return new TextMsg("你好: <a href=\"http://www.baidu.com\">百度</a>");
        } else  if(content.toUpperCase().equals("美景")){
            // 图文消息
            String picUrl = "http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E7%89%87&step_word=&hs=0&pn=3&spn=0&di=136056393550&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=2260926939%2C1550208231&os=2086677986%2C2932337668&simid=0%2C0&adpicid=0&lpn=0&ln=1871&fr=&fmq=1537519877737_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01690955496f930000019ae92f3a4e.jpg%402o.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bzv55s_z%26e3Bv54_z%26e3BvgAzdH3Fo56hAzdH3FZNTAaMzMy_z%26e3Bip4s%3FfotpviPw2j%3D5g&gsm=0&rpstart=0&rpnum=0&islist=&querylist="; // 消息中显示的图片
            String url = "http://baidu.com"; // 点击消息后跳转的网页的地址
            String description = "世界上最漂亮的风景";
            return new NewsMsg(Arrays.asList(new Article("美景", description, picUrl, url), new Article("美景1", description, picUrl, url)));

        } else {
            return new TextMsg("不识别的命令, 您输入的内容是: " + content);
        }
    }
//    /*1.1版本新增,重写父类方法,加入自定义微信消息处理器
//     *不是必须的,上面的方法是统一处理所有的文本消息,如果业务觉复杂,上面的会显得比较乱
//     *这个机制就是为了应对这种情况,每个MessageHandle就是一个业务,只处理指定的那部分消息
//     */
//    @Override
//    protected List<MessageHandle> initMessageHandles() {
//        List<MessageHandle> handles = new ArrayList<MessageHandle>();
        handles.add(new MyMessageHandle());
//        return handles;
//    }
//    //1.1版本新增,重写父类方法,加入自定义微信事件处理器,同上
//    @Override
//    protected List<EventHandle> initEventHandles() {
//        List<EventHandle> handles = new ArrayList<EventHandle>();
        handles.add(new MyEventHandle());
//        return handles;
//    }

    @Override
    protected BaseMsg handleSubscribe(BaseEvent event) {
        return super.handleSubscribe(event);
    }

    @Override
    protected BaseMsg handleMenuClickEvent(MenuEvent event) {
        String key = event.getEventKey();
        if("MAIN1".equals(key.toUpperCase())){
            return new TextMsg("点击按钮");
        } else if("SUB1".equals(key.toUpperCase())){
            return new TextMsg("语文");
        } else if("SUB2".equals(key.toUpperCase())){
            return new TextMsg("数学");
        } else if("SUB3".equals(key.toUpperCase())){
            return new TextMsg("外语");
        } else{
            return new TextMsg("不识别的菜单命令");
        }
    }
    @RequestMapping(value = "create-menu")
    @ResponseBody
    public String createMenu() {
        MenuButton main1 = new MenuButton();
        main1.setType(MenuType.CLICK); // 可点击的菜单
        main1.setKey("main1");
        main1.setName("主菜单一");

        MenuButton main2 = new MenuButton();
        main2.setType(MenuType.VIEW); // 链接的菜单,点击后跳转到对应的 URL
        main2.setName("资产监管");
        main2.setUrl("http://www.baidu.com");

        MenuButton main3 = new MenuButton();
        main3.setType(MenuType.CLICK);
        main3.setName("真题");

        MenuButton sub1 = new MenuButton();
        sub1.setType(MenuType.CLICK); // 带有子菜单
        sub1.setName("语文");
        sub1.setKey("sub1");

        MenuButton sub2 = new MenuButton();
        sub2.setType(MenuType.CLICK);
        sub2.setName("数学");
        sub2.setKey("sub2");

        MenuButton sub3 = new MenuButton();
        sub3.setType(MenuType.CLICK);
        sub3.setName("外语");
        sub3.setKey("sub3");
        main3.setSubButton(Arrays.asList(sub1, sub2,sub3));

        Menu menu = new Menu();
        menu.setButton(Arrays.asList(main1, main2, main3));

        //创建菜单
        ApiConfig config = new ApiConfig(APP_ID, APP_SECRET);
        MenuAPI menuAPI = new MenuAPI(config);
        ResultType resultType = menuAPI.createMenu(menu);
        return resultType.toString();
    }
}

 

创建微信端的按钮菜单   http://whq1987cs.free.idcfengye.com/yz_assets/weixin/create-menu    如果关注后没有菜单则取消关注后在关注一遍,菜单就会刷新到最近了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值