userList.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. const express = require('express');
  2. const router = express.Router();
  3. const crypto = require('crypto');
  4. const events = require('events');
  5. const db = require('../common/mysqlConnection');
  6. /**
  7. * 获取用户列表
  8. * @param {*} req
  9. * @param {*} res
  10. * @param {function} next
  11. */
  12. function getUserList(req, res, next) {
  13. if (req.session.signinStatus) {
  14. let emitter = new events.EventEmitter(),
  15. pageIndex = req.query.pageIndex - 1,
  16. orderBy = req.query.orderBy,
  17. searchValue = req.query.search;
  18. emitter.on('ok', (results, count) => {
  19. res.send({
  20. status: 'success',
  21. info: '获取成功',
  22. data: {
  23. 'results': results,
  24. 'count': count
  25. }
  26. });
  27. });
  28. emitter.on('err', (err, errInfo) => {
  29. res.json({
  30. status: 'error',
  31. info: errInfo,
  32. error: err
  33. });
  34. });
  35. emitter.on('selectUserInfoFromUserTable', () => {
  36. switch (orderBy) {
  37. case 'userId':
  38. orderBy = 'uuid';
  39. break;
  40. case 'userNickName':
  41. orderBy = 'account';
  42. break;
  43. case 'createDate':
  44. orderBy = 'createDate';
  45. break;
  46. default:
  47. orderBy = 'uuid';
  48. break;
  49. }
  50. if (pageIndex > 0) {
  51. pageIndex = parseInt(pageIndex, 10) * 20;
  52. } else {
  53. pageIndex = 0;
  54. }
  55. //定义SQL语句查询
  56. let pool = db.pool.userInfoDatabasePool,
  57. sqlString = 'SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT account, uuid, dbpassword, createDate FROM user_test ORDER BY ? ASC) a LIMIT ?, 20; SELECT FOUND_ROWS()',
  58. value = [orderBy, pageIndex];
  59. if (searchValue) {
  60. 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()';
  61. searchValue = '%' + searchValue + '%';
  62. value = [searchValue, searchValue, searchValue, orderBy, pageIndex];
  63. }
  64. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  65. if (err) {
  66. emitter.emit('err', err.err, '数据库查询出错');
  67. } else {
  68. emitter.emit('ok', results[0], results[1][0]['FOUND_ROWS()']);
  69. }
  70. });
  71. });
  72. emitter.emit('selectUserInfoFromUserTable');
  73. } else {
  74. res.send({
  75. status: 'signout',
  76. info: '未登录',
  77. url: '#/signin'
  78. });
  79. }
  80. }
  81. /**
  82. * 获取用户总数
  83. * @param {*} req
  84. * @param {*} res
  85. * @param {function} next
  86. */
  87. function getUserCount(req, res, next) {
  88. if (req.session.signinStatus) {
  89. let emitter = new events.EventEmitter();
  90. emitter.on('ok', (results) => {
  91. res.send({
  92. status: 'success',
  93. info: '获取成功',
  94. data: {
  95. 'results': results
  96. }
  97. });
  98. });
  99. emitter.on('err', (err, errInfo) => {
  100. res.json({
  101. status: 'error',
  102. info: errInfo,
  103. error: err
  104. });
  105. });
  106. emitter.on('selectUserCountFromUserTable', () => {
  107. //定义SQL语句查询
  108. let pool = db.pool.userInfoDatabasePool,
  109. sqlString = 'SELECT COUNT(1) FROM user_test',
  110. value = null;
  111. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  112. if (err) {
  113. emitter.emit('err', err.err, '数据库查询出错');
  114. } else {
  115. emitter.emit('ok', results[0]['COUNT(1)']);
  116. }
  117. });
  118. });
  119. emitter.emit('selectUserCountFromUserTable');
  120. } else {
  121. res.send({
  122. status: 'signout',
  123. info: '未登录',
  124. url: '#/signin'
  125. });
  126. }
  127. }
  128. /**
  129. * 获取用户拥有的数据库
  130. * @param {*} req
  131. * @param {*} res
  132. * @param {function} next
  133. */
  134. function getUserOwnDb(req, res, next) {
  135. if (req.session.signinStatus) {
  136. let emitter = new events.EventEmitter(),
  137. uuid = req.query.userId;
  138. emitter.on('ok', (results) => {
  139. res.send({
  140. status: 'success',
  141. info: '获取成功',
  142. data: {
  143. 'results': results
  144. }
  145. });
  146. });
  147. emitter.on('err', (err, errInfo) => {
  148. res.json({
  149. status: 'error',
  150. info: errInfo,
  151. error: err
  152. });
  153. });
  154. emitter.on('selectDbOwnInfoFromDbOwnTable', () => {
  155. //定义SQL语句查询
  156. let pool = db.pool.userInfoDatabasePool,
  157. sqlString = 'SELECT dbName FROM dbOwn_test WHERE dbOwnUuid = ?',
  158. value = [uuid];
  159. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  160. if (err) {
  161. emitter.emit('err', err.err, '数据库查询出错');
  162. } else {
  163. emitter.emit('ok', results);
  164. }
  165. });
  166. });
  167. emitter.emit('selectDbOwnInfoFromDbOwnTable');
  168. } else {
  169. res.send({
  170. status: 'signout',
  171. info: '未登录',
  172. url: '#/signin'
  173. });
  174. }
  175. }
  176. /**
  177. * 删除用户
  178. * @param {*} req
  179. * @param {*} res
  180. * @param {function} next
  181. */
  182. function removeUser(req, res, next) {
  183. if (req.session.signinStatus) {
  184. let emitter = new events.EventEmitter(),
  185. uuid = req.body.userId,
  186. userNickName = req.body.userNickName;
  187. emitter.on('ok', (results) => {
  188. res.send({
  189. status: 'success',
  190. info: '删除成功'
  191. });
  192. });
  193. emitter.on('err', (err, errInfo) => {
  194. res.json({
  195. status: 'error',
  196. info: errInfo,
  197. error: err
  198. });
  199. });
  200. emitter.on('deleteFromDbOwnTable', () => {
  201. //定义SQL语句查询
  202. let pool = db.pool.userInfoDatabasePool,
  203. sqlString = 'DELETE FROM dbOwn_test WHERE dbOwnUuid = ?',
  204. value = [userNickName];
  205. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  206. if (err) {
  207. emitter.emit('err', err.err, '删除用户信息表记录时出错');
  208. } else {
  209. emitter.emit('ok');
  210. }
  211. });
  212. });
  213. emitter.on('deleteFromUserTable', () => {
  214. //定义SQL语句查询
  215. let pool = db.pool.userInfoDatabasePool,
  216. sqlString = 'DELETE FROM user_test WHERE uuid = ?',
  217. value = [uuid];
  218. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  219. if (err) {
  220. emitter.emit('err', err.err, '用户数据表删除数据出错');
  221. } else {
  222. emitter.emit('deleteFromDbOwnTable');
  223. }
  224. });
  225. });
  226. emitter.on('dropUser', () => {
  227. //定义SQL语句查询
  228. let pool = db.pool.userDatabasePool,
  229. sqlString = 'USE mysql; DELETE FROM user WHERE User = ?',
  230. value = [uuid];
  231. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  232. if (err) {
  233. emitter.emit('err', err.err, '用户数据表删除数据出错');
  234. } else {
  235. emitter.emit('deleteFromUserTable');
  236. }
  237. });
  238. });
  239. emitter.on('dropDatabase', (results) => {
  240. let pool = db.pool.userDatabasePool,
  241. value = null;
  242. results.forEach((item, index) => {
  243. let sqlString = 'DROP DATABASE ' + item.dbName;
  244. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  245. if (err) {
  246. emitter.emit('err', err.err, '删除数据库时出错');
  247. } else {
  248. emitter.emit('dropUser');
  249. }
  250. });
  251. }, this);
  252. });
  253. emitter.on('selectDbOwnInfoFromDbOwnTable', () => {
  254. //定义SQL语句查询
  255. let pool = db.pool.userInfoDatabasePool,
  256. sqlString = 'SELECT dbName FROM dbOwn_test WHERE dbOwnUuid = ?',
  257. value = [uuid];
  258. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  259. if (err) {
  260. emitter.emit('err', err.err, '数据库查询出错');
  261. } else {
  262. emitter.emit('dropDatabase', results);
  263. }
  264. });
  265. });
  266. emitter.emit('selectDbOwnInfoFromDbOwnTable');
  267. } else {
  268. res.send({
  269. status: 'signout',
  270. info: '未登录',
  271. url: '#/signin'
  272. });
  273. }
  274. }
  275. router.get('/', getUserList);
  276. router.get('/getUserCount', getUserCount);
  277. router.get('/getUserOwnDb', getUserOwnDb);
  278. router.post('/removeUser', removeUser);
  279. module.exports = router