user-controller.js 4.06 KB
/**
 * 用户controller
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2017/04/13
 */
'use strict';

const Context = require('../framework/context');
const UserService = require('../service/user-service');
const _ = require('lodash');

class UserController extends Context {
    constructor() {
        super();
        this.userService = this.instance(UserService);
    }
    login(req, res, next) {
        Promise.all([
            this.userService.login(req.body.username, req.body.password),
            this.userService.shopLogin(req.body.username, req.body.password)]).then(allResult => {
                let user = allResult[0];
                let sess = allResult[1];

                this.userService.getShops(user.pid).then(result => {
                    if (result.code === 200) {
                        let currentShop = _.first(result.data);

                        this.userService.switchShop({
                            shopId: currentShop.shopsId,
                            cookies: sess
                        }).then(shopSess => {
                            this.syncSession({req, res}, Object.assign(user, {
                                shops: result.data
                            }), shopSess, currentShop);

                            return res.json({
                                code: 200,
                                data: {
                                    name: user.account,
                                    email: user.email,
                                    createDate: user.create_date,
                                    shops: result.data,
                                    currentShop: currentShop
                                }
                            });
                        });
                    } else {
                        return res.json(result);
                    }
                });
            }, err => {
                return res.json(err);
            }).catch(next);
    }

    logout(req, res) {
        delete req.session.USER;
        delete req.session.LOGIN_UID;
        delete req.session.CURRENT_SHOP;
        res.clearCookie('PHPSESSID', {
            domain: '.yohobuy.com'
        });
        res.clearCookie('connect.sid', {
            domain: '.yohobuy.com'
        });
        res.clearCookie('_isLogin');
        res.clearCookie('_sign');
        res.clearCookie('yoho-shop');
        res.clearCookie('yoho-shop.sig');
        return res.json({
            code: 200,
            data: '登出成功'
        });
    }
    switchShop(req, res) {
        let shopId = req.body.shopId;

        if (!shopId) {
            return res.json({
                code: 400,
                message: '参数错误'
            });
        }
        let shop = _.find(req.session.USER.shops, s => s.shopsId === shopId);

        if (!shop) {
            return res.json({
                code: 400,
                message: '不存在的店铺'
            });
        }
        this.userService.switchShop({
            shopId,
            cookies: {
                PHPSESSID: encodeURIComponent(req.cookies.PHPSESSID),
                'connect.sid': encodeURIComponent(req.cookies['connect.sid'])
            }
        }).then(response => {
            this.syncShopSession({
                req,
                res
            }, response);
            return res.json({
                code: 200
            });
        });
    }

    syncSession(context, user, sess, currentShop) {
        context.req.session.USER = user;
        context.req.session.LOGIN_UID = user.pid; // pid 为用户名

        this.syncShopSession(context, sess);
        context.res.cookie('_isLogin', true, {
            path: '/'
        });
        context.res.cookie('_sign', currentShop.shopsId, {
            path: '/'
        });
    }

    syncShopSession(context, sess) {
        _.each(sess, (v, k) => {
            context.res.cookie(k, v, {
                path: '/',
                domain: '.yohobuy.com',
                httpOnly: true,
                encode: val => val
            });
        });
    }
}

module.exports = UserController;