user.js 3.57 KB
/**
 * 用户管理controller
 * @author: leo <qi.li@yoho.cn>
 * @date: 23/06/2017
 */
const _ = require('lodash');
const moment = require('moment');
const excelExport = require('excel-export');
const UserModel = require('../models/user');

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
     * @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
     */
    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
     */
    exportUserList(req, res, next) {
        let conf = {
            name: 'mysheet',
            cols: [
                {
                    caption: '用户ID',
                    type: 'number'
                },
                {
                    caption: '昵称',
                    type: 'string'
                },
                {
                    caption: '手机号',
                    type: 'string'
                },
                {
                    caption: '创建时间',
                    type: 'string'
                }
            ],
            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);
                });

                let exportFile = excelExport.execute(conf);

                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader('Content-Disposition', 'attachment; filename=userList.xlsx');
                res.end(exportFile, 'binary');
            })
            .catch(next);
    }
};


module.exports = userController;