Authored by 陈峰

透传接口

@@ -30,7 +30,7 @@ const plugin = { @@ -30,7 +30,7 @@ const plugin = {
30 // return Promise.reject(); 30 // return Promise.reject();
31 }, 31 },
32 initPurview(Vue, user) { 32 initPurview(Vue, user) {
33 - axios.defaults.headers.common.shopsId = user.currentShop.id; 33 + Vue.$cookie.set('shopsId', user.currentShop.id);
34 return userService.purviews().then((purviews) => { 34 return userService.purviews().then((purviews) => {
35 this.updateUser(Vue, user, purviews); 35 this.updateUser(Vue, user, purviews);
36 }); 36 });
@@ -26,8 +26,7 @@ let domainApis = { @@ -26,8 +26,7 @@ let domainApis = {
26 exportSellerProductList: '/SellerProductController/exportSellerProductList', 26 exportSellerProductList: '/SellerProductController/exportSellerProductList',
27 updateSellerPrice: '/SellerPriceController/updateSellerPrice', 27 updateSellerPrice: '/SellerPriceController/updateSellerPrice',
28 updateProduct: '/SellerProductController/updateProduct', 28 updateProduct: '/SellerProductController/updateProduct',
29 - getProduct: '/SellerProductController/getProduct',  
30 - image2: '/SellerShopController/queryShopsByAdminPid', 29 + getProduct: '/SellerProductController/getProduct'
31 } 30 }
32 }; 31 };
33 32
  1 +const _ = require('lodash');
1 const Context = require('./context'); 2 const Context = require('./context');
2 -const request = require('request-promise'); 3 +const rp = require('request-promise');
  4 +const request = require('request');
3 5
4 const logger = global.yoho.logger; 6 const logger = global.yoho.logger;
5 7
6 -const defaultOptions = {  
7 - resolveWithFullResponse: true  
8 -}; 8 +
9 9
10 const API_INTERNAL_ERROR = { 10 const API_INTERNAL_ERROR = {
11 code: 500, 11 code: 500,
@@ -44,16 +44,35 @@ class Api extends Context { @@ -44,16 +44,35 @@ class Api extends Context {
44 headers: Object.assign(defaultHeader, headers) 44 headers: Object.assign(defaultHeader, headers)
45 }); 45 });
46 } 46 }
47 - download() { 47 + proxy(url, data, options) {
  48 + let params = _.merge({
  49 + url,
  50 + headers: {
  51 + 'Content-Type': 'application/json'
  52 + }
  53 + }, options);
  54 +
  55 + if (options.method === 'get') {
  56 + params.qs = data;
  57 + } else {
  58 + params.body = JSON.stringify(data);
  59 + }
  60 +
  61 + return request(params, (error, response) => {
  62 + if (!error) {
  63 + logger.info(`api call ${response.statusCode} [${response.request.method}] ${response.request.href} ${response.request.body}`); // eslint-disable-line
  64 + }
  65 + });
48 } 66 }
49 _request(options) { 67 _request(options) {
50 - options = Object.assign(options, defaultOptions);  
51 -  
52 - return request[options.method || 'get'](options).then(response => { 68 + options = Object.assign(options, {
  69 + resolveWithFullResponse: true
  70 + });
  71 + return rp[options.method || 'get'](options).then(response => {
53 let jsonBody = JSON.parse(response.body); 72 let jsonBody = JSON.parse(response.body);
54 let req = response.request; 73 let req = response.request;
55 74
56 - logger.info(`api call ${req.statusCode} [${req.method}] ${req.uri.href} ${req.body || ''}`); 75 + logger.info(`api call ${response.statusCode} [${req.method}] ${req.uri.href} ${req.body || ''}`);
57 return jsonBody; 76 return jsonBody;
58 }).catch(err => { 77 }).catch(err => {
59 logger.error(`api call ${err.statusCode} [${err.options.method}] 78 logger.error(`api call ${err.statusCode} [${err.options.method}]
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 const Context = require('../common/context'); 2 const Context = require('../common/context');
3 const nodeExcel = require('excel-export'); 3 const nodeExcel = require('excel-export');
4 4
  5 +// 暂时弃用
5 class OutputController extends Context { 6 class OutputController extends Context {
6 constructor() { 7 constructor() {
7 super(); 8 super();
@@ -42,7 +42,7 @@ module.exports = (req, res, next) => { @@ -42,7 +42,7 @@ module.exports = (req, res, next) => {
42 }); 42 });
43 } 43 }
44 let userShops = req.user.shops; 44 let userShops = req.user.shops;
45 - let currentShop = _.find(userShops, shop => shop.id === req.get('shopsId')); 45 + let currentShop = _.find(userShops, shop => shop.id === req.cookies.shopsId);
46 46
47 if (currentShop) { 47 if (currentShop) {
48 let channel = apiMap.split('.')[0]; 48 let channel = apiMap.split('.')[0];
@@ -56,14 +56,17 @@ module.exports = (req, res, next) => { @@ -56,14 +56,17 @@ module.exports = (req, res, next) => {
56 } else if (channel === 'platform') { 56 } else if (channel === 'platform') {
57 baseParams = { 57 baseParams = {
58 shopsId: currentShop.shopsId, 58 shopsId: currentShop.shopsId,
  59 + shopId: currentShop.shopsId,
59 userId: req.session.LOGIN_UID 60 userId: req.session.LOGIN_UID
60 }; 61 };
61 } 62 }
62 let params = Object.assign(req.query, req.body, baseParams); 63 let params = Object.assign(req.query, req.body, baseParams);
63 64
64 - return api[req.method.toLowerCase()](apiUrl, params).then(data => {  
65 - res.json(data);  
66 - }).catch(next); 65 + return api.proxy(apiUrl, params, {
  66 + method: req.method.toLowerCase()
  67 + }).on('error', error => {
  68 + next({code: 500, message: error});
  69 + }).pipe(res);
67 } 70 }
68 return res.status(401).json({ 71 return res.status(401).json({
69 code: 401, 72 code: 401,