微信公眾號極速 SDK - JFinal Weixin

jopen 11年前發布 | 66K 次閱讀 JFinal

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) {} }</pre>

通過 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 極速開發就是這么爽,有木有 ^_^", "

  protected void processInImageMsg(InImageMsg inImageMsg) {     OutImageMsg outMsg = new OutImageMsg(inImageMsg);     // 將剛發過來的圖片再發回去     outMsg.setMediaId(inImageMsg.getMediaId());     render(outMsg);   }      // 其她自動接收并解析消息的便利方法省略 }</pre>

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());     } }</pre>

通過調用 MenuApi、UserApi 等 Api 的相關方法即可獲取封裝成 ApiResult 對象的結果,使用 render 系列方法即可快捷輸出結果。

4、更多支持

JFinal Weixin 官方網站:http://www.jfinal.com 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!