Authored by 徐炜

channel ssr

@@ -23,6 +23,7 @@ const memcached = require('connect-memcached'); @@ -23,6 +23,7 @@ const memcached = require('connect-memcached');
23 const hbs = require('express-handlebars'); 23 const hbs = require('express-handlebars');
24 const pkg = require('./package.json'); 24 const pkg = require('./package.json');
25 const devtools = require('./doraemon/middleware/devtools'); 25 const devtools = require('./doraemon/middleware/devtools');
  26 +const _ = require('lodash');
26 27
27 const app = express(); 28 const app = express();
28 const MemcachedStore = memcached(session); 29 const MemcachedStore = memcached(session);
@@ -52,7 +53,7 @@ app.engine('.hbs', hbs({ @@ -52,7 +53,7 @@ app.engine('.hbs', hbs({
52 defaultLayout: 'layout', 53 defaultLayout: 'layout',
53 layoutsDir: './doraemon/views', 54 layoutsDir: './doraemon/views',
54 partialsDir: './doraemon/views/partial', 55 partialsDir: './doraemon/views/partial',
55 - helpers: global.yoho.helpers 56 + helpers: _.assign(global.yoho.helpers, require('./utils/helpers'))
56 })); 57 }));
57 58
58 app.use(favicon(path.join(__dirname, '/public/favicon.ico'))); 59 app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
@@ -5,6 +5,12 @@ @@ -5,6 +5,12 @@
5 */ 5 */
6 'use strict'; 6 'use strict';
7 const channelModel = require('../models/channel'); 7 const channelModel = require('../models/channel');
  8 +const channelMap = {
  9 + men: '9ee58aadd9559d07207fe4a98843eaac',
  10 + women: '3ad8826fc89fb0d023a4cd06a6991219',
  11 + lifestyle: 'aa8d34c85934c2ccc16e2babd3eb5e47'
  12 +};
  13 +const _ = require('lodash');
8 14
9 /** 15 /**
10 * 频道选择页 16 * 频道选择页
@@ -13,11 +19,27 @@ module.exports = { @@ -13,11 +19,27 @@ module.exports = {
13 index(req, res) { 19 index(req, res) {
14 let channel = req.path.split('/')[1] || req.yoho.channel; 20 let channel = req.path.split('/')[1] || req.yoho.channel;
15 21
16 - res.render('index', {  
17 - module: 'channel',  
18 - page: 'home',  
19 - channel: channel 22 + channelModel.getResourcesData({
  23 + contentCode: channelMap[channel]
  24 + }).then(result => {
  25 + const resources = result.slice(0, 3);
  26 +
  27 + _.each(resources, (resource) => {
  28 + // 只拿第一个数据
  29 + if (_.isArray(resource.data)) {
  30 + resource.data = [resource.data[0]];
  31 + }
  32 + });
  33 +
  34 + res.locals.resources = resources;
  35 +
  36 + res.render('index', {
  37 + module: 'channel',
  38 + page: 'home',
  39 + channel: channel
  40 + });
20 }); 41 });
  42 +
21 }, 43 },
22 channel(req, res, next) { 44 channel(req, res, next) {
23 channelModel.getChannelData().then(result => { 45 channelModel.getChannelData().then(result => {
1 <div id="channel"> 1 <div id="channel">
2 - <channel></channel> 2 + <div id="ssr" class="resources">
  3 + {{#resources}}
  4 + {{#if focus}}
  5 + <div class="focus-floor" style="height: 9.1rem;">
  6 + {{#data}}
  7 + <a href="{{this.url}}" title="{{this.title}}">
  8 + <img src="{{image2 this.src w=750 h=365 q=60}}" width="375" alt="">
  9 + </a>
  10 + {{/data}}
  11 + </div>
  12 + {{/if}}
  13 +
  14 + {{#if titleImage}}
  15 + <div class="title-image">
  16 + {{#data}}
  17 + <div class="floor-header">
  18 + {{title}}
  19 +
  20 + <a class="more" href="{{moreUrl}}">
  21 + {{#eq moreName '...'}}
  22 + <span class="icon icon-more"></span>
  23 + {{^}}
  24 + <!--<span>{{{moreName}}}</span>-->
  25 + {{/eq}}
  26 + </a>
  27 + </div>
  28 +
  29 + <a class="image" href="{{image.url}}">
  30 + <img src="{{image2 image.src w=750 h=365 q=60}}" width="375" alt="">
  31 + </a>
  32 + </div>
  33 + {{/data}}
  34 +
  35 + {{/if}}
  36 +
  37 + {{/resources}}
  38 + </div>
  39 +
  40 + <channel>
  41 + </channel>
3 </div> 42 </div>
@@ -93,9 +93,12 @@ @@ -93,9 +93,12 @@
93 if (result.length) { 93 if (result.length) {
94 dataCache[param] = result; 94 dataCache[param] = result;
95 } 95 }
96 - }).fail(() => { 96 +
  97 + $('#ssr').remove();
  98 +
  99 + }).fail(() => {
97 tip('网络错误'); 100 tip('网络错误');
98 - }); 101 + });
99 } 102 }
100 }, 103 },
101 created() { 104 created() {
  1 +'use strict';
  2 +
  3 +const url = require('url');
  4 +const _ = require('lodash');
  5 +const config = require('../config/common');
  6 +const assetUrl = config.assetUrl;
  7 +
  8 +module.exports = {
  9 + imgSrc: function(imgSrc) {
  10 + return url.resolve(assetUrl, imgSrc);
  11 + },
  12 + image2: function(imageUrl, opts) {
  13 + if (imageUrl && _.isString(imageUrl)) {
  14 + let params = opts.hash;
  15 + let urls = imageUrl.split('?');
  16 + let query = urls[1] || '';
  17 + let uri = urls[0];
  18 +
  19 + if (uri.indexOf('http:') === 0) {
  20 + uri = uri.replace('http:', '');
  21 + }
  22 +
  23 + if (query) {
  24 + query = query.replace(/{width}/g, params.w).replace(/{height}/g, params.h).replace(/{mode}/g, (params.mode || 2));
  25 +
  26 + if (query.indexOf('imageView2') === 0) {
  27 + if (params.q && query.indexOf('/q/') > 0) {
  28 + query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
  29 + } else if (params.q) {
  30 + query += '/q/' + params.q;
  31 + }
  32 + } else if (query.indexOf('imageMogr2') === 0) {
  33 + if (params.q && query.indexOf('/quality/') > 0) {
  34 + query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
  35 + } else if (params.q) {
  36 + query += '/quality/' + params.q;
  37 + }
  38 + } else if (query.indexOf('imageView/') === 0) {
  39 + if (params.q && query.indexOf('/q/') > 0) {
  40 + query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
  41 + } else if (params.q) {
  42 + query += '/q/' + params.q;
  43 + }
  44 +
  45 + if (params.mode) {
  46 + query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
  47 + }
  48 + }
  49 + } else {
  50 + query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
  51 + }
  52 + return uri + '?' + query;
  53 + } else {
  54 + return '';
  55 + }
  56 + },
  57 + ifor: function() {
  58 + var args = Array.prototype.slice.call(arguments);
  59 + var opt = args[args.length - 1];
  60 + var isTrue = false;
  61 +
  62 + for (var i = 0; i < args.length - 1; i++) {
  63 + if (args[i]) {
  64 + isTrue = true;
  65 + break;
  66 + }
  67 + }
  68 +
  69 + if (isTrue) {
  70 + return opt.fn(this);
  71 + } else {
  72 + return opt.inverse(this);
  73 + }
  74 + },
  75 +
  76 + /**
  77 + * 小于某zhi
  78 + *
  79 + * @param variable
  80 + * @param number
  81 + */
  82 + within: function(variable, number, opt) {
  83 + if (variable < number) {
  84 + return opt.fn(this);
  85 + } else {
  86 + return opt.inverse(this);
  87 + }
  88 + },
  89 + eq: function(a, b, options) {
  90 + if (arguments.length === 2) {
  91 + options = b;
  92 + b = options.hash.compare;
  93 + }
  94 + if (a === b) {
  95 + return options.fn(this);
  96 + }
  97 + return options.inverse(this);
  98 + }
  99 +}
  100 +;