Authored by htoooth

receview

/**
* 最近浏览controller
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/10/11
*/
'use strict';
const rvModel = require('../models/recent-view');
const _ = require('lodash');
const index = (req, res, next) => {
let limit = req.query.limit;
let browserSkn = decodeURIComponent(req.cookies._browseskn);
// 拆解skn
let skn = browserSkn ? browserSkn.replace(/\-(\d)+(\,){0,1}/g, ',') : '';
// 去除skn字符串的最后一个多余的,
if (skn && skn.lastIndexOf(',') === skn.length - 1) {
skn = skn.slice(0, -1);
}
if (!skn) {
res.jsonp({
code: 200,
data: [],
message: 'User info'
});
} else {
skn = _.slice(_.uniq(skn.split(',')), 0, limit).join(','); // 去重+截取
rvModel.index(skn, limit).then(data => {
res.jsonp(data);
}).catch(next);
}
};
module.exports = {
index
};
... ...
/**
* sub app common
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/10/11
*/
var express = require('express'),
path = require('path'),
hbs = require('express-handlebars');
var app = express();
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
app.on('mount', function(parent) {
delete parent.locals.settings; // 不继承父 App 的设置
Object.assign(app.locals, parent.locals);
});
app.set('views', path.join(__dirname, 'views/action'));
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`],
helpers: global.yoho.helpers
}));
// router
app.use(require('./router'));
module.exports = app;
... ...
/**
* recent view model
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/10/11
*/
'use strict';
const _ = require('lodash');
const api = global.yoho.API;
const helper = global.yoho.helpers;
const index = (skn, limit) => {
return api.get('', {
method: 'h5.product.batch',
productSkn: skn,
limit: limit
}).then(result => {
if (result.code === 200) {
let data = [];
let historyProduct = result.data.product_list;
_.forEach(historyProduct, hp => {
if (!hp) {
return;
}
let mp = hp.market_price;
let sp = hp.sales_price;
let defaultGoods = _.find(hp.goods_list, {is_default: 'Y'});
// 无默认商品取商品列表第一个
if (!defaultGoods) {
defaultGoods = hp.goods_list[0];
}
data.push({
market_price: mp === sp ? '' : ${helper.round(mp, 2)}`,
price: ${helper.round(sp, 2)}`,
product_name: hp.product_name,
url: helper.urlFormat(
`/product/pro_${hp.product_id}_${defaultGoods.goods_id}/${hp.cn_alphabet}.html`, '', 'item'),
pic_url: helper.image(defaultGoods.images_url, 150, 200, 2, 70)
});
});
return {
code: 200,
data: data,
message: result.message
};
} else {
// get list error
return {
code: result.code,
message: result.message,
data: []
};
}
});
};
module.exports = {
index
};
\ No newline at end of file
... ...
/**
* router of sub app common
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/10/11
*/
'use strict';
const router = require('express').Router(); // eslint-disable-line
const cRoot = './controllers';
const rvCtrl = require(`${cRoot}/recent-view`);
router.get('/recentReview', rvCtrl.index); // 最近浏览
module.exports = router;
... ...