user.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.UserModel = void 0;
  7. const bcryptjs_1 = __importDefault(require("bcryptjs"));
  8. const database_1 = require("../config/database");
  9. class UserModel {
  10. static async create(params) {
  11. try {
  12. const { username, password, role = 'user', email } = params;
  13. const salt = await bcryptjs_1.default.genSalt(10);
  14. const hashedPassword = await bcryptjs_1.default.hash(password, salt);
  15. const emailValue = email || null;
  16. const uuidResult = await (0, database_1.executeQuery)('SELECT UUID() as uuid');
  17. const id = uuidResult[0].uuid;
  18. const query = `
  19. INSERT INTO users (id, username, password, email, role, created_at, updated_at)
  20. VALUES (?, ?, ?, ?, ?, NOW(), NOW())
  21. `;
  22. await (0, database_1.executeQuery)(query, [id, username, hashedPassword, emailValue, role]);
  23. const user = await this.getById(id);
  24. if (!user) {
  25. throw new Error('创建用户失败,无法获取创建的用户信息');
  26. }
  27. return user;
  28. }
  29. catch (error) {
  30. console.error('创建用户失败:', error);
  31. throw error;
  32. }
  33. }
  34. static async getById(id) {
  35. try {
  36. const query = `
  37. SELECT id, username, password, email, role, created_at, updated_at
  38. FROM users
  39. WHERE id = ?
  40. `;
  41. const result = await (0, database_1.executeQuery)(query, [id]);
  42. if (result.length === 0) {
  43. return null;
  44. }
  45. return result[0];
  46. }
  47. catch (error) {
  48. console.error('根据ID获取用户失败:', error);
  49. throw error;
  50. }
  51. }
  52. static async getByUsername(username) {
  53. try {
  54. const query = `
  55. SELECT id, username, password, email, role, created_at, updated_at
  56. FROM users
  57. WHERE username = ?
  58. `;
  59. const result = await (0, database_1.executeQuery)(query, [username]);
  60. if (result.length === 0) {
  61. return null;
  62. }
  63. return result[0];
  64. }
  65. catch (error) {
  66. console.error('根据用户名获取用户失败:', error);
  67. throw error;
  68. }
  69. }
  70. static async update(id, updates) {
  71. try {
  72. const updateFields = [];
  73. const params = [];
  74. if (updates.username) {
  75. updateFields.push('username = ?');
  76. params.push(updates.username);
  77. }
  78. if (updates.role) {
  79. updateFields.push('role = ?');
  80. params.push(updates.role);
  81. }
  82. if (updates.email) {
  83. updateFields.push('email = ?');
  84. params.push(updates.email);
  85. }
  86. updateFields.push('updated_at = NOW()');
  87. if (updateFields.length === 1) {
  88. return await this.getById(id);
  89. }
  90. params.push(id);
  91. const query = `
  92. UPDATE users
  93. SET ${updateFields.join(', ')}
  94. WHERE id = ?
  95. `;
  96. await (0, database_1.executeQuery)(query, params);
  97. return await this.getById(id);
  98. }
  99. catch (error) {
  100. console.error('更新用户失败:', error);
  101. throw error;
  102. }
  103. }
  104. static async updatePassword(id, newPassword) {
  105. try {
  106. const salt = await bcryptjs_1.default.genSalt(10);
  107. const hashedPassword = await bcryptjs_1.default.hash(newPassword, salt);
  108. const query = `
  109. UPDATE users
  110. SET password = ?, updated_at = NOW()
  111. WHERE id = ?
  112. `;
  113. await (0, database_1.executeQuery)(query, [hashedPassword, id]);
  114. }
  115. catch (error) {
  116. console.error('更新用户密码失败:', error);
  117. throw error;
  118. }
  119. }
  120. static async delete(id) {
  121. try {
  122. const query = 'DELETE FROM users WHERE id = ?';
  123. const result = await (0, database_1.executeQuery)(query, [id]);
  124. return result.affectedRows > 0;
  125. }
  126. catch (error) {
  127. console.error('删除用户失败:', error);
  128. throw error;
  129. }
  130. }
  131. static async getAll(limit, offset) {
  132. try {
  133. let query = `
  134. SELECT id, username, password, email, role, created_at, updated_at
  135. FROM users
  136. ORDER BY created_at DESC
  137. `;
  138. const params = [];
  139. if (limit !== undefined) {
  140. query += ' LIMIT ?';
  141. params.push(limit);
  142. if (offset !== undefined) {
  143. query += ' OFFSET ?';
  144. params.push(offset);
  145. }
  146. }
  147. const result = await (0, database_1.executeQuery)(query, params);
  148. return result;
  149. }
  150. catch (error) {
  151. console.error('获取所有用户失败:', error);
  152. throw error;
  153. }
  154. }
  155. }
  156. exports.UserModel = UserModel;
  157. //# sourceMappingURL=user.js.map