class UserManagement extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.users = []; this.currentUser = null; } connectedCallback() { this.render(); this.fetchUsers(); } async fetchUsers() { try { const response = await fetch('/api/all-users'); const data = await response.json(); if (data.success) { this.users = data.users.filter(user => user.id !== 1); this.render(); } else { console.error('获取用户列表失败:', data.message); } } catch (error) { console.error('获取用户列表出错:', error); } } showError(message) { this.shadowRoot.querySelector('.error-message').textContent = message; this.shadowRoot.querySelector('.error-message').style.display = 'block'; } async handleEditInfo(userId) { try { const response = await fetch(`/api/user-info/${userId}`); const data = await response.json(); if (data.success) { this.currentUser = data.user; this.showEditModal(); } else { alert('获取用户信息失败: ' + data.message); } } catch (error) { console.error('获取用户信息出错:', error); alert('获取用户信息失败'); } } showEditModal() { this.shadowRoot.querySelector('.modal').style.display = 'flex'; this.populateForm(); } hideEditModal() { this.shadowRoot.querySelector('.modal').style.display = 'none'; this.currentUser = null; } populateForm() { const form = this.shadowRoot.querySelector('.edit-form'); form.username.value = this.currentUser.username || ''; form.full_name.value = this.currentUser.full_name || ''; form.email.value = this.currentUser.email || ''; form.phone.value = this.currentUser.phone || ''; form.department.value = this.currentUser.department || ''; form.position.value = this.currentUser.position || ''; } async handleSaveUserInfo() { const form = this.shadowRoot.querySelector('.edit-form'); const userInfo = { full_name: form.full_name.value, email: form.email.value, phone: form.phone.value, department: form.department.value, position: form.position.value }; try { const response = await fetch('/api/update-user-info', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: this.currentUser.id, userInfo: userInfo }) }); const data = await response.json(); if (data.success) { alert('用户信息更新成功'); this.hideEditModal(); this.fetchUsers(); // 刷新用户列表 } else { alert('用户信息更新失败: ' + data.message); } } catch (error) { console.error('更新用户信息出错:', error); alert('更新用户信息失败'); } } handleChangeAccessLevel(userId) { this.currentUser = { id: userId }; this.showAccessModal(); } showAccessModal() { this.shadowRoot.querySelector('.access-modal').style.display = 'flex'; } hideAccessModal() { this.shadowRoot.querySelector('.access-modal').style.display = 'none'; this.currentUser = null; } async handleSaveAccessLevel() { const form = this.shadowRoot.querySelector('.access-form'); const accessLevel = parseInt(form.access_level.value); try { const response = await fetch('/api/update-access-level', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: this.currentUser.id, accessLevel: accessLevel }) }); const data = await response.json(); if (data.success) { alert('权限级别更新成功'); this.hideAccessModal(); this.fetchUsers(); // 刷新用户列表 } else { alert('权限级别更新失败: ' + data.message); } } catch (error) { console.error('更新权限级别出错:', error); alert('更新权限级别失败'); } } async handleResetPassword(userId) { if (confirm('确定要重置该用户的密码吗?')) { try { const response = await fetch('/api/reset-password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: parseInt(userId) }) }); const data = await response.json(); if (data.success) { alert('密码重置成功'); } else { alert('密码重置失败: ' + data.message); } } catch (error) { console.error('重置密码出错:', error); alert('重置密码失败'); } } } async handleDelete(userId) { if (confirm('确定要删除该用户吗?此操作不可恢复!')) { try { const response = await fetch('/api/delete-user', { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: parseInt(userId) }) }); const data = await response.json(); if (data.success) { alert('用户删除成功'); // 重新获取用户列表 this.fetchUsers(); } else { alert('用户删除失败: ' + data.message); } } catch (error) { console.error('删除用户出错:', error); alert('删除用户失败'); } } } getAccessLevelName(level) { const accessLevels = { 0: '访客', 1: '用户', 2: '开发人员', 3: '组长' }; return accessLevels[level] || level; } render() { this.shadowRoot.innerHTML = `
用户管理
${this.users.length === 0 ? '
正在加载用户数据...
' : ` ${this.users.map(user => ` `).join('')}
ID 用户名 姓名 权限等级 操作
${user.id} ${user.username} ${user.full_name || '-'} ${this.getAccessLevelName(user.access_level)}
` }
调整权限级别
`; } } customElements.define('user-management', UserManagement);