yoho-action.js
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* YohoAction
*
* @author: Aiden Xu<aiden.xu@yoho.cn>
* @date: 2016/7/11
*/
'use strict';
const AbstractAction = require('./abstract-action');
const Channel = require('./channel');
const moment = require('moment');
const COOKIE_DOMAIN = '.yohobuy.com';
const COOKIE_CHANEL_MAX_AGE = 300;
const _ = require('lodash');
class YohoAction extends AbstractAction {
/**
* 设置网站SEO的标题
*
* @param title 标题
* @param sign 连接的字符串
* @param showMore 是否显示更多内容
* @return void
*/
setTitle(title, showMore, sign) {
showMore = showMore || true;
sign = sign || ' | ';
_.merge(this.renderContext, {
title_more: showMore,
title: title + sign
});
}
getUid() {
return this.request.user.uid;
}
/**
* 获得当前用户所在的频道
*
* @returns {*}
*/
getSessionChannel() {
const channel = this.request.cookies._Channel || Channel.CHANNEL_DEFAULT;
if (!channel) {
// 设置默认频道
this.setSessionChannel(Channel.CHANNEL_BOYS);
}
return channel;
}
/**
* 设置当前用户的频道
*/
setSessionChannel(channel) {
this.response.cookie('_Channel', channel || Channel.CHANNEL_DEFAULT, {
domain: COOKIE_DOMAIN,
maxAge: moment.duration(COOKIE_CHANEL_MAX_AGE, 'days').seconds()
});
}
/**
* 根据用户访问的频道猜测用户性别
*/
guessUserGender() {
switch (this.getSessionChannel()) {
case 'boys':
return '1,3';
case 'girls':
return '2,3';
default:
return '1,2,3';
}
}
/**
* 设置网站SEO的关键词
*
* @param keywords 关键词,多个之间用","逗号分隔
*/
setKeywords(keywords) {
// this->_view->assign('keywords', rtrim(keywords, ',') . ',');
_.merge(this.renderContext, {
keywords: keywords.replace(/~+/, '')
});
}
/**
* 设置网站SEO的描述内容
*
* @param description 描述内容
* @param showMore 是否显示更多内容
* @param sign 连接的字符串
*/
setDescription(description, showMore, sign) {
_.merge(this.renderContext, {
description_more: showMore,
description: description + sign
});
}
}
module.exports = YohoAction;