Authored by htoooth

receview

  1 +/**
  2 + * 最近浏览controller
  3 + * @author: xuqi<qi.xu@yoho.cn>
  4 + * @date: 2016/10/11
  5 + */
  6 +
  7 +'use strict';
  8 +
  9 +const rvModel = require('../models/recent-view');
  10 +const _ = require('lodash');
  11 +
  12 +const index = (req, res, next) => {
  13 +
  14 + let limit = req.query.limit;
  15 +
  16 + let browserSkn = decodeURIComponent(req.cookies._browseskn);
  17 +
  18 + // 拆解skn
  19 + let skn = browserSkn ? browserSkn.replace(/\-(\d)+(\,){0,1}/g, ',') : '';
  20 +
  21 + // 去除skn字符串的最后一个多余的,
  22 + if (skn && skn.lastIndexOf(',') === skn.length - 1) {
  23 + skn = skn.slice(0, -1);
  24 + }
  25 +
  26 + if (!skn) {
  27 + res.jsonp({
  28 + code: 200,
  29 + data: [],
  30 + message: 'User info'
  31 + });
  32 + } else {
  33 + skn = _.slice(_.uniq(skn.split(',')), 0, limit).join(','); // 去重+截取
  34 + rvModel.index(skn, limit).then(data => {
  35 + res.jsonp(data);
  36 + }).catch(next);
  37 + }
  38 +
  39 +};
  40 +
  41 +module.exports = {
  42 + index
  43 +};
  1 +/**
  2 + * sub app common
  3 + * @author: xuqi<qi.xu@yoho.cn>
  4 + * @date: 2016/10/11
  5 + */
  6 +
  7 +var express = require('express'),
  8 + path = require('path'),
  9 + hbs = require('express-handlebars');
  10 +
  11 +var app = express();
  12 +
  13 +// set view engin
  14 +var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
  15 +
  16 +app.on('mount', function(parent) {
  17 + delete parent.locals.settings; // 不继承父 App 的设置
  18 + Object.assign(app.locals, parent.locals);
  19 +});
  20 +
  21 +app.set('views', path.join(__dirname, 'views/action'));
  22 +app.engine('.hbs', hbs({
  23 + extname: '.hbs',
  24 + defaultLayout: 'layout',
  25 + layoutsDir: doraemon,
  26 + partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`],
  27 + helpers: global.yoho.helpers
  28 +}));
  29 +
  30 +// router
  31 +app.use(require('./router'));
  32 +
  33 +module.exports = app;
  1 +/**
  2 + * recent view model
  3 + * @author: xuqi<qi.xu@yoho.cn>
  4 + * @date: 2016/10/11
  5 + */
  6 +
  7 +'use strict';
  8 +
  9 +const _ = require('lodash');
  10 +const api = global.yoho.API;
  11 +const helper = global.yoho.helpers;
  12 +
  13 +const index = (skn, limit) => {
  14 +
  15 + return api.get('', {
  16 + method: 'h5.product.batch',
  17 + productSkn: skn,
  18 + limit: limit
  19 + }).then(result => {
  20 +
  21 + if (result.code === 200) {
  22 + let data = [];
  23 + let historyProduct = result.data.product_list;
  24 +
  25 + _.forEach(historyProduct, hp => {
  26 + if (!hp) {
  27 + return;
  28 + }
  29 +
  30 + let mp = hp.market_price;
  31 + let sp = hp.sales_price;
  32 +
  33 + let defaultGoods = _.find(hp.goods_list, {is_default: 'Y'});
  34 +
  35 + // 无默认商品取商品列表第一个
  36 + if (!defaultGoods) {
  37 + defaultGoods = hp.goods_list[0];
  38 + }
  39 +
  40 + data.push({
  41 + market_price: mp === sp ? '' : ${helper.round(mp, 2)}`,
  42 + price: ${helper.round(sp, 2)}`,
  43 + product_name: hp.product_name,
  44 + url: helper.urlFormat(
  45 + `/product/pro_${hp.product_id}_${defaultGoods.goods_id}/${hp.cn_alphabet}.html`, '', 'item'),
  46 + pic_url: helper.image(defaultGoods.images_url, 150, 200, 2, 70)
  47 + });
  48 + });
  49 +
  50 + return {
  51 + code: 200,
  52 + data: data,
  53 + message: result.message
  54 + };
  55 + } else {
  56 +
  57 + // get list error
  58 + return {
  59 + code: result.code,
  60 + message: result.message,
  61 + data: []
  62 + };
  63 + }
  64 + });
  65 +};
  66 +
  67 +module.exports = {
  68 + index
  69 +};
  1 +/**
  2 + * router of sub app common
  3 + * @author: xuqi<qi.xu@yoho.cn>
  4 + * @date: 2016/10/11
  5 + */
  6 +
  7 +'use strict';
  8 +
  9 +const router = require('express').Router(); // eslint-disable-line
  10 +const cRoot = './controllers';
  11 +
  12 +const rvCtrl = require(`${cRoot}/recent-view`);
  13 +
  14 +router.get('/recentReview', rvCtrl.index); // 最近浏览
  15 +
  16 +module.exports = router;