-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (124 loc) · 3.84 KB
/
app.js
File metadata and controls
138 lines (124 loc) · 3.84 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var LRU = require("lru-cache")
var YZClient = require('yz-open-sdk-nodejs');
var Token = require('yz-open-sdk-nodejs/Token');
var getToken = require('./utils/index.js').getToken;
// 简单的设置下token缓存,可以存到其他存储中
var cache = new LRU({
maxAge: 604800000
});
var YZ_Client, hasError = false;
var YZToken = '';
const client_id = '';
const client_secret = '';
const kdt_id = '';
// 获取token并初始化有赞请求客服端
getYZToken();
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// 设置接口操作白名单,只有在此申明的接口才能进行操作
// 只支持查,不支持改
// 格式 /^youzan.(在这里增加类型).*\.(增加操作)$/
var API_LIST = {
// 不要写g,原因见:http://www.365mini.com/page/javascript-regexp-test.htm
whiteList: /^youzan\.(item|trades|shop|itemcategories|retail|logistics|regions?|sold|pay|ump|salesman|crm|users?|scrm|ebiz).*\.(get|search|query|getbycode|count|all|list)$/i
}
app.use('/api/:url', function(req, res){
if(!cache.get('YZToken')){
getYZToken();
return res.send({
code: 40002,
desc: hasError ? hasError : '正在初始化有赞接口'
})
}
if(!API_LIST.whiteList.test(req.params.url)){
return res.send({
code: 40000,
desc: '没有操作权限!'
})
}
YZ_Client.invoke(req.params.url, '4.0.0', req.method, req.method === 'GET' ? req.query : req.body, undefined).then(function(resp){
let dataBody = {
code: '00001',
data: null,
desc: 'SUCCESS'
}
let body = resp.body;
try{
body = JSON.parse(body)
}catch(e){
dataBody.code = 40000;
dataBody.desc = e.msg;
return res.send(dataBody);
}
let { error_response, response} = body;
console.log(typeof resp.body)
if(error_response){
dataBody.code = error_response.code;
dataBody.desc = error_response.msg;
if(error_response.code === 40001){
getYZToken();
}
}else{
dataBody.data = response;
}
res.send(dataBody);
// console.log(response.body);
}).catch(function(err){
let dataBody = {
code: 40000,
data: null,
desc: err.message
}
// console.log(err);
res.send(dataBody)
})
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
function getYZToken(){
// 这里填写后台的设置,kdt_id是授权店铺id
getToken(client_id, client_secret, kdt_id, function(err, token){
if(err){
hasError = '初始化有赞接口失败 ' + err.message;
return console.log(hasError)
}
hasError = null;
YZToken = token.access_token;
cache.set("YZToken", YZToken);
console.log('初始化有赞接口成功: ', token)
YZ_Client = new YZClient(new Token(YZToken));
});
}
module.exports = app;