发布于 2014-10-21 04:31:54 | 201 次阅读 | 评论: 0 | 来源: 网友投递
JFinal 基于Java极速WEB+ORM 框架
JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。在拥有Java语言所有优势的同时再拥有ruby、python、php等动态语言的开发效率!为您节约更多时间
JFinal Weixin 是基于 JFinal 的微信公众号极速 SDK,只需参考 Demo 代码即可进行极速开发。
1、WeixinConfig
public class WeixinConfig extends JFinalConfig {
public void configConstant(Constants me) {
loadPropertyFile("a_little_config.txt");
me.setDevMode(getPropertyToBoolean("devMode", false));
// 配置微信 API 相关常量
ApiConfig.setDevMode(me.getDevMode());
ApiConfig.setUrl(getProperty("url"));
ApiConfig.setToken(getProperty("token"));
ApiConfig.setAppId(getProperty("appId"));
ApiConfig.setAppSecret(getProperty("appSecret"));
}
public void configRoute(Routes me) {
me.add("/weixin", DemoController.class);
me.add("/api", ApiController.class, "/api");
}
public void configPlugin(Plugins me) {}
public void configInterceptor(Interceptors me) {}
public void configHandler(Handlers me) {}
}
通过 ApiConfig 提供 Weixin 公众平台所需的相关配置,并在configRoute 方法配置路由。
2、DemoController
public class DemoController extends WeixinController {
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() + "nn" + helpStr);
render(outMsg);
}
}
protected void processInImageMsg(InImageMsg inImageMsg) {
OutImageMsg outMsg = new OutImageMsg(inImageMsg);
// 将刚发过来的图片再发回去
outMsg.setMediaId(inImageMsg.getMediaId());
render(outMsg);
}
// 其她自动接收并解析消息的便利方法省略
}
DemoController 通过继承自 WeixinController 便拥有了接收消息和发送消息的便利方法。
3、ApiController
public class ApiController extends Controller {
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());
}
}
通过调用 MenuApi、UserApi 等 Api 的相关方法即可获取封装成 ApiResult 对象的结果,使用 render 系列方法即可快捷输出结果。
4、更多支持
JFinal Weixin 官方网站:http://www.jfinal.com
关注官方微信号马上体验 demo 功能:
