user.js 10.1 KB
/**
 * 用户管理controller
 * @author: leo <qi.li@yoho.cn>
 * @date: 23/06/2017
 */
const _ = require('lodash');
const moment = require('moment');
const UserModel = require('../models/user');
const camelcase = require('camelcase');
const {excel_export} = require('../../../utils/excel');

const DO_SUCCESS = '操作成功';
const GET_SUCCESS = '获取成功';

const timeFmt = (time) => {
    if (_.isNumber(time)) {
        time = moment.unix(time);
    }

    return moment(time).format('YYYY-MM-DD HH:mm:ss');
};

const userController = {
    /**
     * 用户列表页
     * @param req
     * @param res
     */
    userListPage(req, res) {

        res.render('user/list', {
            bodyClass: 'nav-md',
            module: 'admin',
            page: 'user'
        });
    },

    /**
     * 用户信息收集列表
     * @param req
     * @param res
     */
    usersListPage(req, res) {
        res.render('user/users-list', {
            bodyClass: 'nav-md',
            module: 'admin',
            page: 'users-list'
        });
    },

    prizeUsersListPage(req, res) {
        res.render('user/prize-users-list', {
            bodyClass: 'nav-md',
            module: 'admin',
            page: 'prize-users-list'
        });
    },

    /**
     * 用户列表
     * @param req
     * @param res
     * @param next
     */
    userList(req, res, next) {
        const pageNo = req.query.pageNo || 1;
        const pageSize = req.query.pageSize || 20;

        req.ctx(UserModel).userList({
            pageNo,
            pageSize
        })
            .then(list => {

                _.each(list, item => {
                    item.createTime = timeFmt(item.createTime);
                });

                return list;
            })
            .then(list => {
                req.ctx(UserModel).allUsersNum()
                    .then(totalCount => {
                        res.json({
                            code: 200,
                            data: list,
                            pageNo: +pageNo,
                            pageSize: +pageSize,
                            totalCount,
                            totalPage: Math.ceil(totalCount / pageSize),
                            message: GET_SUCCESS
                        });
                    });
            })
            .catch(next);
    },

    /**
     * 用户信息收集列表
     * @param req
     * @param res
     * @param next
     */
    usersList(req, res, next) {
        const pageNo = req.query.pageNo || 1;
        const pageSize = req.query.pageSize || 20;

        req.ctx(UserModel).usersList(pageNo, pageSize).then(list => {

            let result = [];

            _.each(list, item => {
                let data = {};

                item.user_birthday = timeFmt(item.user_birthday);
                _.each(item, (val, key) => {
                    data[camelcase(key)] = val;
                });
                result.push(data);
            });
            return result;
        }).then(list => {
            req.ctx(UserModel).allInfoUsersNum().then(totalCount => {
                res.json({
                    code: 200,
                    data: list,
                    pageNo: +pageNo,
                    pageSize: +pageSize,
                    totalCount,
                    totalPage: Math.ceil(totalCount / pageSize),
                    message: GET_SUCCESS
                });
            });
        }).catch(next);
    },

    /**
     * 中奖用户列表
     * @param req
     * @param res
     * @param next
     */
    prizeUsersList(req, res, next) {
        const pageNo = req.query.pageNo || 1;
        const pageSize = req.query.pageSize || 20;
        const actId = req.query.actId || 25;

        req.ctx(UserModel).prizeUsersList(pageNo, pageSize, actId).then(list => {

            let result = [];

            _.each(list, item => {
                let data = {};

                _.each(item, (val, key) => {
                    data[camelcase(key)] = val;
                });
                result.push(data);
            });
            return result;
        }).then(list => {
            req.ctx(UserModel).allPrizeUsersNum(actId).then(totalCount => {
                res.json({
                    code: 200,
                    data: list,
                    pageNo: +pageNo,
                    pageSize: +pageSize,
                    totalCount,
                    totalPage: Math.ceil(totalCount / pageSize),
                    message: GET_SUCCESS
                });
            });
        }).catch(next);
    },

    /**
     * 用户删除
     * @param req
     * @param res
     * @param next
     */
    deleteUser(req, res, next) {
        const userId = req.body.userId;

        req.ctx(UserModel).deleteUser(userId)
            .then(() => {
                res.json({
                    code: 200,
                    message: DO_SUCCESS
                });
            })
            .catch(next);
    },

    /**
     * 用户信息收集用户删除
     * @param req
     * @param res
     * @param next
     */
    deleteInfoUsers(req, res, next) {
        const userId = req.body.userId;

        req.ctx(UserModel).deleteInfoUser(userId).then(() => {
            res.json({
                code: 200,
                message: DO_SUCCESS
            });
        }).catch(next);
    },

    /**
     * 抽奖用户删除
     * @param req
     * @param res
     * @param next
     */
    deletePrizeUsers(req, res, next) {
        const userId = req.body.userId;

        req.ctx(UserModel).deletePrizeUser(userId).then(() => {
            res.json({
                code: 200,
                message: DO_SUCCESS
            });
        }).catch(next);
    },

    /**
     * 用户列表导出
     * @param req
     * @param res
     * @param next
     */
    exportUserList(req, res, next) {
        let conf = {
            name: 'mysheet',
            cols: ['用户ID', '昵称', '手机号', '创建时间'],
            rows: []
        };

        req.ctx(UserModel).exportUserList()
            .then(result => {
                let temp = [];

                _.each(result, item => {
                    temp = [];
                    temp.push(item.id);
                    temp.push(item.userName);
                    temp.push(item.userPhone);
                    temp.push(timeFmt(item.createTime));
                    conf.rows.push(temp);
                });

                return excel_export({
                    title: conf.cols,
                    data: conf.rows,
                    fileName: 'userList',
                    sheetName: conf.name,
                    res
                });
            })
            .catch(next);
    },
    exportInfoUserList(req, res, next) {
        let conf = {
            name: 'myInfoSheet',
            cols: [
                '用户ID',
                '昵称',
                '生日',
                '性别',
                '手机号',
                'email',
                '省份',
                '城市'],
            rows: []
        };

        req.ctx(UserModel).exportInfoUserList().then(result => {
            let temp = [];

            _.each(result, item => {
                temp = [];

                temp.push(item.id);
                temp.push(item.user_name);
                temp.push(moment(item.user_birthday).format('YYYY-MM-DD'));
                temp.push(['男', '女'][item.user_gender]);
                temp.push(item.user_phone);
                temp.push(item.user_email);
                temp.push(item.user_province);
                temp.push(item.user_city);
                conf.rows.push(temp);
            });

            return excel_export({
                title: conf.cols,
                data: conf.rows,
                fileName: 'userInfoList',
                sheetName: conf.name,
                res
            });
        }).catch(next);
    },
    exportPrizeUserList(req, res, next) {
        let conf = {
            name: 'myInfoSheet',
            cols: [
                '用户ID',
                '昵称',
                '手机号',
                '地址',
                '鞋码'],
            rows: []
        };

        req.ctx(UserModel).exportPrizeUserList().then(result => {
            let temp = [];

            _.each(result, item => {
                temp = [];

                temp.push(item.id);
                temp.push(item.user_name);
                temp.push(item.user_phone);
                temp.push(item.user_address);
                temp.push(item.user_shoes_size);
                conf.rows.push(temp);
            });

            return excel_export({
                title: conf.cols,
                data: conf.rows,
                fileName: 'prizeUserList',
                sheetName: conf.name,
                res
            });
        }).catch(next);
    },
    userLoginLog(req, res) {
        res.render('user/login-log', {
            bodyClass: 'nav-md',
            module: 'admin',
            page: 'user-login-log'
        });
    },
    exportUserLoginLog(req, res, next) {
        const url = req.query.url;
        const startTime = req.query.startTime;
        const endTime = req.query.endTime;

        let conf = {
            name: 'mysheet',
            cols: ['用户id', '登录时间', '平台', '地址', 'ip'],
            rows: []
        };

        req.ctx(UserModel).exportUserLoginLog({url, startTime, endTime})
            .then(result => {
                let temp = [];

                _.each(result, item => {
                    temp = [];
                    temp.push(item.user_id);
                    temp.push(moment(item.login_time).format('YYYY-MM-DD HH:mm:ss'));
                    temp.push(item.platform);
                    temp.push(item.referer);
                    temp.push(item.ip);
                    conf.rows.push(temp);
                });

                return excel_export({
                    title: conf.cols,
                    data: conf.rows,
                    fileName: 'user-login-log',
                    sheetName: conf.name,
                    res
                });
            })
            .catch(next);
    }
};


module.exports = userController;