controller.js 1.81 KB
const _ = require('lodash');
const { login, account } = require('./model');

const config = global.config;

const loginByPassword = (req, res, next) => {
    login.password(req.body).then(result => {
        if (result.access) {
            req.session.user = {
                name: result.username
            };
        }

        res.json({
            code: 200,
            data: {
                access: result.access
            },
            message: result.message
        });
    }).catch(next)
};

const checkAdmin = (req, res, next) => {
    let account = config.rootAccount || {};

    if (account.username !== _.get(req.session, 'user.name')) {
        return res.json({
            code: 400,
            message: '没有访问权限,请联系管理员开通'
        });;
    }

    return next();
};

const accountList = (req, res, next) => {
    account.list().then(list => {
        return res.json({
            code: 200,
            data: { list },
            message: ''
        });
    }).catch(next);
};

const accountAdd = (req, res, next) => {
    account.add(req.body.account, _.get(req.session, 'user.name')).then(result => {
        if (result.err) {
            return res.json({
                code: 400,
                message: err
            });
        }

        if (req.body.needList) {
            accountList(req, res, next);
        } else {
            return res.json({
                code: 200,
                message: '添加成功'
            });
        }
    }).catch(next);
};

const accountDelete = (req, res, next) => {
    account.delete(req.body.id).then(() => {
        return res.json({
            code: 200,
            message: '删除成功'
        });
    }).catch(next);
};

module.exports = {
    loginByPassword,
    checkAdmin,
    accountList,
    accountAdd,
    accountDelete
};