index.js 4.28 KB
/**
 * 个人中心主页
 * @author: shenzm<zhimin.shen@yoho.cn>
 * @date: 2016/07/18
 */
'use strict';
const homeModel = require('../models/index');
const helpers = global.yoho.helpers;

/**
 * 个人中心主页
 */
const component = {
    index: (req, res, next) => {
        const uid = req.user.uid;

        if (!uid && req.xhr) {
            return res.json({
                code: 400,
                message: '抱歉,您暂未登录!'
            });
        }

        homeModel.getUserHomeData(uid).then(data => {
            const proData = data[0];
            const result = {
                module: 'me',
                page: 'index',
                noLocalCSS: true,
                head_ico: proData && proData.head_ico ? helpers.image(proData.head_ico, 200, 200) : '',
                nickname: proData ? proData.nickname : '登录/注册'
            };

            res.render('index', Object.assign(result, data[1]));
        }).catch(next);
    },
    mydetails: (req, res, next) => {
        const uid = req.user.uid;

        homeModel.getUserProfileData(uid).then(data => {
            var gender = '';

            data = data || {};

            if (data.gender === '1') {
                gender = 'men';
            } else if (data.gender === '2') {
                gender = 'women';
            }

            res.render('mydetails', {
                module: 'me',
                page: 'mydetails',
                head_ico: data.head_ico ? helpers.image(data.head_ico, 92, 92) : '',
                nickname: data.nickname,
                gender: gender,
                birthday: data.birthday
            });
        }).catch(next);
    },
    saveMydetails: (req, res, next) => {
        var params = {
            uid: req.user.uid
        };

        if (req.body.nickname !== undefined) {
            params.nick_name = req.body.nickname;
        }

        if (req.body.gender !== undefined) {
            params.gender = req.body.gender;
        }

        if (req.body.birthday !== undefined) {
            params.birthday = req.body.birthday;
        }

        homeModel.saveMydetails(params).then(data => {
            return res.json(data);
        }).catch(next);
    },
    service: (req, res, next) => {
        res.redirect('http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=703953&configID=149819&jid=1099911094');
    },
    help: (req, res, next) => {
        if (req.query.hasOwnProperty('id')) {
            let helpDetailPara = {
                code: req.query.code,
                caption: req.query.caption
            };

            homeModel.getHelpDetail(helpDetailPara).then(helpDetail => {
                res.render('help-detail', {
                    module: 'me',
                    page: 'help',
                    noLocalJS: true,
                    noLocalCSS: true,
                    helpDetail: helpDetail
                });
            }).catch(next);
        } else {
            homeModel.getHelpInfo().then(helpList => {
                res.render('help', {
                    module: 'me',
                    page: 'help',
                    noLocalJS: true,
                    noLocalCSS: true,
                    helpList: helpList
                });
            }).catch(next);
        }

    },
    feedback: (req, res) => {
        res.render('feedback', {
            module: 'me',
            page: 'feedback',
            suggestSub: true,
            noLocalCSS: true
        });
    },
    saveFeedback: (req, res, next) => {
        let saveFeedbackPara = {
            uid: req.user.uid,
            udid: req.sessionID,
            content: req.body.content,
            suggest_type: 2
        };

        homeModel.saveFeedback(saveFeedbackPara).then(result => {
            if (result.code === 200) {
                return res.json({
                    code: 200,
                    message: '谢谢您的反馈'
                });
            } else {
                return res.json({
                    code: 400,
                    message: '保存出错'
                });
            }
        }).catch(next);
    },
    about: (req, res) => {
        res.render('about', {
            module: 'me',
            page: 'index',
            noLocalJS: true,
            noLocalCSS: true
        });
    }
};

module.exports = component;