Authored by 徐炜

Merge branch 'feature/customerService' into feature/customerServiceDemo

@@ -82,11 +82,6 @@ app.use((req, res, next) => { @@ -82,11 +82,6 @@ app.use((req, res, next) => {
82 req.session = {}; 82 req.session = {};
83 } 83 }
84 84
85 - // 获取客服开关,读cache  
86 - global.yoho.cache.get('customerServiceSwitch').then(resule => {  
87 - res.locals.customerServiceSwitch = resule || false;  
88 - }).catch(next);  
89 -  
90 next(); 85 next();
91 }); 86 });
92 87
@@ -104,6 +99,7 @@ try { @@ -104,6 +99,7 @@ try {
104 const errorHanlder = require('./doraemon/middleware/error-handler'); 99 const errorHanlder = require('./doraemon/middleware/error-handler');
105 const setPageInfo = require('./doraemon/middleware/set-pageinfo'); 100 const setPageInfo = require('./doraemon/middleware/set-pageinfo');
106 const pageCache = require('./doraemon/middleware/page-cache'); 101 const pageCache = require('./doraemon/middleware/page-cache');
  102 + const secretSwitch = require('./doraemon/middleware/secret-switch');
107 103
108 // YOHO 前置中间件 104 // YOHO 前置中间件
109 app.use(subDomain()); 105 app.use(subDomain());
@@ -114,7 +110,7 @@ try { @@ -114,7 +110,7 @@ try {
114 app.use(user()); 110 app.use(user());
115 app.use(seo()); 111 app.use(seo());
116 app.use(setPageInfo()); 112 app.use(setPageInfo());
117 - 113 + app.use(secretSwitch());
118 app.use(pageCache()); 114 app.use(pageCache());
119 require('./dispatch')(app); 115 require('./dispatch')(app);
120 116
@@ -8,16 +8,13 @@ @@ -8,16 +8,13 @@
8 const cache = global.yoho.cache; 8 const cache = global.yoho.cache;
9 9
10 const index = (req, res, next) => { 10 const index = (req, res, next) => {
11 - let customerServiceSwitch = req.query.customerServiceSwitch; 11 + const param = req.query['com.yohobuy.customerservice.enabled'];
  12 + let status = param === 'true';
12 13
13 - if (typeof customerServiceSwitch === 'undefined') {  
14 - res.json(false);  
15 - return;  
16 - }  
17 -  
18 - cache.set('customerServiceSwitch', customerServiceSwitch ? true : false, -1).then(result => { 14 + cache.set('customerServiceSwitch', status, 0).then(result => {
19 res.json(result); 15 res.json(result);
20 }).catch(next); 16 }).catch(next);
  17 +
21 }; 18 };
22 19
23 module.exports = { 20 module.exports = {
@@ -13,7 +13,7 @@ var multipart = require('connect-multiparty'); @@ -13,7 +13,7 @@ var multipart = require('connect-multiparty');
13 var multipartMiddleware = multipart(); 13 var multipartMiddleware = multipart();
14 14
15 const rvCtrl = require(`${cRoot}/recent-view`); 15 const rvCtrl = require(`${cRoot}/recent-view`);
16 -const cache = require(`${cRoot}/cache`); 16 +const cache = require(`${cRoot}/secret-switch`);
17 const uploadCtrl = require(`${cRoot}/upload`); 17 const uploadCtrl = require(`${cRoot}/upload`);
18 const erp2goods = require(`${cRoot}/erp2goods`); 18 const erp2goods = require(`${cRoot}/erp2goods`);
19 const getBanner = require(`${cRoot}/getBanner`); 19 const getBanner = require(`${cRoot}/getBanner`);
@@ -25,7 +25,7 @@ router.post('/upload/image', multipartMiddleware, uploadCtrl.uploadImg); @@ -25,7 +25,7 @@ router.post('/upload/image', multipartMiddleware, uploadCtrl.uploadImg);
25 25
26 router.get('/erp2goods', erp2goods.find); 26 router.get('/erp2goods', erp2goods.find);
27 27
28 -router.get('/cache', cache.index); // 设置cache 28 +router.get('/secret-switch', cache.index); // 设置cache
29 29
30 router.get('/getbanner', getBanner.index); 30 router.get('/getbanner', getBanner.index);
31 31
  1 +const logger = global.yoho.logger;
  2 +
  3 +const secretSwitch = () => {
  4 + return (req, res, next) => {
  5 + req.user = {}; // 全局的用户数据
  6 + req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
  7 +
  8 + if (!req.session) {
  9 + req.session = {};
  10 + }
  11 +
  12 + // 获取客服开关,读cache
  13 + global.yoho.cache.get('customerServiceSwitch').then(result => {
  14 + Object.assign(res.locals, {
  15 + customerServiceSwitch: result || false
  16 + });
  17 + next();
  18 + }).catch((err) => {
  19 + logger.error(err);
  20 + next();
  21 + });
  22 + };
  23 +};
  24 +
  25 +module.exports = secretSwitch;