设为首页 加入收藏

TOP

使用FastBootWeixin框架快速开发微信公众号(一)
2018-04-08 08:51:19 】 浏览:1702
Tags:使用 FastBootWeixin 框架 快速 开发 公众

本框架基于SpringBoot实现,使用简单快捷,可以快速的完成一个微信公众号

在使用本框架前建议对微信公众号开发文档有所了解,不过在不了解公众号文档的情况下使用本框架也能完成一个简单的微信公众号。

一、简单示例:

1. 申请测试公众号

微信测试公众号申请链接

2. 准备完成,创建maven项目并加入maven依赖

<!-- 支持1.4.0.RELEASE及以上 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

<dependencies>
    <!-- fastbootWeixin的核心依赖 -->
    <dependency>
        <groupId>com.mxixm</groupId>
        <artifactId>fastboot-weixin</artifactId>
        <version>0.1.2.alpha</version>
    </dependency>

    <!-- SpringBoot的web项目,必须 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 暂时只能使用apache的http,后续可加入其它http支持 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
</dependencies>

3. 编写示例代码

在resource目录下新建配置文件application.properties或者其他spring boot支持的配置文件类型,加入配置:

  • wx.token=随机生成的一串字母与数字,推荐使用随机生成32位的UUID
  • wx.appid=测试号的appid,测试号管理界面有
  • wx.appsecret=测试号的appsecret,测试号管理界面有

测试代码:

package com.mxixm.fastboot.weixin;

import com.mxixm.fastboot.weixin.annotation.WxApplication;
import com.mxixm.fastboot.weixin.annotation.WxAsyncMessage;
import com.mxixm.fastboot.weixin.annotation.WxButton;
import com.mxixm.fastboot.weixin.module.web.WxRequest;
import com.mxixm.fastboot.weixin.module.event.WxEvent;
import com.mxixm.fastboot.weixin.module.message.WxMessage;
import com.mxixm.fastboot.weixin.module.user.WxUser;
import com.mxixm.fastboot.weixin.mvc.annotation.WxController;
import com.mxixm.fastboot.weixin.mvc.annotation.WxEventMapping;
import com.mxixm.fastboot.weixin.mvc.annotation.WxMessageMapping;
import org.springframework.boot.SpringApplication;

@WxApplication
@WxController
public class WxApp {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WxApp.class, args);
    }

    /**
     * 定义微信菜单
     */
    @WxButton(group = WxButton.Group.LEFT, main = true, name = "左")
    public void left() {
    }

    /**
     * 定义微信菜单
     */
    @WxButton(group = WxButton.Group.RIGHT, main = true, name = "右")
    public void right() {
    }

    /**
     * 定义微信菜单,并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.LEFT,
            order = WxButton.Order.FIRST,
            name = "文本消息")
    public String leftFirst(WxRequest wxRequest, WxUser wxUser) {
        return "测试文本消息";
    }

    /**
     * 定义微信菜单,并接受事件
     */
    @WxButton(type = WxButton.Type.VIEW,
            group = WxButton.Group.LEFT,
            order = WxButton.Order.SECOND,
            url = "http://baidu.com",
            name = "点击链接")
    @WxAsyncMessage
    public WxMessage link() {
        return WxMessage.News.builder().addItem("测试图文消息", "测试", "https://ss0.bdstatic.com/5aV1bjq
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇基于接口设计与编程 下一篇MySQL ERROR 1050 (42S01): Table..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目