index.js 14.3 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
/**
 * guang controller
 * @author: lcy<chuanyang.liu@yoho.cn>
 * @date: 2016/08/31
 */

'use strict';

const _ = require('lodash');

const guangModel = require('../models/guang-service');
const headerModel = require('../../../doraemon/models/header');
const ghelper = require('../models/guang-helper');
const urlHelper = require('../models/url-helper');
const querystring = require('querystring');

/**
 * 首页文章列表 类型列表
 */
exports.index = (req, res, next) => {
    let channel = req.yoho.channel;
    let page = req.query.page || 1;
    let pageSize = req.query.pageSize || 20;
    let type = req.query.type || 0;

    let pathNav = req.ctx(guangModel).getPathNav(channel);
    let uid = req.user.uid;
    let udid = ghelper.getUdid(req, res);
    let gender = ghelper.getGenderByCookie(req);
    let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
    let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);

    const reqCtx = req.ctx(guangModel);

    Promise.all([
        reqCtx.getBanner(channel),
        reqCtx.getCategory(type, channel),
        reqCtx.getArticleList(gender, type, uid, udid, page, '', '', pageSize, channel, true),
        reqCtx.getHotTags(page, pageSize, channel, isHotDegrade),
        reqCtx.getAds(channel, isAdDegrade),
        reqCtx.getRecoArticles(gender, 1, 10, channel),
        headerModel.requestHeaderData(channel),
        reqCtx.getTopList(gender, uid, udid, page, true)
    ]).then(ret => {

        if (_.get(ret[2], 'msgs') || _.get(ret[7], 'msgs')) {
            res.set('Cache-Control', 'no-cache');
        }

        let seoTdk = ghelper.getIndexSeo(req.query, ret[1]);

        res.render('guang/index', Object.assign(seoTdk, {
            guang: {
                slider: ret[0],
                msgTypes: ret[1],
                msgs: (+type === 0 && +page === 1 && ret[7] && ret[7].msgs) ?
                    _.concat(ret[7].msgs, ret[2] && ret[2].msgs) : ret[2] && ret[2].msgs,
                hotTags: ret[3],
                ads: ret[4],
                exRecos: ret[5],

                guangIndexPage: true,
                pageSize: pageSize,
                type: type,
                pathNav: pathNav,
                baseUrl: urlHelper.listUrl(channel, type),
                page: page,
                total: (ret[2] && ret[2].total) || 0
            },
            module: 'guang',
            page: 'guang',
            headerData: ret[6] && ret[6].headerData
        }));
    }).catch(next);

};

/**
 * 指定标签的文章列表
 */
exports.tags = (req, res, next) => {

    let query = req.query.query = (req.query.query || '').replace(/<|>/ig, '');
    let page = req.query.page || 1;
    let pageSize = req.query.pageSize || 20;
    let type = req.query.type || 0;

    let uid = req.user.uid;
    let udid = ghelper.getUdid(req, res);
    let gender = ghelper.getGenderByCookie(req);
    let channel = req.yoho.channel;
    let pathNav = req.ctx(guangModel).getPathNav(channel, query);
    let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
    let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);

    const reqCtx = req.ctx(guangModel);

    Promise.all([
        reqCtx.getBanner(channel),

        reqCtx.getArticleList(gender, 0, uid, udid, page, query, '', pageSize, channel, true),
        reqCtx.getHotTags(1, 20, channel, isHotDegrade),
        reqCtx.getAds(channel, isAdDegrade),
        reqCtx.getRecoArticles(gender, 1, 10, channel),
        headerModel.requestHeaderData(channel)
    ]).then(ret => {

        if (_.isEmpty(ret[1]) || _.isEmpty(ret[1].msgs)) {
            res.set('Cache-Control', 'no-cache');
        }

        let seoTdk = ghelper.getTagSeo(query);

        res.render('guang/tag', Object.assign(seoTdk, {
            guang: {
                slider: ret[0],
                msgs: ret[1] && ret[1].msgs,
                hotTags: ret[2],
                ads: ret[3],
                exRecos: ret[4],

                tag: query,
                guangIndexPage: true,
                baseUrl: `?${querystring.stringify(req.query)}`,
                pageSize: pageSize,
                type: type,
                pathNav: pathNav,
                page: page,
                total: (ret[1] && ret[1].total) || 0
            },
            module: 'guang',
            page: 'list',
            headerData: ret[5] && ret[5].headerData
        }));
    }).catch(next);
};

/**
 * 作者文章列表
 */
exports.editor = (req, res, next) => {

    let authorId = req.query.author_id;
    let channel = req.yoho.channel;
    let gender = ghelper.getGenderByCookie(req);
    let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
    let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);

    let uid = req.user.uid;
    let udid = ghelper.getUdid(req, res);

    let pathNav = req.ctx(guangModel).getPathNav(channel);

    let page = req.query.page || 1;
    let pageSize = req.query.pageSize || 20;

    const reqCtx = req.ctx(guangModel);

    Promise.all([
        reqCtx.getAuthor(channel, authorId),
        reqCtx.getArticleList(gender, null, uid, udid, page, '', authorId, pageSize, channel, true),
        reqCtx.getHotTags(1, 20, channel, isHotDegrade),
        reqCtx.getAds(channel, isAdDegrade),
        reqCtx.getRecoArticles(gender, 1, 10, channel),
        headerModel.requestHeaderData(channel)
    ]).then(ret => {

        if (_.isEmpty(ret[1]) || _.isEmpty(ret[1].msgs)) {
            res.set('Cache-Control', 'no-cache');
        }

        let seoTdk = ghelper.getEditorSeo(_.get(ret[0], 'name', ''));

        res.render('guang/editor', Object.assign(seoTdk, {
            guang: {
                editor: ret[0],
                msgs: ret[1] && ret[1].msgs,
                hotTags: ret[2],
                ads: ret[3],
                exRecos: ret[4],

                gender: gender,
                baseUrl: urlHelper.editorUrl(channel, authorId),
                pageSize: pageSize,
                pathNav: pathNav,
                page: page,
                total: (ret[1] && ret[1].total) || 0
            },
            module: 'guang',
            page: 'list',
            headerData: ret[5] && ret[5].headerData
        }));
    }).catch(next);
};

/**
 * 文章详情
 */
exports.detail = (req, res, next) => {

    let id = req.params.id || req.query.id;
    let page = req.query.page || 1;
    let pageSize = req.query.pageSize || 10;
    let col = req.query.col || 0;
    let pjax = req.query._pjax;
    let uid = req.user.uid;
    let udid = ghelper.getUdid(req, res);
    let gender = ghelper.getGenderByCookie(req);
    let channel = req.yoho.channel;
    let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
    let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);

    const reqCtx = req.ctx(guangModel);

    if (!_.isNumber(id)) {
        id = _.parseInt(id);
    }

    if (pjax) {
        req.ctx(guangModel).getArticleComments(udid, uid, id, page, pageSize).then(ret => {

            res.set('Cache-Control', 'no-cache');
            res.render('guang/detail-comment', {
                layout: false,
                comment: ret
            }, function(err, html) {
                if (err) {
                    return res.send('');
                }
                return res.status(200).send(html);
            });
        });

        return;
    }

    // 登陆后自动收藏
    if (col === 1 && uid > 0) {
        reqCtx.collectArticle(id, uid);
    }

    reqCtx.getArticleInfo(id, channel).then(info => {

        // 判断参数是否有效, 无效会跳转到错误页面
        if (!info || !info.title) {
            return next();
        }

        let promises = [
            headerModel.requestHeaderData(channel),
            reqCtx.getArticleContent(id),
            reqCtx.getHotTags(1, 20, channel, isHotDegrade),
            reqCtx.getArticleComments(udid, uid, id, page, pageSize),
            reqCtx.getArticleBaseInfo(id, uid, udid),
            reqCtx.getArticleRelateBrand(id),
            reqCtx.getRecoArticles(gender, 1, 10, channel),
            reqCtx.getAds(channel, isAdDegrade)
        ];

        if (info.authorId) {
            promises.push(req.ctx(guangModel).getAuthor(channel, info.authorId));
        } else {
            promises.push({});
        }

        if (info.tag) {
            promises.push(req.ctx(guangModel).getRelateArticleList(id, info.tag, 2, channel));
        } else {
            promises.push({});
        }

        let pathNav = req.ctx(guangModel).getPathNav(channel, info.title);

        Promise.all(promises).then(ret => {

            if (_.isEmpty(ret[1])) {
                res.set('Cache-Control', 'no-cache');
            }

            res.render('guang/detail', Object.assign({
                module: 'guang',
                page: 'detail',
                headerData: ret[0] && ret[0].headerData,
                guang: {
                    pathNav: pathNav,

                    id: id,
                    header: Object.assign({}, info, ret[8] || {},
                        { commentNum: _.get(ret[3], 'commentNum', 0) }),
                    content: ret[1],
                    hotTags: ret[2],
                    comment: ret[3],
                    commentInfo: req.session['comment_' + udid],
                    userInfo: ret[4],
                    brands: ret[5],

                    tag: info.tags,
                    relatedPost: (ret[9] && ret[9].length) ? ret[9] : false,
                    exRecos: ret[6],
                    ads: ret[7],

                    // 分享
                    shareImg: info.shareImg,
                    sharedTitle: info.title,
                    shareDesc: info.desc,
                    weixinUrl: info.weixinUrl,
                    commentFirstPageUrl: '?pageSize=10'
                }
            }, {
                title: `${info.title} | YOHO!BUY有货`,
                keywords: info.tag.length > 0 ? `${info.tag}` :
                    ['Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,品牌服饰,男士护肤,',
                        '黑框眼镜,匡威,板鞋,i.t,izzue,5cm,eastpak,vans,lylescott,g-shock,new balance,lacoste,melissa,',
                        'casio,卡西欧手表,舒雅,jasonwood,odm,AAAA,香港购物,日本潮流'].join(''),
                description: info.desc.length > 0 ? `${info.desc}` :
                    '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜',
                webNavHeader: channel
            }));
        });
    }).catch(next);
};

/**
 * 列表页(列表首页、标签列表页、作者列表页)动态数据,如:查看数,点赞数,评论数,是否点赞,是否回复
 * @param req
 * @param res
 */
exports.listDynamicData = (req, res) => {
    let ids = req.query.ids;

    let udid = ghelper.getUdid(req, res);

    let other = {};
    let query = req.query.query,
        type = req.query.type;

    if (req.user.uid) {
        other.uid = req.user.uid;
    }

    if (query) {
        other.query = query;
    }

    if (type) {
        other.type = type;
    }

    req.ctx(guangModel).getDynamicDataByIds(ids, udid, other).then(ret => {
        res.send(ret);
    });
};

/**
 * 详情页动态数据,如:评论数,回复数,是否点赞,是否收藏
 * @param req
 * @param res
 */
exports.detailDynamicData = (req, res) => {
    let id = req.query.id,
        uid = req.user.uid,
        udid = ghelper.getUdid(req, res);

    req.ctx(guangModel).getDynamicDataById(id, uid, udid).then((ret) => {
        res.status(200).send(ret);
    }).catch(() => {
        res.status(400);
    });
};

/**
 * 添加评论
 */
exports.comment = (req, res) => {

    let id = req.body.id || req.query.id;
    let uid = req.user.uid;
    let udid = ghelper.getUdid(req, res);
    let comment = req.body.comment;

    // let pageSize = req.query.pageSize || 20;

    if (!uid) {
        req.session['comment_' + udid] = comment;
        return res.send({
            code: 401
        });
    }

    req.ctx(guangModel).addComment(id, uid, comment).then(() => {
        if (req.session['comment_' + udid]) {
            req.session['comment_' + udid] = null;
        }

        return req.ctx(guangModel).getArticleComments(udid, uid, id, 1, 20);
    }).then(commentInfo => {

        res.render('guang/detail-comment', {
            comment: commentInfo,
            layout: false
        }, (err, content) => {

            if (err) {

                return res.send({
                    code: 401
                });
            }
            return res.send({
                code: 200,
                data: {
                    count: commentInfo.commentNum,
                    content: content
                }
            });
        });
    }).catch(() => {
        return res.send({
            code: 400,
            message: '评论失败'
        });
    });
};

/**
 * 赞
 */
exports.praise = (req, res) => {

    let id = req.body.id || req.query.id;

    // let uid = req.user.uid;
    let udid = ghelper.getUdid(req, res);

    req.ctx(guangModel).praise(id, udid).then(ret => {
        res.send(ret);
    }).catch(() => {
        res.send({
            code: 400
        });
    });
};

/**
 * 取消赞
 */
exports.cancelPraise = (req, res) =>{
    let id = req.body.id || req.query.id;
    let udid = ghelper.getUdid(req, res);

    req.ctx(guangModel).cancelPraise(id, udid).then(ret => {
        res.send(ret);
    }).catch(() => {
        res.send({
            code: 400
        });
    });
};

/**
 * 收藏
 */
exports.collect = (req, res) =>{
    let id = req.body.id || req.query.id;
    let uid = req.user.uid;

    if (!uid) {
        return res.send({
            code: 401
        });
    }

    req.ctx(guangModel).collect(id, uid).then(ret => {
        res.send(ret);
    }).catch(() => {
        res.send({
            code: 400,
            message: '收藏失败'
        });
    });
};

/**
 * 取消收藏
 */
exports.cancelCollect = (req, res) =>{
    let id = req.body.id || req.query.id;
    let uid = req.user.uid;

    if (!uid) {
        return res.send({
            code: 401
        });
    }

    req.ctx(guangModel).cancelCollect(id, uid).then(ret => {
        res.send(ret);
    }).catch(() => {
        res.send({
            code: 400,
            message: '取消收藏失败'
        });
    });
};