JFinal WeiXin 微信极速开发 SDK
本文转载自:http://www.jfinal.com/project/2
最近使用jfinal做微信公众号二次开发,在有些地方很困惑,最后是通过阅读此文找到解决问题的方法,
感觉对自己很有帮助,故转发从文,希望跟我一样在这些方面有困惑的人得到帮助。
详细文档在此:https://gitee.com/jfinal/jfinal-weixin/wikis/Home
1、WeixinConfig配置
详情请见:JFinal weixin中的WeixinConfig配置
2、WeixinMsgController
1 public class WeixinMsgController extends MsgControllerAdapter{ 2 protected void processInTextMsg(InTextMsg inTextMsg) { 3 String msgContent = inTextMsg.getContent().trim(); 4 // 帮助提示 5 if ("help".equalsIgnoreCase(msgContent)) { 6 OutTextMsg outMsg = new OutTextMsg(inTextMsg); 7 outMsg.setContent(helpStr); 8 render(outMsg); 9 } 10 // 图文消息测试 11 else if ("news".equalsIgnoreCase(msgContent)) { 12 OutNewsMsg outMsg = new OutNewsMsg(inTextMsg); 13 outMsg.addNews("图文消息title", "图文消息description", "图文消息片 url", "图文消息 url"); 14 render(outMsg); 15 } 16 // 音乐消息测试 17 else if ("music".equalsIgnoreCase(msgContent)) { 18 OutMusicMsg outMsg = new OutMusicMsg(inTextMsg); 19 outMsg.setTitle("Day By Day"); 20 outMsg.setDescription("建议在 WIFI 环境下流畅欣赏此音乐"); 21 outMsg.setMusicUrl("http://www.jfinal.com/DayByDay-T-ara.mp3"); 22 outMsg.setHqMusicUrl("http://www.jfinal.com/DayByDay-T-ara.mp3"); 23 outMsg.setFuncFlag(true); 24 render(outMsg); 25 } 26 else if ("美女".equalsIgnoreCase(msgContent)) { 27 OutNewsMsg outMsg = new OutNewsMsg(inTextMsg); 28 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"); 29 render(outMsg); 30 } 31 // 其它文本消息直接返回原值 + 帮助提示 32 else { 33 OutTextMsg outMsg = new OutTextMsg(inTextMsg); 34 outMsg.setContent("\t文本消息已成功接收,内容为: " + inTextMsg.getContent() + "\n\n" + helpStr); 35 render(outMsg); 36 } 37 } 38 39 //图片信息 40 protected void processInImageMsg(InImageMsg inImageMsg) { 41 OutImageMsg outMsg = new OutImageMsg(inImageMsg); 42 // 将刚发过来的图片再发回去 43 outMsg.setMediaId(inImageMsg.getMediaId()); 44 render(outMsg); 45 } 46 47 //音频信息 48 protected void processInVoiceMsg(InVoiceMsg inVoiceMsg) { 49 OutVoiceMsg outMsg = new OutVoiceMsg(inVoiceMsg); 50 // 将刚发过来的语音再发回去 51 outMsg.setMediaId(inVoiceMsg.getMediaId()); 52 render(outMsg); 53 } 54 55 //视频信息 56 protected void processInVideoMsg(InVideoMsg inVideoMsg) { 57 /* 腾讯 api 有 bug,无法回复视频消息,暂时回复文本消息代码测试 58 OutVideoMsg outMsg = new OutVideoMsg(inVideoMsg); 59 outMsg.setTitle("OutVideoMsg 发送"); 60 outMsg.setDescription("刚刚发来的视频再发回去"); 61 // 将刚发过来的视频再发回去,经测试证明是腾讯官方的 api 有 bug,待 api bug 却除后再试 62 outMsg.setMediaId(inVideoMsg.getMediaId()); 63 render(outMsg); 64 */ 65 OutTextMsg outMsg = new OutTextMsg(inVideoMsg); 66 outMsg.setContent("\t视频消息已成功接收,该视频的 mediaId 为: " + inVideoMsg.getMediaId()); 67 render(outMsg); 68 } 69 70 //地理位置信息 71 protected void processInLocationMsg(InLocationMsg inLocationMsg) { 72 OutTextMsg outMsg = new OutTextMsg(inLocationMsg); 73 outMsg.setContent("已收到地理位置消息:" + 74 "\nlocation_X = " + inLocationMsg.getLocation_X() + 75 "\nlocation_Y = " + inLocationMsg.getLocation_Y() + 76 "\nscale = " + inLocationMsg.getScale() + 77 "\nlabel = " + inLocationMsg.getLabel()); 78 render(outMsg); 79 } 80 81 //链接信息 82 protected void processInLinkMsg(InLinkMsg inLinkMsg) { 83 OutNewsMsg outMsg = new OutNewsMsg(inLinkMsg); 84 outMsg.addNews("链接消息已成功接收", "链接使用图文消息的方式发回给你,还可以使用文本方式发回。点击图文消息可跳转到链接地址页面,是不是很好玩 :)" , "http://mmbiz.qpic.cn/mmbiz/zz3Q6WSrzq1ibBkhSA1BibMuMxLuHIvUfiaGsK7CC4kIzeh178IYSHbYQ5eg9tVxgEcbegAu22Qhwgl5IhZFWWXUw/0", inLinkMsg.getUrl()); 85 render(outMsg); 86 } 87 88 //关注/取消关注推送信息 89 protected void processInFollowEvent(InFollowEvent inFollowEvent) { 90 OutTextMsg outMsg = new OutTextMsg(inFollowEvent); 91 outMsg.setContent("感谢关注 JFinal Weixin 极速开发,为您节约更多时间,去陪恋人、家人和朋友 :) \n\n\n " + helpStr); 92 // 如果为取消关注事件,将无法接收到传回的信息 93 render(outMsg); 94 } 95 96 //扫描带参数二维码事件 97 protected void processInQrCodeEvent(InQrCodeEvent inQrCodeEvent) { 98 OutTextMsg outMsg = new OutTextMsg(inQrCodeEvent); 99 outMsg.setContent("processInQrCodeEvent() 方法测试成功"); 100 render(outMsg); 101 } 102 103 //上报地理位置事件 104 protected void processInLocationEvent(InLocationEvent inLocationEvent) { 105 OutTextMsg outMsg = new OutTextMsg(inLocationEvent); 106 outMsg.setContent("processInLocationEvent() 方法测试成功"); 107 render(outMsg); 108 } 109 110 //接收到自定义菜单事件 111 protected void processInMenuEvent(InMenuEvent inMenuEvent) { 112 OutTextMsg outMsg = new OutTextMsg(inMenuEvent); 113 outMsg.setContent("processInMenuEvent() 方法测试成功"); 114 render(outMsg); 115 } 116 117 //处理接收到的语音识别结果 118 protected void processInSpeechRecognitionResults(InSpeechRecognitionResults inSpeechRecognitionResults) { 119 OutTextMsg outMsg = new OutTextMsg(inSpeechRecognitionResults); 120 outMsg.setContent("processInSpeechRecognitionResults() 方法测试成功"); 121 render(outMsg); 122 } 123 }
WeixinMsgController 通过继承自MsgControllerAdapter便拥有了接收消息和发送消息的便利方法
3、WeixinApiController
1 public class WeixinApiController extends ApiController { 2 3 /** 4 * 获取公众号菜单 5 */ 6 public void getMenu() { 7 ApiResult apiResult = MenuApi.getMenu(); 8 if (apiResult.isSucceed()) 9 renderText(apiResult.getJson()); 10 else 11 renderText(apiResult.getErrorMsg()); 12 } 13 14 /** 15 * 获取公众号关注用户 16 */ 17 public void getFollowers() { 18 ApiResult apiResult = UserApi.getFollows(); 19 renderText(apiResult.getJson()); 20 } 21 22 /** 23 * 创建菜单 24 */ 25 public void createMenu() { 26 ApiResult apiResult = MenuApi.createMenu(getMenuJson()); 27 if (apiResult.isSucceed()){ 28 System.out.print("创建成功:--"+apiResult.getJson()); 29 renderText(apiResult.getJson()); 30 } 31 else { 32 System.out.print("创建失败:--" + apiResult.getJson()); 33 renderText(apiResult.getErrorMsg()); 34 } 35 } 36 37 /** 38 * 删除菜单 39 */ 40 public void deleteMenu(){ 41 ApiResult apiResult = MenuApi.deleteMenu(); 42 if (apiResult.isSucceed()){ 43 renderText(apiResult.getJson()); 44 } 45 else { 46 renderText(apiResult.getErrorMsg()); 47 } 48 } 49 }
通过调用 MenuApi、UserApi 等 Api 的相关方法即可获取封装成 ApiResult 对象的结果,使用 render 系列方法即可快捷输出结果
4、非Maven用户得到所有依赖 jar 包两种方法
将项目导入eclipse jee中,使用 export 功能导出 war包,其中的 WEB-INF/lib 下面会自动生成 jar 包 让使用 maven 的朋友使用 mvn package 打出 war包,其中的 WEB-INF/lib 下面会自动生成 jar 包 以上两种方法注意要先将pom.xml中的导出类型设置为 war,添加 war 内容进去即可 依赖jackson或fastjson
5、jar包依赖详细说明
详见请见:JFinal weixin 2.1 Jar依赖

浙公网安备 33010602011771号