Skip to content

Commit

Permalink
更新到 koa2 并重构代码,已基本可用
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-seth committed May 10, 2017
1 parent ab23e4e commit c9458c6
Show file tree
Hide file tree
Showing 16 changed files with 884 additions and 940 deletions.
8 changes: 6 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ app.use(views(__dirname + '/app/views', {
extension: 'pug'
}))

app.use(bodyParser())
app.use(bodyParser({
onerror: function(err, ctx) {
console.error(err)
}
}))

app.use(async(ctx, next) => {
const start = new Date()

await next()
const ms = new Date() - start
console.log(`${ctx.method} ${decodeURIComponent(ctx.url)} - ${ms}ms`)
// console.log(`${ctx.method} ${decodeURIComponent(ctx.url)} - ${ms}ms`)
})

require('./config/routes')(router)
Expand Down
39 changes: 3 additions & 36 deletions app/api/douban.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
const Promise = require('bluebird')
const Axios = require('axios')
// const Promise = require('bluebird')

module.exports = function(method, url, params, body) {
let options = {
method: method,
url: url,
params: Object.assign({}, params),
data: body
}
// module.exports = function(method, url, params, body) {

return new Promise(function(resolve, reject) {
Axios(options)
.then(Response => {
console.log(response)
// if (response.status === 200) {
// resolve(response.data)
// } else {
// reject(response.statusText.toString())
// }
})
.catch(error => {
console.log(error)
// if (error.response) {
// // The request was made, but the server responded with a status code
// // that falls out of the range of 2xx
// //console.log(error.response.data)
// //console.log(error.response.headers)
// console.log(error.response.status)
// reject(error.response.status.toString())
// } else {
// // Something happened in setting up the request that triggered an Error
// console.log('Error', error.message)
// reject(error.message.toString())
// }
})
})
}
// }
38 changes: 34 additions & 4 deletions app/controllers/wechat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
var wechat = require('../../wechat/wechatListen')
const sha1 = require('sha1')

var config = require('../../config/config')
var handler = require('../../wechat/handlerRequest')
var parseRequest = require('../../wechat/parseRequest')
var handleRequest = require('../../wechat/handleRequest')
var handleResponse = require('../../wechat/handleResponse')

module.exports = async function(ctx, next) {
// this.middle = wechat(config.wechat, handler.handlerRequest)
// await this.middle(next)

const token = config.wechat.token
const signature = ctx.query.signature
const nonce = ctx.query.nonce
const timestamp = ctx.query.timestamp
const echostr = ctx.query.echostr

const str = [token, timestamp, nonce].sort().join('')
const sha = sha1(str)

if (sha !== signature) {
ctx.body = 'Bad Request'
return false
}
ctx.body = ''

// ctx.req is node origin request
const requestData = await parseRequest(ctx.req, ctx.request)
// console.log(requestData)

const responseData = await handleRequest(requestData)
// console.log(responseData)

this.middle = wechat(config.wechat, handler.handlerRequest)
const xml = await handleResponse(requestData, responseData)
console.log(xml)

await this.middle(next)
ctx.status = 200
ctx.type = 'application/xml'
ctx.body = xml
}
2 changes: 1 addition & 1 deletion config/access_token.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"access_token":"gAtVLdvYGg9zK-WRTzMqtXG6Xh2PJcP29T6IPgaWR4mfwBWnYJZqnHZwFAiB2mniMAbf4T000SKuRvlq8wHJk8JQ1RRmn89Zl8Au3xqEO6pQl91rh_n2eiYeF23DnNehSRVhAIABAE","expires_in":1494061671899}
{"access_token":"AbfmpGku3nxLJ5yWMpiy7hU3mAqhnymqg0fDLPxCQl-v5f_41QCHAbKR759d3sEAcX5MeBT0x6TvbhjNTsCafYm6qgY539t8-cE_dhSYKXKek7hDBNXUMnCqaL0jcxJ-ZOQjAHAPEG","expires_in":1494402197856}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"start": "./node_modules/.bin/nodemon app"
},
"dependencies": {
"axios": "^0.16.1",
"bcrypt": "^1.0.2",
"bluebird": "^3.5.0",
"co": "^4.6.0",
Expand All @@ -20,10 +19,12 @@
"koa-router": "^7.1.1",
"koa-static": "^3.0.0",
"koa-views": "^6.0.2",
"koa-xml-body": "^1.1.0",
"mongoose": "^4.9.8",
"pug": "^2.0.0-rc.1",
"raw-body": "^2.2.0",
"request": "^2.81.0",
"request-promise": "^4.2.1",
"sha1": "^1.1.1",
"urlencode": "^1.1.0",
"xml2js": "^0.4.17"
Expand Down
Loading

0 comments on commit c9458c6

Please sign in to comment.