Authored by yyq

req ctx

@@ -6,8 +6,6 @@ @@ -6,8 +6,6 @@
6 */ 6 */
7 'use strict'; 7 'use strict';
8 8
9 -// const api = global.yoho.API;  
10 -  
11 const yhChannel = { 9 const yhChannel = {
12 men: { 10 men: {
13 channel: '301' 11 channel: '301'
1 'use strict'; 1 'use strict';
2 2
3 -// const api = global.yoho.API;  
4 const service = global.yoho.ServiceAPI; 3 const service = global.yoho.ServiceAPI;
5 const processResources = require(`${global.utils}/beautify/resources`); 4 const processResources = require(`${global.utils}/beautify/resources`);
6 const processProductList = require(`${global.utils}/beautify/product`); 5 const processProductList = require(`${global.utils}/beautify/product`);
@@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
6 6
7 'use strict'; 7 'use strict';
8 8
  9 +const _ = require('lodash');
  10 +
9 module.exports = () => { 11 module.exports = () => {
10 return (req, res, next) => { 12 return (req, res, next) => {
11 const channelMap = { 13 const channelMap = {
@@ -43,6 +45,25 @@ module.exports = () => { @@ -43,6 +45,25 @@ module.exports = () => {
43 yoho.isAndroid = /Android/i.test(userAgent); 45 yoho.isAndroid = /Android/i.test(userAgent);
44 yoho.isYohoBuy = /YohoBuy/i.test(userAgent); 46 yoho.isYohoBuy = /YohoBuy/i.test(userAgent);
45 47
  48 + // client ip
  49 + yoho.clientIp = (function() {
  50 + let remoteIp = req.get('X-Yoho-Real-IP') || req.get('X-Forwarded-For') || req.get('X-Real-IP') || req.ip || ''; // eslint-disable-line
  51 +
  52 + if (remoteIp.indexOf(',') > 0) {
  53 + let arr = remoteIp.split(',');
  54 +
  55 + remoteIp = _.trim(arr[0]);
  56 + }
  57 +
  58 + if (_.startsWith(remoteIp, '10.66.')) {
  59 + remoteIp = req.get('X-Real-IP');
  60 + }
  61 +
  62 + remoteIp = _.trim(remoteIp);
  63 +
  64 + return remoteIp;
  65 + }());
  66 +
46 Object.assign(res.locals, yoho); 67 Object.assign(res.locals, yoho);
47 Object.assign(req.yoho, yoho); 68 Object.assign(req.yoho, yoho);
48 69
1 -const api = global.yoho.API;  
2 const service = global.yoho.ServiceAPI; 1 const service = global.yoho.ServiceAPI;
3 const logger = global.yoho.logger; 2 const logger = global.yoho.logger;
4 const checkParams = require('../../utils/check-params'); 3 const checkParams = require('../../utils/check-params');
@@ -28,15 +27,25 @@ module.exports = () => { @@ -28,15 +27,25 @@ module.exports = () => {
28 27
29 let result; 28 let result;
30 29
  30 + let apiCtx = req.ctx(global.yoho.BaseModel);
  31 +
31 if (apiInfo.service) { 32 if (apiInfo.service) {
32 - result = await service.get(apiInfo.api, params, {  
33 - cache: cache,  
34 - code: 200 33 + result = await apiCtx.get({
  34 + api: service,
  35 + url: apiInfo.api,
  36 + data: params,
  37 + param: {
  38 + cache: cache,
  39 + code: 200
  40 + }
35 }); 41 });
36 } else { 42 } else {
37 - result = await api[method]('', params, {  
38 - code: 200,  
39 - cache: cache 43 + result = await apiCtx[method]({
  44 + data: params,
  45 + param: {
  46 + code: 200,
  47 + cache: cache
  48 + }
40 }); 49 });
41 } 50 }
42 if (result) { 51 if (result) {
@@ -62,7 +62,8 @@ const getContext = (req) => { @@ -62,7 +62,8 @@ const getContext = (req) => {
62 isiOS: req.yoho.isiOS, 62 isiOS: req.yoho.isiOS,
63 isAndroid: req.yoho.isAndroid, 63 isAndroid: req.yoho.isAndroid,
64 isYohoBuy: req.yoho.isYohoBuy, 64 isYohoBuy: req.yoho.isYohoBuy,
65 - channel: req.yoho.channel 65 + channel: req.yoho.channel,
  66 + clientIp: req.yoho.clientIp
66 } 67 }
67 }; 68 };
68 }; 69 };
@@ -35,12 +35,21 @@ const request = async ({url, method, reqParams, context}) => { @@ -35,12 +35,21 @@ const request = async ({url, method, reqParams, context}) => {
35 if (apiInfo.service) { 35 if (apiInfo.service) {
36 return await service.get(apiInfo.api, params, { 36 return await service.get(apiInfo.api, params, {
37 cache: cache, 37 cache: cache,
38 - code: 200 38 + code: 200,
  39 + headers: {
  40 + 'X-YOHO-IP': context.env.clientIp,
  41 + 'X-Forwarded-For': context.env.clientIp
  42 + }
39 }); 43 });
  44 + headers
40 } else { 45 } else {
41 return await api[method]('', params, { 46 return await api[method]('', params, {
42 code: 200, 47 code: 200,
43 - cache: cache 48 + cache: cache,
  49 + headers: {
  50 + 'X-YOHO-IP': context.env.clientIp,
  51 + 'X-Forwarded-For': context.env.clientIp
  52 + }
44 }); 53 });
45 } 54 }
46 } catch (e) { 55 } catch (e) {