signup.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. router.post('/', (req, res, next) => {
  7. if (!req.body) {
  8. res.status(200).send({
  9. status: 'error',
  10. info: '参数错误'
  11. });
  12. } else {
  13. let captchaCode = req.body.captcha;
  14. if (req.session.captcha !== captchaCode) {
  15. res.status(200).send({
  16. status: 'error',
  17. info: '验证码错误'
  18. });
  19. } else {
  20. let emitter = new events.EventEmitter(),
  21. userId = req.body.userId,
  22. password = req.body.password;
  23. emitter.on('ok', () => {
  24. res.status(200).send({
  25. status: 'successed',
  26. info: '注册成功',
  27. url: '#/signin'
  28. });
  29. });
  30. emitter.on('err', (err, errInfo) => {
  31. res.status(200).send({
  32. status: 'error',
  33. info: errInfo,
  34. error: err
  35. });
  36. });
  37. //检测UserId是否存在
  38. emitter.on('selectUserIdFromUserTable', () => {
  39. //定义SQL语句查询UserID是否存在
  40. let pool = db.pool.adminDatabasePool;
  41. sqlString = 'SELECT userId FROM CloudSqlAdmin_Users WHERE userId = ?',
  42. value = [userId];
  43. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  44. if (err) {
  45. emitter.emit('err', err.err, err.errInfo);
  46. } else {
  47. if (results.length !== 0) {
  48. emitter.emit('err', null, '邮箱或用户名已存在');
  49. } else {
  50. emitter.emit('createSalt');
  51. }
  52. }
  53. });
  54. })
  55. //生成salt
  56. emitter.on('createSalt', () => {
  57. crypto.randomBytes(128, (err, salt) => {
  58. if (err) { throw err; }
  59. salt = salt.toString('hex');
  60. emitter.emit('createPassword', salt);
  61. });
  62. });
  63. //生成密码
  64. emitter.on('createPassword', (salt) => {
  65. crypto.pbkdf2(password, salt, 4096, 256, 'SHA256', (err, hash) => {
  66. if (err) {
  67. emitter.emit('err', err, '后端逻辑出错,请联系管理员修复该问题');
  68. throw err;
  69. }
  70. hash = hash.toString('hex'); //生成密文
  71. emitter.emit('insertUserInfoToUserTable', hash, salt);
  72. })
  73. });
  74. //向User表插入数据
  75. emitter.on('insertUserInfoToUserTable', (hash, salt) => {
  76. //定义向Users表插入用户账户信息的SQL语句
  77. let pool = db.pool.adminDatabasePool;
  78. sqlString = 'INSERT INTO CloudSqlAdmin_Users (uid, userId, password, salt, createTime, lastUpdateTime) VALUES (uuid(), ?, ?, ?, NOW(), NOW())',
  79. value = [userId, hash, salt];
  80. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  81. if (err) {
  82. emitter.emit('err', err.err, err.errInfo);
  83. } else {
  84. emitter.emit('insertUserInfoToUserProfileTable');
  85. }
  86. });
  87. });
  88. //向UserProfile表插入数据
  89. emitter.on('insertUserInfoToUserProfileTable', () => {
  90. //定义向UsersProfile表插入用户账户信息的SQL语句
  91. let pool = db.pool.adminDatabasePool;
  92. sqlString = 'INSERT INTO CloudSqlAdmin_UsersProfile (uid, userId, inviteCode, userStatus, createTime, lastUpdateTime) VALUES (uuid(), ?, ?, 1, NOW(), NOW())',
  93. value = [userId, '$$$TempPassCode'];
  94. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  95. if (err) {
  96. emitter.emit('err', err.err, err.errInfo);
  97. } else {
  98. emitter.emit('ok');
  99. }
  100. });
  101. });
  102. emitter.emit('selectUserIdFromUserTable');
  103. }
  104. }
  105. });
  106. module.exports = router;