|
|
import checkParams from '../../utils/check-params';
|
|
|
import apiMaps from '../../config/api-map';
|
|
|
import createReport from 'report-error';
|
|
|
|
|
|
const yohoApi = global.yoho.API;
|
|
|
const ufoAPI = global.yoho.UfoAPI;
|
|
|
const serviceApi = global.yoho.ServiceAPI;
|
|
|
const ufoAPI = global.yoho.UfoAPI;
|
|
|
const checkParams = require('../../utils/check-params');
|
|
|
const handleResult = require('../../utils/handle-result');
|
|
|
const apiMaps = require('../../config/api-map');
|
|
|
const errorHandler = require('./error-handler');
|
|
|
|
|
|
|
|
|
const checkApiMap = url => {
|
|
|
return apiMaps[url] ? apiMaps[url] : void 0;
|
|
|
};
|
|
|
const request = async({url, method, reqParams = {}, context}) => {
|
|
|
const apiInfo = checkApiMap(url);
|
|
|
const {env, user} = context;
|
|
|
module.exports = async(req, res, next) => {
|
|
|
res.set({
|
|
|
'Cache-Control': 'no-cache',
|
|
|
Pragma: 'no-cache',
|
|
|
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
|
|
|
});
|
|
|
const apiInfo = apiMaps[req.path];
|
|
|
|
|
|
if (!apiInfo) {
|
|
|
return Promise.reject(new Error(`未找到对应的接口:${url}`));
|
|
|
return next();
|
|
|
}
|
|
|
const baseParams = {};
|
|
|
|
|
|
req.route = {
|
|
|
path: req.path
|
|
|
};
|
|
|
|
|
|
if (!apiInfo.service) {
|
|
|
Object.assign(reqParams, {
|
|
|
uid: (user && user.uid) ? {
|
|
|
baseParams.method = apiInfo.api;
|
|
|
}
|
|
|
if (apiInfo.auth) {
|
|
|
if (req.user && req.user.uid) {
|
|
|
baseParams.uid = {
|
|
|
toString: () => {
|
|
|
return user.uid;
|
|
|
return req.user.uid;
|
|
|
},
|
|
|
sessionKey: user.sessionKey,
|
|
|
appSessionType: user.appSessionType
|
|
|
} : 1,
|
|
|
method: apiInfo.api,
|
|
|
});
|
|
|
sessionKey: req.user.sessionKey,
|
|
|
appSessionType: req.user.appSessionType
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
const params = checkParams.getParams(reqParams, apiInfo);
|
|
|
const cache = method.toLowerCase() !== 'get' ? false : apiInfo.cache;
|
|
|
const headers = {
|
|
|
'X-YOHO-IP': env.clientIp,
|
|
|
'X-Forwarded-For': env.clientIp,
|
|
|
'User-Agent': 'yoho/nodejs'
|
|
|
};
|
|
|
|
|
|
try {
|
|
|
const reqParams = Object.assign({}, req.query, req.body, baseParams);
|
|
|
const params = checkParams.getParams(reqParams, apiInfo, req);
|
|
|
const cache = (req.method.toLowerCase() !== 'get' || apiInfo.auth) ? false : apiInfo.cache;
|
|
|
|
|
|
let method = req.method.toLowerCase() === 'post' ? 'post' : 'get';
|
|
|
|
|
|
let result;
|
|
|
|
|
|
let apiCtx = req.ctx(global.yoho.BaseModel);
|
|
|
|
|
|
if (apiInfo.service) {
|
|
|
return await serviceApi.get(`${apiInfo.api}${apiInfo.path}`, params, {
|
|
|
cache: cache,
|
|
|
headers
|
|
|
result = await apiCtx.get({
|
|
|
api: serviceApi,
|
|
|
url: `${apiInfo.api || ''}${apiInfo.path || ''}`,
|
|
|
data: params,
|
|
|
param: {
|
|
|
cache: cache,
|
|
|
}
|
|
|
});
|
|
|
} else if (apiInfo.ufo) {
|
|
|
return await ufoAPI[method](`${apiInfo.path || ''}`, params, {
|
|
|
cache: cache,
|
|
|
headers
|
|
|
result = await apiCtx[method]({
|
|
|
api: ufoAPI,
|
|
|
url: apiInfo.path || '',
|
|
|
data: params,
|
|
|
param: {
|
|
|
cache: cache
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
return await yohoApi[method](`${apiInfo.path || ''}`, params, {
|
|
|
cache: cache,
|
|
|
headers
|
|
|
result = await apiCtx[method]({
|
|
|
data: params,
|
|
|
url: apiInfo.path || '',
|
|
|
param: {
|
|
|
cache: cache
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
} catch (error) {
|
|
|
return Promise.reject({
|
|
|
code: error.code || 500,
|
|
|
message: error.message || '服务器错误'
|
|
|
if (result) {
|
|
|
return res.json(handleResult(result, apiInfo));
|
|
|
}
|
|
|
return res.json({
|
|
|
code: 400
|
|
|
});
|
|
|
} catch (error) {
|
|
|
return errorHandler.serverError(error, req, res, next);
|
|
|
}
|
|
|
}; |
|
|
|
|
|
const catchError = (context, reqParams) => {
|
|
|
return result => {
|
|
|
if (result && result.code === 500) {
|
|
|
createReport(context, 'api')(Object.assign({
|
|
|
api: reqParams.method,
|
|
|
}, result));
|
|
|
}
|
|
|
return result;
|
|
|
};
|
|
|
};
|
|
|
|
|
|
export const createApi = context => {
|
|
|
return {
|
|
|
get(url, reqParams = {}) {
|
|
|
return request({url, method: 'get', reqParams, context}).then(catchError(context, reqParams));
|
|
|
},
|
|
|
post(url, reqParams = {}) {
|
|
|
return request({url, method: 'post', reqParams, context}).then(catchError(context, reqParams));
|
|
|
}
|
|
|
};
|
|
|
}; |
...
|
...
|
|