微信公众号极速 SDK JFinal Weixin

码农软件 · 软件分类 · 微信/微信小程序 · 2019-03-14 19:56:55

软件介绍

JFinal Weixin 是基于 JFinal 的微信公众号极速 SDK,只需参考 Demo 代码即可进行极速开发。自 JFinal Weixin 1.2 版本开始已添加对多公众号支持

0、Maven 坐标

<dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jfinal-weixin</artifactId>
    <version>2.3</version>
</dependency>

1、WeixinConfig

public class WeixinConfig extends JFinalConfig {
  public void configConstant(Constants me) {
    loadPropertyFile("a_little_config.txt");
    me.setDevMode(getPropertyToBoolean("devMode", false));
		
    // ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
    ApiConfigKit.setDevMode(me.getDevMode());
  }
	
  public void configRoute(Routes me) {
    me.add("/msg", WeixinMsgController.class);
    me.add("/api", WeixinApiController.class, "/api");
  }
	
  public void configPlugin(Plugins me) {}
  public void configInterceptor(Interceptors me) {}
  public void configHandler(Handlers me) {}
}

以上通过 configRoute 方法配置了访问路由 "/msg" 与 "/api"。项目启动后,在微信服以务器上配置 url:http://域名/msg

重要:后续新版本的配置方式有所改进,详情见官方文档:https://gitee.com/jfinal/jfinal-weixin/wikis/JFinal-weixin%E4%B8%AD%E7%9A%84WeixinConfig%E9%85%8D%E7%BD%AE

2、WeixinMsgController

public class WeixinMsgController extends MsgController {
  protected void processInTextMsg(InTextMsg inTextMsg) {
   String msgContent = inTextMsg.getContent().trim();
   // 帮助提示
   if ("help".equalsIgnoreCase(msgContent)) {
     OutTextMsg outMsg = new OutTextMsg(inTextMsg);
     outMsg.setContent(helpStr);
     render(outMsg);
   }	
   else if ("美女".equalsIgnoreCase(msgContent)) {
     OutNewsMsg outMsg = new OutNewsMsg(inTextMsg);
     outMsg.addNews("秀色可餐", "JFinal Weixin 极速开发就是这么爽,有木有 ^_^", "http://mmbiz.qpic.cn/mmbiz/zz3Q6WSrzq2GJLC60ECD7rE7n1cvKWRNFvOyib4KGdic3N5APUWf4ia3LLPxJrtyIYRx93aPNkDtib3ADvdaBXmZJg/0", "http://mp.weixin.qq.com/s?__biz=MjM5ODAwOTU3Mg==&mid=200987822&idx=1&sn=7eb2918275fb0fa7b520768854fb7b80#rd");
     render(outMsg);
   }
   // 其它文本消息直接返回原值 + 帮助提示
   else {
     OutTextMsg outMsg = new OutTextMsg(inTextMsg);
     outMsg.setContent("\t文本消息已成功接收,内容为: " + inTextMsg.getContent() + "\n\n" + helpStr);
     render(outMsg);
   }
  }

  protected void processInImageMsg(InImageMsg inImageMsg) {
    OutImageMsg outMsg = new OutImageMsg(inImageMsg);
    // 将刚发过来的图片再发回去
    outMsg.setMediaId(inImageMsg.getMediaId());
    render(outMsg);
  }
  

  /**
   * 如果要支持多公众账号,只需要在此返回各个公众号对应的  ApiConfig 对象即可
   * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
   *
   * 注意:后续新版本改进了配置方式,下面的配置不再需要,详情见下面的官方文档:
   *      https://gitee.com/jfinal/jfinal-weixin/wikis/Home
   */
  public ApiConfig getApiConfig() {
    return new ApiConfig(...);
  }

  // 其她自动接收并解析消息的便利方法省略
}

WeixinMsgController 通过继承自 MsgController 便拥有了接收消息和发送消息的便利方法。

3、WeixinApiController

public class WeixinApiController extends ApiController {
  public void index() {
    render("/api/index.html");
  }
	
  /**
  * 获取公众号菜单
  */
  public void getMenu() {
    ApiResult apiResult = MenuApi.getMenu();
    if (apiResult.isSucceed())
      renderText(apiResult.getJson());
    else
      renderText(apiResult.getErrorMsg());
    }
	
    /**
    * 获取公众号关注用户
    */
    public void getFollowers() {
      ApiResult apiResult = UserApi.getFollows();
      renderText(apiResult.getJson());
    }

  /**
   * 如果要支持多公众账号,只需要在此返回各个公众号对应的  ApiConfig 对象即可
   * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
   * 
   * 注意:后续新版本改进了配置方式,下面的配置不再需要,详情见下面的官方文档:
   *      https://gitee.com/jfinal/jfinal-weixin/wikis/Home
   */
  public ApiConfig getApiConfig() {
    return new ApiConfig(...);
  }
}

通过调用 MenuApi、UserApi 等 Api 的相关方法即可获取封装成 ApiResult 对象的结果,使用 render 系列方法即可快捷输出结果。

4、更多支持

JFinal Weixin 官方文档: https://gitee.com/jfinal/jfinal-weixin/wikis/Home

关注官方微信号马上体验 demo 功能:

本文地址:https://codercto.com/soft/d/1354.html

JSP 2.0技术手册

JSP 2.0技术手册

杜远君、林康司、林上杰 / 湖北教育出版社,电子工业出版社 / 2004-5-1 / 59.0

本书图文并茂,以丰富的实例为引导,全面介绍了主流的Java Web开发技术——JSP 2.0,重点介绍Java在展示层的两项重要技术:Java Servlet与JavaServer Pages。它们是最重要的Java核心技术。对这两项技术的深入了解,将有助于您未来对于JavaServer Faces(JSF)技术以及Java Web Services技术的学习。 本书分为三大部分,前......一起来看看 《JSP 2.0技术手册》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具