yoho-action.js 2.62 KB
/**
 * 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() {
        let gender = null;
        let channel = this.getSessionChannel();

        switch (channel) {
            case 'boys':
                gender = '1,3';
                break;
            case 'girls':
                gender = '2,3';
                break;
            default:
                gender = '1,2,3';
        }

        return gender;
    }

    /**
     * 设置网站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;