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

/**
 * 个人中心主页
 */
const component = {
    index: (req, res) => {
        res.render('mindex', {
            module: 'me',
            page: 'index'
        });
    },
    userdata: (req, res, next) => {
        const uid = req.user.uid;

        homeModel.getUserHomeData(uid).then(data => {
            const proData = data[0];
            const result = {
                headIco: proData && proData.head_ico ? helpers.image(proData.head_ico, 170, 170) : '',
                nickName: Object.keys(proData).length ? proData.nickname : '登录/注册'
            };

            return res.json(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) => {
        const params = {
            uid: req.user.uid
        };

        if (req.body.nickname) {
            params.nick_name = req.body.nickname;
        }
        if (req.body.gender) {
            params.gender = req.body.gender;
        }
        if (req.body.birthday) {
            params.birthday = req.body.birthday;
        }

        homeModel.saveMydetails(params).then(data => {
            return res.json(data);
        }).catch(next);
    },
    service: (req, res) => {
        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',
            noLocalCSS: true
        });
    },
    saveFeedback: (req, res, next) => {
        const 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: 'about',
            noLocalCSS: true
        });
    },
    provisions: (req, res) => {
        res.render('provisions', {
            module: 'me',
            page: 'provisions',
            noLocalJS: true,
            noLocalCSS: true
        });
    },
    agreement: (req, res) => {
        res.render('agreement', {
            module: 'me',
            page: 'agreement',
            noLocalJS: true,
            noLocalCSS: true
        });
    },
    records: (req, res)=>{
        res.render('records', {
            module: 'me',
            page: 'records'
        });
    },
    recordData: (req, res, next) => {
        let browserSkn = req.cookies._browseskn_h5;
        let limit = req.query.limit;

        // 拆解skn
        let skn = browserSkn ? decodeURIComponent(browserSkn).replace(/\-(\d)+(\,){0,1}/g, ',') : '';

        // 去除skn字符串的最后一个多余的,
        if (skn && skn.lastIndexOf(',') === skn.length - 1) {
            skn = skn.slice(0, -1);
        }

        if (!skn) {
            res.json({
                code: 200,
                data: [],
                message: 'User info'
            });
        } else {
            skn = _.slice(_.uniq(skn.split(',')), 0, limit).join(','); // 去重+截取

            homeModel.recentPreview(skn, limit).then(data => {
                res.json(data);
            }).catch(next);
        }
    },
    recordDataNew: (req, res, next) => {
        if (!req.user.uid) {
            res.json({
                code: 200,
                data: [],
                message: 'not login'
            });
        } else {
            homeModel.recentPreviewNew(req.user.uid, 20).then(result => {
                if (result.data && result.data.product_list) {
                    let sknarr = result.data.product_list.map(item=>item.product_skn);

                    return homeModel.getSknUrl(sknarr, result);
                } else {
                    return result;
                }

            }).then(result=>{
                res.json(result);
            }).catch(next);
        }

    },
    deleteRecordData(req, res, next) {
        if (!req.user.uid) {
            res.json({
                code: 200,
                data: [],
                message: 'not login'
            });
        } else {
            homeModel.deleteRecordData(req.user.uid, req.query.skn).then(result => {
                res.json(result);
            }).catch(next);
        }
    },
    recordNum(req, res, next) {
        if (!req.user.uid) {
            res.json({
                code: 200,
                data: [],
                message: 'not login'
            });
        } else {
            homeModel.recordNum(req.user.uid, req.sessionID).then(result => {
                res.json(result);
            }).catch(next);
        }
    },
    addRecords(req, res, next) {
        let browserSkn = req.cookies._browseskn_h5;

        if (!req.user.uid) {
            res.json({
                code: 200,
                data: [],
                message: 'not login'
            });
        } else {
            homeModel.addRecords(req.user.uid, req.sessionID, browserSkn).then(result => {
                res.cookie('_browseskn_h5', '', {
                    maxAge: 0,
                    domain: '.yohoblk.com'
                });
                res.json(result);
            }).catch(next);
        }
    }
};

module.exports = component;