Authored by yyq

req ctx

... ... @@ -6,8 +6,6 @@
*/
'use strict';
// const api = global.yoho.API;
const yhChannel = {
men: {
channel: '301'
... ...
'use strict';
// const api = global.yoho.API;
const service = global.yoho.ServiceAPI;
const processResources = require(`${global.utils}/beautify/resources`);
const processProductList = require(`${global.utils}/beautify/product`);
... ...
... ... @@ -6,6 +6,8 @@
'use strict';
const _ = require('lodash');
module.exports = () => {
return (req, res, next) => {
const channelMap = {
... ... @@ -43,6 +45,25 @@ module.exports = () => {
yoho.isAndroid = /Android/i.test(userAgent);
yoho.isYohoBuy = /YohoBuy/i.test(userAgent);
// client ip
yoho.clientIp = (function() {
let remoteIp = req.get('X-Yoho-Real-IP') || req.get('X-Forwarded-For') || req.get('X-Real-IP') || req.ip || ''; // eslint-disable-line
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = _.trim(arr[0]);
}
if (_.startsWith(remoteIp, '10.66.')) {
remoteIp = req.get('X-Real-IP');
}
remoteIp = _.trim(remoteIp);
return remoteIp;
}());
Object.assign(res.locals, yoho);
Object.assign(req.yoho, yoho);
... ...
const api = global.yoho.API;
const service = global.yoho.ServiceAPI;
const logger = global.yoho.logger;
const checkParams = require('../../utils/check-params');
... ... @@ -28,15 +27,25 @@ module.exports = () => {
let result;
let apiCtx = req.ctx(global.yoho.BaseModel);
if (apiInfo.service) {
result = await service.get(apiInfo.api, params, {
cache: cache,
code: 200
result = await apiCtx.get({
api: service,
url: apiInfo.api,
data: params,
param: {
cache: cache,
code: 200
}
});
} else {
result = await api[method]('', params, {
code: 200,
cache: cache
result = await apiCtx[method]({
data: params,
param: {
code: 200,
cache: cache
}
});
}
if (result) {
... ...
... ... @@ -62,7 +62,8 @@ const getContext = (req) => {
isiOS: req.yoho.isiOS,
isAndroid: req.yoho.isAndroid,
isYohoBuy: req.yoho.isYohoBuy,
channel: req.yoho.channel
channel: req.yoho.channel,
clientIp: req.yoho.clientIp
}
};
};
... ...
... ... @@ -35,12 +35,21 @@ const request = async ({url, method, reqParams, context}) => {
if (apiInfo.service) {
return await service.get(apiInfo.api, params, {
cache: cache,
code: 200
code: 200,
headers: {
'X-YOHO-IP': context.env.clientIp,
'X-Forwarded-For': context.env.clientIp
}
});
headers
} else {
return await api[method]('', params, {
code: 200,
cache: cache
cache: cache,
headers: {
'X-YOHO-IP': context.env.clientIp,
'X-Forwarded-For': context.env.clientIp
}
});
}
} catch (e) {
... ...