user.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. document.addEventListener('DOMContentLoaded', function() {
  2. // 确保在页面内容完全加载后执行
  3. console.log('user.js loaded');
  4. // 其他 JavaScript 代码
  5. });
  6. /*-------------------------------------------------- */
  7. // 统一的打开模态框函数
  8. function openModal(modalId) {
  9. const modal = document.getElementById(modalId);
  10. if (modal) {
  11. modal.style.display = "block"; // 显示模态框
  12. } else {
  13. console.error('模态框不存在,ID:', modalId);
  14. }
  15. }
  16. // 统一的关闭模态框函数
  17. function closeModal(modalId) {
  18. const modal = document.getElementById(modalId);
  19. if (modal) {
  20. modal.style.display = "none"; // 隐藏模态框
  21. } else {
  22. console.error('模态框不存在,ID:', modalId);
  23. }
  24. }
  25. // 点击"新增用户"按钮时,打开添加用户的模态框
  26. document.getElementById("addUserButton").addEventListener('click', function() {
  27. saveNewUserActionModal(); // 打开的是添加用户的模态框
  28. });
  29. /*-------------------------------------------------- */
  30. fetchUsers();
  31. function fetchUsers() {
  32. console.log('Fetching users...');
  33. fetch('http://127.0.0.1:8080/api/admin/userInfoAll', {
  34. method: 'GET',
  35. headers: {
  36. 'Authorization': `Bearer ${authToken}`,
  37. 'Content-Type': 'application/json'
  38. }
  39. })
  40. .then(response => response.json())
  41. .then(data => {
  42. const cardsContainer = document.querySelector('#users-cards');
  43. cardsContainer.innerHTML = ''; // 清空旧的卡片内容
  44. data.data.forEach(user => {
  45. if (user.Account === 'admin') {
  46. return;
  47. }
  48. const card = document.createElement('div');
  49. card.classList.add('user-info-card');
  50. card.innerHTML = `
  51. <div class="user-info-card-content">
  52. <p><strong>用户名:</strong> ${user.Username}</p>
  53. <p><strong>电话:</strong> ${user.Telephone}</p>
  54. <p><strong>邮箱:</strong> ${user.Email}</p>
  55. <p><strong>权限:</strong> ${user.Role}</p>
  56. <p><strong>账号:</strong> ${user.Account}</p>
  57. </div>
  58. `;
  59. card.addEventListener('click', (event) => {
  60. openUserActionModal(user);
  61. });
  62. cardsContainer.appendChild(card);
  63. });
  64. });
  65. }
  66. //打开用户信息模态框
  67. function openUserActionModal(user) {
  68. // 填充用户信息到模态框
  69. document.getElementById("modal-username").innerText = `用户名: ${user.Username}`;
  70. document.getElementById("modal-telephone").innerText = `电话: ${user.Telephone}`;
  71. document.getElementById("modal-email").innerText = `邮箱: ${user.Email}`;
  72. document.getElementById("modal-role").innerText = `权限: ${user.Role}`;
  73. document.getElementById("modal-account").innerText = `账号: ${user.Account}`;
  74. // 调用统一的 openModal 函数来显示模态框
  75. openModal('userActionModal');
  76. // 处理修改用户按钮点击
  77. document.getElementById("editUserButton").onclick = () => {
  78. editUser(user);
  79. };
  80. // 处理删除用户按钮点击
  81. document.getElementById("deleteUserButton").onclick = () => {
  82. deleteUser(user);
  83. };
  84. // 处理修改密码按钮点击
  85. document.getElementById("resetPasswordButton").onclick = () => {
  86. resetUserPassword(user);
  87. };
  88. // 关闭模态框的点击事件(右上角关闭按钮)
  89. document.querySelector(".user-info-close").onclick = function() {
  90. closeModal("userActionModal"); // 使用封装好的 closeModal 函数
  91. };
  92. // 点击模态框外部时关闭模态框
  93. window.onclick = function(event) {
  94. const modal = document.getElementById("userActionModal");
  95. if (event.target === modal) {
  96. closeModal("userActionModal"); // 使用封装好的 closeModal 函数
  97. }
  98. };
  99. }
  100. function editUser(user) {
  101. console.log("Edit user", user);
  102. // 在这里实现修改用户逻辑
  103. }
  104. function deleteUser(user) {
  105. console.log("Delete user", user);
  106. // 在这里实现删除用户逻辑
  107. }
  108. function resetUserPassword(user) {
  109. console.log("Reset password for user", user);
  110. // 在这里实现修改密码逻辑
  111. }
  112. // 保存新用户
  113. // 保存新用户的函数,并在适当的时候打开和关闭模态框
  114. function saveNewUserActionModal(user) {
  115. openModal('user-info-addUserModal'); // 打开的是添加用户的模态框
  116. // 点击模态框外部时关闭模态框
  117. window.onclick = function(event) {
  118. const modal = document.getElementById("user-info-addUserModal");
  119. if (event.target === modal) {
  120. closeModal("user-info-addUserModal"); // 使用封装好的 closeModal 函数
  121. }
  122. };
  123. }
  124. function saveNewUser() {
  125. const username = document.getElementById('user-info-addUsername').value.trim();
  126. const password = document.getElementById('user-info-addPassword').value.trim();
  127. const account = document.getElementById('user-info-addAccount').value.trim();
  128. const telephone = document.getElementById('user-info-addTelephone').value.trim();
  129. const email = document.getElementById('user-info-addEmail').value.trim();
  130. // 验证每个字段都不能为空
  131. if (!username || !password || !account || !telephone || !email) {
  132. alert('有空选项未填写');
  133. return;
  134. }
  135. // 验证电话是否为11位数字
  136. const phonePattern = /^\d{11}$/;
  137. if (!phonePattern.test(telephone)) {
  138. alert('电话必须是11位数字');
  139. return;
  140. }
  141. // 验证密码长度是否至少为6位
  142. if (password.length < 6) {
  143. alert('密码长度必须至少为6位');
  144. return;
  145. }
  146. // 验证邮箱是否包含@符号
  147. if (!email.includes('@')) {
  148. alert('邮箱必须包含@符号');
  149. return;
  150. }
  151. // 验证账号长度是否至少为3位
  152. if (account.length < 3) {
  153. alert('账号长度必须至少为3位');
  154. return;
  155. }
  156. // 构造新用户对象
  157. const newUser = {
  158. Username: username,
  159. Password: password,
  160. Account: account,
  161. Telephone: telephone,
  162. Email: email
  163. };
  164. // 发起请求保存新用户
  165. fetch('http://127.0.0.1:8080/api/register', { // 修改为实际的API路径
  166. method: 'POST',
  167. headers: {
  168. 'Authorization': `Bearer ${authToken}`, // 确保 authToken 是有效的
  169. 'Content-Type': 'application/json'
  170. },
  171. body: JSON.stringify(newUser)
  172. })
  173. .then(response => response.json())
  174. .then(data => {
  175. if (data.success) {
  176. alert('用户创建成功!');
  177. fetchUsers(); // 重新加载用户列表
  178. closeModal('user-info-addUserModal'); // 关闭模态框
  179. } else {
  180. alert('用户创建失败:' + data.error);
  181. }
  182. })
  183. .catch(error => {
  184. alert('请求失败:' + error);
  185. closeModal('user-info-addUserModal'); // 即使请求失败也可以选择关闭模态框
  186. });
  187. }