Authored by 徐炜

channel ssr

... ... @@ -23,6 +23,7 @@ const memcached = require('connect-memcached');
const hbs = require('express-handlebars');
const pkg = require('./package.json');
const devtools = require('./doraemon/middleware/devtools');
const _ = require('lodash');
const app = express();
const MemcachedStore = memcached(session);
... ... @@ -52,7 +53,7 @@ app.engine('.hbs', hbs({
defaultLayout: 'layout',
layoutsDir: './doraemon/views',
partialsDir: './doraemon/views/partial',
helpers: global.yoho.helpers
helpers: _.assign(global.yoho.helpers, require('./utils/helpers'))
}));
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
... ...
... ... @@ -5,6 +5,12 @@
*/
'use strict';
const channelModel = require('../models/channel');
const channelMap = {
men: '9ee58aadd9559d07207fe4a98843eaac',
women: '3ad8826fc89fb0d023a4cd06a6991219',
lifestyle: 'aa8d34c85934c2ccc16e2babd3eb5e47'
};
const _ = require('lodash');
/**
* 频道选择页
... ... @@ -13,11 +19,27 @@ module.exports = {
index(req, res) {
let channel = req.path.split('/')[1] || req.yoho.channel;
res.render('index', {
module: 'channel',
page: 'home',
channel: channel
channelModel.getResourcesData({
contentCode: channelMap[channel]
}).then(result => {
const resources = result.slice(0, 3);
_.each(resources, (resource) => {
// 只拿第一个数据
if (_.isArray(resource.data)) {
resource.data = [resource.data[0]];
}
});
res.locals.resources = resources;
res.render('index', {
module: 'channel',
page: 'home',
channel: channel
});
});
},
channel(req, res, next) {
channelModel.getChannelData().then(result => {
... ...
<div id="channel">
<channel></channel>
<div id="ssr" class="resources">
{{#resources}}
{{#if focus}}
<div class="focus-floor" style="height: 9.1rem;">
{{#data}}
<a href="{{this.url}}" title="{{this.title}}">
<img src="{{image2 this.src w=750 h=365 q=60}}" width="375" alt="">
</a>
{{/data}}
</div>
{{/if}}
{{#if titleImage}}
<div class="title-image">
{{#data}}
<div class="floor-header">
{{title}}
<a class="more" href="{{moreUrl}}">
{{#eq moreName '...'}}
<span class="icon icon-more"></span>
{{^}}
<!--<span>{{{moreName}}}</span>-->
{{/eq}}
</a>
</div>
<a class="image" href="{{image.url}}">
<img src="{{image2 image.src w=750 h=365 q=60}}" width="375" alt="">
</a>
</div>
{{/data}}
{{/if}}
{{/resources}}
</div>
<channel>
</channel>
</div>
... ...
... ... @@ -93,9 +93,12 @@
if (result.length) {
dataCache[param] = result;
}
}).fail(() => {
$('#ssr').remove();
}).fail(() => {
tip('网络错误');
});
});
}
},
created() {
... ...
'use strict';
const url = require('url');
const _ = require('lodash');
const config = require('../config/common');
const assetUrl = config.assetUrl;
module.exports = {
imgSrc: function(imgSrc) {
return url.resolve(assetUrl, imgSrc);
},
image2: function(imageUrl, opts) {
if (imageUrl && _.isString(imageUrl)) {
let params = opts.hash;
let urls = imageUrl.split('?');
let query = urls[1] || '';
let uri = urls[0];
if (uri.indexOf('http:') === 0) {
uri = uri.replace('http:', '');
}
if (query) {
query = query.replace(/{width}/g, params.w).replace(/{height}/g, params.h).replace(/{mode}/g, (params.mode || 2));
if (query.indexOf('imageView2') === 0) {
if (params.q && query.indexOf('/q/') > 0) {
query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
} else if (params.q) {
query += '/q/' + params.q;
}
} else if (query.indexOf('imageMogr2') === 0) {
if (params.q && query.indexOf('/quality/') > 0) {
query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
} else if (params.q) {
query += '/quality/' + params.q;
}
} else if (query.indexOf('imageView/') === 0) {
if (params.q && query.indexOf('/q/') > 0) {
query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
} else if (params.q) {
query += '/q/' + params.q;
}
if (params.mode) {
query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
}
}
} else {
query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
}
return uri + '?' + query;
} else {
return '';
}
},
ifor: function() {
var args = Array.prototype.slice.call(arguments);
var opt = args[args.length - 1];
var isTrue = false;
for (var i = 0; i < args.length - 1; i++) {
if (args[i]) {
isTrue = true;
break;
}
}
if (isTrue) {
return opt.fn(this);
} else {
return opt.inverse(this);
}
},
/**
* 小于某zhi
*
* @param variable
* @param number
*/
within: function(variable, number, opt) {
if (variable < number) {
return opt.fn(this);
} else {
return opt.inverse(this);
}
},
eq: function(a, b, options) {
if (arguments.length === 2) {
options = b;
b = options.hash.compare;
}
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
}
}
;
... ...