-
Notifications
You must be signed in to change notification settings - Fork 3
/
moduleMiddleware.js
72 lines (57 loc) · 2.42 KB
/
moduleMiddleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const modules = require('./main')
const fs = require('fs')
const path = require('path')
const { cookieParse, cookieStringify } = require('./util/index')
const ResultCode = require('./util/ResultCode')
/**
* 外部 cookie 路径
*/
function moduleMiddleware(options = {}) {
const cookiePath = path.resolve(__dirname, 'cookie.txt')
return async (req, res, next) => {
const { method, originalUrl, query } = req
if (method === 'GET' || method === 'get') {
const url = originalUrl.split('?')[0]
const moduleKey = url.substr(1).replace(/\//g, '_')
const modulesDefinition = modules[moduleKey]
if (modulesDefinition) {
query._log = options.log
query._log && console.log('=========== ManYanApi ================')
/// 本地 cookie
/// 加载外面 cookie 文件
let cookie = {}
if (fs.existsSync(cookiePath)) {
query._log && console.log(`加载本地 ${cookiePath}\n`)
cookie = cookieParse(fs.readFileSync(cookiePath).toString().replace(/\n/g, ''))
}
// 处理 城市 id 名字 cookie
if (query.ci && query.ct) {
query._log && console.log('解析城市 id 和城市名称 cookie\n')
Object.assign(cookie, {
ci: encodeURIComponent(query.ci),
ct: encodeURIComponent(query.ct)
})
}
query['cookie'] = cookie
const moduleRequest = await modulesDefinition(query)
query._log && console.log('请求结束\n')
req.dataName = 'maoyen'
req.data = moduleRequest
if (ResultCode.success.code === moduleRequest.code.code && fs.existsSync(cookiePath) && !!moduleRequest.cookie && Object.keys(moduleRequest.cookie).length > 0) {
query._log && console.log('保存返回 cookie\n');
const fsCookie = cookieParse(fs.readFileSync(cookiePath).toString().replace(/\n/g, ''))
Object.assign(fsCookie, moduleRequest.cookie)
fs.writeFileSync(cookiePath, cookieStringify(fsCookie))
}
if (query._log) {
const url = `${req.headers.host}${decodeURIComponent(originalUrl)}`
moduleRequest.code === ResultCode.success.code ?
console.log(`请求成功: ${url}\n`) :
console.log(`请求失败: [status:${moduleRequest.status}]: [code:${moduleRequest.code}]: ${url} : [file]: ${moduleKey}\n`)
}
}
}
next()
}
}
module.exports = moduleMiddleware