nodejs 中koa框架下的微信公眾號開發初始篇


最近在搞微信公眾號開發,后端采用的是nodejs下的koa框架,初識后端的菜鳥,自己搞難度太大了,網上找了很多文章,采用的中間件大都是express框架下的,不過好在爬了許多坑之后總算看見點曙光了,遂把探索出來的一點東西拿出來分享,畢竟能力有限,拿出來也是希望大神看到了給點建議,

主要用 koa-wechat 處理驗證和消息解析 ,用wechat-api來進行按鈕組等api操作,剛開始沒什么內容,大家隨意看看就好,

廢話不多說,直接上代碼(這是一個route的代碼):

 

var koa = require('koa'), co = require('co'), fs = require('fs'), request = require('co-request'); var route = require('koa-route'); var WechatAPI = require('wechat-api'); var wechat = require('koa-wechat'); exports.init = function(app) { app.use(wechat({ token: 'your token' })); app.use(route.post('/api/wechat', postFun)); } //因為服務器多進程,故需要保存token到全局,其他參見wechat-api文檔 var api = new WechatAPI('appid', 'appsecret', function(callback) { // 傳入一個獲取全局token的方法
  fs.readFile('access_token.txt', 'utf8', function(err, txt) { if (err) { return callback(err); } callback(null, JSON.parse(txt)); }); }, function(token, callback) { // 請將token存儲到全局,跨進程、跨機器級別的全局,比如寫到數據庫、redis等
  fs.writeFile('access_token.txt', JSON.stringify(token), callback); }); var weixin = { appid: 'appid', appsecret: 'appsecret', prefix: 'https://api.weixin.qq.com/cgi-bin/', mpPrefix: 'https://mp.weixin.qq.com/cgi-bin/', fileServerPrefix: 'http://file.api.weixin.qq.com/cgi-bin/', payPrefix: 'https://api.weixin.qq.com/pay/', merchantPrefix: 'https://api.weixin.qq.com/merchant/', customservicePrefix: 'https://api.weixin.qq.com/customservice/' } //這里是在最初的時候運行下創建一個'access_token.txt,(因為在設置token為全局后總是不成功,懷疑初始化沒有獲取token)有更好的辦法可以交流
function* getAccessToken() { console.log("getAccessToken start"); var url = weixin.prefix + 'token?grant_type=client_credential&appid=' + weixin.appid + '&secret=' + weixin.appsecret; var response = yield request.get(url); var result = JSON.parse(response.body); console.log('result', result); weixin.token = result.access_token; console.log("token", weixin.token); return weixin.token; } function* postFun(next) { console.log("**************postFun***********"); var info = this.req.body console.log("postFun info", info); console.log("info raw", info.raw); if (info.type === 'text') { if (info.raw.Content == '你好') { this.body = { content: '你好', type: 'text' } } else { this.body = { type: "music", content: { title: "什么都不說了,來段音樂吧", description: "一路上有你", musicUrl: "http://www.xxxxx.com/yilushangyouni.mp3", hqMusicUrl: "http://www/yilushangyouni.mp3", thumbMediaId: "thisThumbMediaId" } } } } else if (info.type === 'event') { if (info.raw.Event === 'subscribe') { //添加關注事件 
      console.log("用戶:" + info.uid + "新添加了關注"); this.body = { content: '你好,歡迎', type: 'text' }; } else { console.log('event::', info.raw); } } else {
//經試驗這個是有問題的,實際是沒法播放的,估計是需要上傳到微信服務器
this.body = { type: "music", content: { title: "什么都不說了,來段音樂吧", description: "一路上有你", musicUrl: "http://sc.111ttt.com/up/mp3/239837/2DF7A5657F60BE1DEF33B8DC3EA42492.mp3", hqMusicUrl: "http://sc.111ttt.com/up/mp3/239837/2DF7A5657F60BE1DEF33B8DC3EA42492.mp3", thumbMediaId: "thisThumbMediaId" } } } console.log("************** postFun end ***********"); } var menu = { "button": [{ "name": "父按鈕", "sub_button": [{ "type": "view", "name": "子按鈕", "url": "http://www." }, { "type": "view", "name": "子按鈕", "url": "http://www." }, { "type": "view", "name": "子按鈕", "url": "http:/" }] }, { "name": "父按鈕", "sub_button": [{ "type": "view", "name": "子按鈕", "url": "http:" }, { "type": "view", "name": "子按鈕", "url": "http:" }] }, { "name": "父按鈕", "sub_button": [{ "type": "view", "name": "關於我", "url": "http:" }, { "type": "view", "name": "遇到我", "url": "http:" }, { "type": "view", "name": "聯系我們", "url": "http:" }] }] } //創建菜單函數,只為了試驗有效性,更多API參考官方文檔 function* appMenu() { console.log('appMenu start'); yield api.createMenu(menu, function(err, result) { if (err) { throw err }; console.log('appMenu', result); }); console.log('appMenu end'); }
//只是為了試驗而執行了一下,自己根據需要執行 co(appMenu());

能力有限,歡迎補充。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM