123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- const express = require('express');
- const router = express.Router();
- const crypto = require('crypto');
- const events = require('events');
- const db = require('../common/mysqlConnection');
- /**
- * 获取用户列表
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function getUserList(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter(),
- pageIndex = req.query.pageIndex - 1,
- orderBy = req.query.orderBy,
- searchValue = req.query.search;
- emitter.on('ok', (results, count) => {
- res.send({
- status: 'success',
- info: '获取成功',
- data: {
- 'results': results,
- 'count': count
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('selectUserInfoFromUserTable', () => {
- switch (orderBy) {
- case 'userId':
- orderBy = 'uuid';
- break;
- case 'userNickName':
- orderBy = 'account';
- break;
- case 'createDate':
- orderBy = 'createDate';
- break;
- default:
- orderBy = 'uuid';
- break;
- }
- if (pageIndex > 0) {
- pageIndex = parseInt(pageIndex, 10) * 20;
- } else {
- pageIndex = 0;
- }
-
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT account, uuid, dbpassword, createDate FROM user_test ORDER BY ? ASC) a LIMIT ?, 20; SELECT FOUND_ROWS()',
- value = [orderBy, pageIndex];
- if (searchValue) {
- sqlString = 'SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT account, uuid, dbpassword, createDate FROM user_test WHERE account LIKE ? OR uuid LIKE ? OR createDate LIKE ? ORDER BY ? ASC) a LIMIT ?, 20; SELECT FOUND_ROWS()';
- searchValue = '%' + searchValue + '%';
- value = [searchValue, searchValue, searchValue, orderBy, pageIndex];
- }
-
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '数据库查询出错');
- } else {
- emitter.emit('ok', results[0], results[1][0]['FOUND_ROWS()']);
- }
- });
- });
- emitter.emit('selectUserInfoFromUserTable');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- /**
- * 获取用户总数
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function getUserCount(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter();
- emitter.on('ok', (results) => {
- res.send({
- status: 'success',
- info: '获取成功',
- data: {
- 'results': results
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('selectUserCountFromUserTable', () => {
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'SELECT COUNT(1) FROM user_test',
- value = null;
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '数据库查询出错');
- } else {
- emitter.emit('ok', results[0]['COUNT(1)']);
- }
- });
- });
- emitter.emit('selectUserCountFromUserTable');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- /**
- * 获取用户拥有的数据库
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function getUserOwnDb(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter(),
- uuid = req.query.userId;
- emitter.on('ok', (results) => {
- res.send({
- status: 'success',
- info: '获取成功',
- data: {
- 'results': results
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('selectDbOwnInfoFromDbOwnTable', () => {
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'SELECT dbName FROM dbOwn_test WHERE dbOwnUuid = ?',
- value = [uuid];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '数据库查询出错');
- } else {
- emitter.emit('ok', results);
- }
- });
- });
- emitter.emit('selectDbOwnInfoFromDbOwnTable');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- /**
- * 删除用户
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function removeUser(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter(),
- uuid = req.body.userId,
- userNickName = req.body.userNickName;
- emitter.on('ok', (results) => {
- res.send({
- status: 'success',
- info: '删除成功'
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('deleteFromDbOwnTable', () => {
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'DELETE FROM dbOwn_test WHERE dbOwnUuid = ?',
- value = [userNickName];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '删除用户信息表记录时出错');
- } else {
- emitter.emit('ok');
- }
- });
- });
- emitter.on('deleteFromUserTable', () => {
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'DELETE FROM user_test WHERE uuid = ?',
- value = [uuid];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '用户数据表删除数据出错');
- } else {
- emitter.emit('deleteFromDbOwnTable');
- }
- });
- });
- emitter.on('dropUser', () => {
- //定义SQL语句查询
- let pool = db.pool.userDatabasePool,
- sqlString = 'USE mysql; DELETE FROM user WHERE User = ?',
- value = [uuid];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '用户数据表删除数据出错');
- } else {
- emitter.emit('deleteFromUserTable');
- }
- });
- });
- emitter.on('dropDatabase', (results) => {
- let pool = db.pool.userDatabasePool,
- value = null;
- results.forEach((item, index) => {
- let sqlString = 'DROP DATABASE ' + item.dbName;
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '删除数据库时出错');
- } else {
- emitter.emit('dropUser');
- }
- });
- }, this);
- });
- emitter.on('selectDbOwnInfoFromDbOwnTable', () => {
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'SELECT dbName FROM dbOwn_test WHERE dbOwnUuid = ?',
- value = [uuid];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '数据库查询出错');
- } else {
- emitter.emit('dropDatabase', results);
- }
- });
- });
- emitter.emit('selectDbOwnInfoFromDbOwnTable');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- router.get('/', getUserList);
- router.get('/getUserCount', getUserCount);
- router.get('/getUserOwnDb', getUserOwnDb);
- router.post('/removeUser', removeUser);
- module.exports = router
|