user.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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://10.28.20.105: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. function openUserActionModal(user) {
  67. // 填充用户信息到模态框
  68. document.getElementById("modal-username").innerText = `用户名: ${user.Username}`;
  69. document.getElementById("modal-telephone").innerText = `电话: ${user.Telephone}`;
  70. document.getElementById("modal-email").innerText = `邮箱: ${user.Email}`;
  71. document.getElementById("modal-role").innerText = `权限: ${user.Role}`;
  72. document.getElementById("modal-account").innerText = `账号: ${user.Account}`;
  73. // 调用统一的 openModal 函数来显示模态框
  74. openModal('userActionModal');
  75. // 处理修改用户按钮点击
  76. document.getElementById("editUserButton").onclick = () => {
  77. closeModal("userActionModal"); // 先关闭用户信息模态框
  78. editUser(user); // 打开编辑用户模态框
  79. };
  80. // 处理删除用户按钮点击
  81. document.getElementById("deleteUserButton").onclick = () => {
  82. deleteUser(user);
  83. };
  84. // 关闭模态框的点击事件(右上角关闭按钮)
  85. document.querySelector(".user-info-close").onclick = function() {
  86. closeModal("userActionModal"); // 使用封装好的 closeModal 函数
  87. };
  88. // 点击模态框外部时关闭模态框
  89. window.onclick = function(event) {
  90. const modal = document.getElementById("userActionModal");
  91. if (event.target === modal) {
  92. closeModal("userActionModal"); // 使用封装好的 closeModal 函数
  93. }
  94. };
  95. }
  96. //修改用户
  97. function editUser(user) {
  98. const modal = document.getElementById("editUserModal");
  99. const usernameInput = document.getElementById("username");
  100. const telephoneInput = document.getElementById("telephone");
  101. const emailInput = document.getElementById("email");
  102. const passwordInput = document.getElementById("password");
  103. const roleSelectDiv = document.getElementById("roleSelectDiv");
  104. const roleSelect = document.getElementById("role");
  105. // 填充用户信息
  106. usernameInput.value = user.Username;
  107. telephoneInput.value = user.Telephone;
  108. emailInput.value = user.Email;
  109. // 检查权限并填充角色选择框
  110. if (currentUserPermissions.includes('update_role')) {
  111. roleSelectDiv.style.display = "block";
  112. fetch('http://10.28.20.105:8080/api/admin/GetRoleNames', {
  113. method: 'GET',
  114. headers: {
  115. 'Authorization': `Bearer ${authToken}`,
  116. 'Content-Type': 'application/json'
  117. }
  118. })
  119. .then(response => response.json())
  120. .then(data => {
  121. roleSelect.innerHTML = ''; // 清空选项
  122. data.roles.forEach(role => {
  123. const option = document.createElement('option');
  124. option.value = role;
  125. option.textContent = role;
  126. if (user.Role === role) {
  127. option.selected = true;
  128. }
  129. roleSelect.appendChild(option);
  130. });
  131. })
  132. .catch(error => console.error('获取角色列表时出错:', error));
  133. } else {
  134. roleSelectDiv.style.display = "none";
  135. }
  136. // 显示编辑用户模态框
  137. modal.classList.add("show");
  138. // 关闭模态框事件
  139. document.getElementById("closeModalButton").onclick = () => {
  140. modal.classList.remove("show");
  141. };
  142. console.log("user",user);
  143. // 保存修改事件
  144. document.getElementById("saveChangesButton").onclick = () => {
  145. const updatedUser = {
  146. id: user.Id,
  147. unique_id : user.UniqueID,
  148. account: user.Account,
  149. username: usernameInput.value,
  150. telephone: telephoneInput.value,
  151. email: emailInput.value,
  152. password: passwordInput.value,
  153. role: roleSelect.value
  154. };
  155. // API 请求保存用户信息
  156. fetch('http://10.28.20.105:8080/api/admin/updateUser', {
  157. method: 'POST',
  158. headers: {
  159. 'Authorization': `Bearer ${authToken}`,
  160. 'Content-Type': 'application/json'
  161. },
  162. body: JSON.stringify(updatedUser)
  163. })
  164. .then(response => {
  165. if (response.ok) {
  166. console.log('用户信息保存成功');
  167. modal.classList.remove("show"); // 关闭模态框
  168. alert('用户信息修改成功!'); // 使用alert弹出提示
  169. fetchUsers();
  170. //window.location.reload(); // 刷新页面
  171. // 可以在这里添加成功提示
  172. } else {
  173. alert('用户信息修改失败:', response.statusText); // 使用alert弹出提示
  174. console.error('保存用户信息失败:', response.statusText);
  175. }
  176. })
  177. .catch(error => console.error('请求出错:', error));
  178. };
  179. }
  180. // 绑定按钮点击事件
  181. document.getElementById("editUserButton").onclick = () => {
  182. editUser(user);
  183. };
  184. //删除用户
  185. function deleteUser(user) {
  186. const uniqueID = user.UniqueID; // 获取用户的唯一ID
  187. fetch('http://10.28.20.105:8080/api/admin/deleteUser', {
  188. method: 'POST',
  189. headers: {
  190. 'Authorization': `Bearer ${authToken}`,
  191. 'Content-Type': 'application/json'
  192. },
  193. body: JSON.stringify({ UniqueID: uniqueID })
  194. })
  195. .then(response => response.json())
  196. .then(data => {
  197. if (data.success) {
  198. closeModal("userActionModal"); // 使用封装好的 closeModal 函数
  199. alert('用户删除成功!');
  200. fetchUsers(); // 重新加载用户列表
  201. } else {
  202. closeModal("userActionModal"); // 使用封装好的 closeModal 函数
  203. alert('用户删除失败:' + data.error);
  204. }
  205. })
  206. .catch(error => {
  207. console.error('删除用户时发生错误:', error);
  208. alert('删除用户时发生错误,请稍后再试。');
  209. });
  210. }
  211. // 保存新用户
  212. // 保存新用户的函数,并在适当的时候打开和关闭模态框
  213. function saveNewUserActionModal(user) {
  214. openModal('user-info-addUserModal'); // 打开的是添加用户的模态框
  215. // 点击模态框外部时关闭模态框
  216. window.onclick = function(event) {
  217. const modal = document.getElementById("user-info-addUserModal");
  218. if (event.target === modal) {
  219. closeModal("user-info-addUserModal"); // 使用封装好的 closeModal 函数
  220. }
  221. };
  222. }
  223. function saveNewUser() {
  224. const username = document.getElementById('user-info-addUsername').value.trim();
  225. const password = document.getElementById('user-info-addPassword').value.trim();
  226. const account = document.getElementById('user-info-addAccount').value.trim();
  227. const telephone = document.getElementById('user-info-addTelephone').value.trim();
  228. const email = document.getElementById('user-info-addEmail').value.trim();
  229. // 验证每个字段都不能为空
  230. if (!username || !password || !account || !telephone || !email) {
  231. alert('有空选项未填写');
  232. return;
  233. }
  234. // 验证电话是否为11位数字
  235. const phonePattern = /^\d{11}$/;
  236. if (!phonePattern.test(telephone)) {
  237. alert('电话必须是11位数字');
  238. return;
  239. }
  240. // 验证密码长度是否至少为6位
  241. if (password.length < 6) {
  242. alert('密码长度必须至少为6位');
  243. return;
  244. }
  245. // 验证邮箱是否包含@符号
  246. if (!email.includes('@')) {
  247. alert('邮箱必须包含@符号');
  248. return;
  249. }
  250. // 验证账号长度是否至少为3位
  251. if (account.length < 3) {
  252. alert('账号长度必须至少为3位');
  253. return;
  254. }
  255. // 构造新用户对象
  256. const newUser = {
  257. Username: username,
  258. Password: password,
  259. Account: account,
  260. Telephone: telephone,
  261. Email: email
  262. };
  263. // 发起请求保存新用户
  264. fetch('http://10.28.20.105:8080/api/register', { // 修改为实际的API路径
  265. method: 'POST',
  266. headers: {
  267. 'Authorization': `Bearer ${authToken}`, // 确保 authToken 是有效的
  268. 'Content-Type': 'application/json'
  269. },
  270. body: JSON.stringify(newUser)
  271. })
  272. .then(response => response.json())
  273. .then(data => {
  274. if (data.success) {
  275. alert('用户创建成功!');
  276. fetchUsers(); // 重新加载用户列表
  277. closeModal('user-info-addUserModal'); // 关闭模态框
  278. } else {
  279. alert('用户创建失败:' + data.error);
  280. }
  281. })
  282. .catch(error => {
  283. alert('请求失败:' + error);
  284. closeModal('user-info-addUserModal'); // 即使请求失败也可以选择关闭模态框
  285. });
  286. }