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

// const model_order = require('../models/order')

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

        uid = 8050378; // 测试uid

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

        homeModel.getUserHomeData(uid).then(data => {
            const proData = data[0];
            const result = {
                module: 'home',
                page: 'index',
                head_ico: proData && proData.head_ico ? helpers.image(proData.head_ico, 200, 200) : '',
                profile_name: uid && proData ? proData.profile_name : '登录/注册',
                userinfourl: uid ? '/home/mydetails' : helpers.urlFormat('/signin.html', {
                    refer: req.originalUrl
                }),
            };

            res.render('index', _.merge(result, data[1]));
        }).catch(next);
    },
    help: (req, res, next) => {
        homeModel.getHelpInfo().then(helpList => {
            res.render('help', {
                module: 'home',
                page: 'help',
                helpList: helpList
            });
        }).catch(next);
    },
    helpDetail: (req, res, next) => {
        let helpDetailPara = {
            code: req.query.code,
            caption: req.query.caption
        };

        homeModel.getHelpDetail(helpDetailPara).then(helpDetail => {
            res.render('help-detail', {
                module: 'home',
                page: 'help',
                helpDetail: helpDetail
            });
        }).catch(next);
    },
    feedback: (req, res, next) => {
        res.render('feedback', {
            module: 'home',
            page: 'feedback',
            suggestSub: true,
        }).catch(next);
    },
    saveFeedback: (req, res, next) => {
        let saveFeedbackPara = {
            uid: '13549737',
            content: req.body.content,
            suggest_type: 2
        };

        if (req.user && req.user.uid) {
            _.merge(saveFeedbackPara, {
                uid: req.user.uid
            });
        }

        if (req.user && req.user.udid && !req.user.uid) {
            _.merge(saveFeedbackPara, {
                udid: req.user.udid
            });
        }

        homeModel.saveFeedback(saveFeedbackPara).then(result => {
            console.log(result);

            if (result.code === 200) {
                return res.json({
                    code: 200,
                    message: '谢谢您的反馈'
                });
            } else {
                return res.json({
                    code: 400,
                    message: '保存出错'
                });
            }
        }).catch(next);
    },

    // 关于我们
    aboutUs: (req, res) => {
        res.render('about-us', {
            module: 'home',
            page: 'index'
        });
    },
    order: (req, res) => {
        let type = req.query.type;

        res.render('order', {
            module: 'home',
            page: 'order',
            type: type
        });
    },
    orderDetail: (req, res) => {
        let orderCode = req.query.ordercode;

        res.render('order-detail', {
            module: 'home',
            page: 'order',
            orderCode: orderCode
        });
    },
    getOrderData: (req, res) => {
        // let param = Object.assign({uid: req.user.uid}, req.query);
        res.json({});
    },
    coin: (req, res) => {
        res.render('coin', {
            module: 'home',
            page: 'order'
        });
    }
};

module.exports = component;