| | |
| | | #include "BondEq.h" |
| | | #include "afxdialogex.h" |
| | | #include "UserManagerDlg.h" |
| | | #include "InputDialog.h" |
| | | #include "UserManager.h" |
| | | #include "NewCellTypes/GridCellCombo.h" |
| | | #include "NewCellTypes/GridCellNumeric.h" |
| | | |
| | | #include <set> |
| | | |
| | | const COLORREF CURR_USER_BK_COLOR = RGB(0, 255, 0); |
| | | |
| | | // CUserManagerDlg 对话框 |
| | | |
| | |
| | | return; |
| | | |
| | | int nRows = 1; |
| | | int nCols = 7; |
| | | int nCols = 8; |
| | | |
| | | int nFixRows = 1; |
| | | int nFixCols = 0; |
| | |
| | | |
| | | m_gridUserManager.DeleteAllItems(); |
| | | m_gridUserManager.SetVirtualMode(FALSE); |
| | | m_gridUserManager.GetDefaultCell(TRUE, FALSE)->SetBackClr(g_nGridFixCellColor); |
| | | m_gridUserManager.GetDefaultCell(FALSE, TRUE)->SetBackClr(g_nGridFixCellColor); |
| | | m_gridUserManager.GetDefaultCell(FALSE, FALSE)->SetBackClr(g_nGridCellColor); |
| | | m_gridUserManager.SetFixedTextColor(g_nGridFixFontColor); |
| | | m_gridUserManager.GetDefaultCell(TRUE, FALSE)->SetBackClr(g_nGridFixCellColor); // 设置固定行背景色 |
| | | m_gridUserManager.GetDefaultCell(FALSE, TRUE)->SetBackClr(g_nGridFixCellColor); // 设置固定列背景色 |
| | | m_gridUserManager.GetDefaultCell(FALSE, FALSE)->SetBackClr(g_nGridCellColor); // 设置单元格背景色 |
| | | m_gridUserManager.SetFixedTextColor(g_nGridFixFontColor); // 设置固定行列字体颜色 |
| | | |
| | | m_gridUserManager.SetRowCount(nRows); |
| | | m_gridUserManager.SetColumnCount(nCols); |
| | |
| | | m_gridUserManager.SetFixedColumnCount(nFixCols); |
| | | |
| | | // Col |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 90); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 20); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("No.")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 100); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("用户名")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("密码")); |
| | |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("会话过期(小时)")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("最后一次登录时间")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 100); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("角色描述")); |
| | | |
| | | m_gridUserManager.SetFixedRowSelection(FALSE); |
| | | m_gridUserManager.SetFixedColumnSelection(FALSE); |
| | | m_gridUserManager.EnableSelection(TRUE); |
| | | m_gridUserManager.SetEditable(TRUE); |
| | | m_gridUserManager.SetRowResize(FALSE); |
| | | m_gridUserManager.SetColumnResize(FALSE); |
| | | m_gridUserManager.SetColumnResize(TRUE); |
| | | m_gridUserManager.ExpandColumnsToFit(TRUE); |
| | | m_gridUserManager.SetListMode(TRUE); // 启用列表模式 |
| | | m_gridUserManager.EnableSelection(TRUE); // 启用选择 |
| | | m_gridUserManager.SetSingleRowSelection(TRUE); // 自动整行高亮(限制为单行选择) |
| | | m_gridUserManager.ExpandLastColumn(); // 最后一列填充网格 |
| | | |
| | | m_mapRoleDescriptions.clear(); |
| | | m_mapRoleDescriptions.emplace(_T("管理员"), _T("管理所有用户,分配权限")); |
| | | m_mapRoleDescriptions.emplace(_T("工程师"), _T("维护系统,解决技术问题")); |
| | | m_mapRoleDescriptions.emplace(_T("操作员"), _T("执行日常操作任务")); |
| | | |
| | | FillUserManager(); |
| | | } |
| | |
| | | return; |
| | | } |
| | | |
| | | int nCurrNameRow = -1; |
| | | std::vector<std::vector<std::string>> usersData = userManager.getUsers(); |
| | | if (!usersData.empty()) { |
| | | m_gridUserManager.SetRowCount(usersData.size() + 1); |
| | | |
| | | for (size_t i = 0; i < usersData.size(); i++) { |
| | | for (int i = 0; i < usersData.size(); i++) { |
| | | int nRowIdx = i + 1; |
| | | int nColIdx = 0; |
| | | |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, std::to_string(i + 1).c_str()); |
| | | for (size_t j = 0; j < usersData[i].size(); j++) { |
| | | for (int j = 0; j < usersData[i].size(); j++) { |
| | | if (usersData[i][1].empty()) { |
| | | continue; |
| | | } |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, usersData[i][j].c_str()); |
| | | } |
| | | } |
| | | |
| | | m_gridUserManager.Invalidate(); |
| | | m_gridUserManager.UpdateWindow(); |
| | | // 当前登录用户高亮 |
| | | if (nCurrNameRow == -1 && usersData[i][0] == userManager.getCurrentUser()) { |
| | | nCurrNameRow = nRowIdx; |
| | | } |
| | | } |
| | | } |
| | | |
| | | CStringArray permissions; |
| | |
| | | int nCols = m_gridUserManager.GetColumnCount(); |
| | | for (int i = 1; i < m_gridUserManager.GetRowCount(); ++i) { |
| | | m_gridUserManager.SetItemState(i, 0, GVIS_READONLY); // 第一列只读 |
| | | m_gridUserManager.SetItemState(i, nCols - 2, GVIS_READONLY); // 倒数第二列只读 |
| | | m_gridUserManager.SetItemState(i, nCols - 1, GVIS_READONLY); // 最后一列只读 |
| | | |
| | | // 第四列设置权限列为下拉框 |
| | |
| | | } |
| | | |
| | | m_gridUserManager.SetItemText(i, 3, permissions.GetAt(nRole)); |
| | | |
| | | auto it = m_mapRoleDescriptions.find(permissions.GetAt(nRole)); |
| | | if (it != m_mapRoleDescriptions.end()) { |
| | | m_gridUserManager.SetItemText(i, 7, it->second); |
| | | } |
| | | } |
| | | |
| | | // 第五、六列(会话超时)设置为Numeric |
| | | m_gridUserManager.SetCellType(i, 4, RUNTIME_CLASS(CGridCellNumeric)); |
| | | m_gridUserManager.SetCellType(i, 5, RUNTIME_CLASS(CGridCellNumeric)); |
| | | //m_gridUserManager.SetCellType(i, 4, RUNTIME_CLASS(CGridCellNumeric)); |
| | | //m_gridUserManager.SetCellType(i, 5, RUNTIME_CLASS(CGridCellNumeric)); |
| | | m_gridUserManager.SetItemState(i, 4, GVIS_READONLY); // 第五列只读 |
| | | m_gridUserManager.SetItemState(i, 5, GVIS_READONLY); // 第六列只读 |
| | | |
| | | m_gridUserManager.ExpandColumnsToFit(FALSE); |
| | | m_gridUserManager.ExpandLastColumn(); |
| | | } |
| | | |
| | | for (int i = 0; i < nCols; i++) { |
| | | m_gridUserManager.SetItemBkColour(nCurrNameRow, i, CURR_USER_BK_COLOR); |
| | | } |
| | | m_gridUserManager.SetItemState(nCurrNameRow, 3, GVIS_READONLY); |
| | | |
| | | m_gridUserManager.Invalidate(); |
| | | m_gridUserManager.UpdateWindow(); |
| | | } |
| | | |
| | | void CUserManagerDlg::AddRow(CGridCtrl* pGridCtrl) |
| | |
| | | return; |
| | | } |
| | | |
| | | CInputDialog inputDialog(_T("添加用户"), _T("请输入用户名:")); |
| | | if (inputDialog.DoModal() != IDOK) { |
| | | return; |
| | | } |
| | | |
| | | CString inputText = inputDialog.GetInputText(); |
| | | if (inputText.IsEmpty()) { |
| | | AfxMessageBox(_T("用户名不能为空!"), MB_OK | MB_ICONEXCLAMATION); |
| | | return; |
| | | } |
| | | |
| | | if (IsUsernameDuplicate(inputText)) { |
| | | AfxMessageBox(_T("用户名重复!"), MB_OK | MB_ICONEXCLAMATION); |
| | | return; |
| | | } |
| | | |
| | | int nRowCount = pGridCtrl->GetRowCount(); |
| | | pGridCtrl->SetRowCount(nRowCount + 1); |
| | | int newRowIndex = nRowCount; |
| | |
| | | CString strText; |
| | | strText.Format(_T("%d"), newRowIndex); |
| | | pGridCtrl->SetItemText(newRowIndex, 0, strText); |
| | | strText.Format(_T("User%d"), newRowIndex); |
| | | pGridCtrl->SetItemText(newRowIndex, 1, strText); |
| | | pGridCtrl->SetItemText(newRowIndex, 1, inputText); |
| | | pGridCtrl->SetItemText(newRowIndex, 2, _T("123456")); |
| | | pGridCtrl->SetItemText(newRowIndex, 3, _T("操作员")); |
| | | pGridCtrl->SetItemText(newRowIndex, 4, _T("30")); |
| | | pGridCtrl->SetItemText(newRowIndex, 5, _T("72")); |
| | | pGridCtrl->SetItemText(newRowIndex, 6, _T("2024-01-01 00:00:00")); |
| | | |
| | | int nCols = m_gridUserManager.GetColumnCount(); |
| | | m_gridUserManager.SetItemState(newRowIndex, 0, GVIS_READONLY); // 第一列 |
| | | m_gridUserManager.SetItemState(newRowIndex, nCols - 1, GVIS_READONLY); // 最后一列 |
| | | auto it = m_mapRoleDescriptions.find(_T("操作员")); |
| | | if (it != m_mapRoleDescriptions.end()) { |
| | | pGridCtrl->SetItemText(newRowIndex, 7, it->second); |
| | | } |
| | | |
| | | int nCols = pGridCtrl->GetColumnCount(); |
| | | pGridCtrl->SetItemState(newRowIndex, 0, GVIS_READONLY); // 第一列只读 |
| | | pGridCtrl->SetItemState(newRowIndex, nCols - 2, GVIS_READONLY); // 倒数第二列只读 |
| | | pGridCtrl->SetItemState(newRowIndex, nCols - 1, GVIS_READONLY); // 最后一列只读 |
| | | |
| | | // 第四列设置(权限列)为下拉框 |
| | | CStringArray permissions; |
| | |
| | | permissions.Add(_T("工程师")); |
| | | permissions.Add(_T("操作员")); |
| | | |
| | | if (m_gridUserManager.SetCellType(newRowIndex, 3, RUNTIME_CLASS(CGridCellCombo))) { |
| | | CGridCellCombo* pCell = static_cast<CGridCellCombo*>(m_gridUserManager.GetCell(newRowIndex, 3)); |
| | | if (pGridCtrl->SetCellType(newRowIndex, 3, RUNTIME_CLASS(CGridCellCombo))) { |
| | | CGridCellCombo* pCell = static_cast<CGridCellCombo*>(pGridCtrl->GetCell(newRowIndex, 3)); |
| | | pCell->SetOptions(permissions); |
| | | pCell->SetStyle(CBS_DROPDOWNLIST); |
| | | |
| | | m_gridUserManager.SetItemText(newRowIndex, 3, permissions.GetAt(2)); |
| | | pGridCtrl->SetItemText(newRowIndex, 3, permissions.GetAt(2)); |
| | | } |
| | | |
| | | // 第五、六列(会话超时)设置为Numeric |
| | | m_gridUserManager.SetCellType(newRowIndex, 4, RUNTIME_CLASS(CGridCellNumeric)); |
| | | m_gridUserManager.SetCellType(newRowIndex, 5, RUNTIME_CLASS(CGridCellNumeric)); |
| | | //pGridCtrl->SetCellType(newRowIndex, 4, RUNTIME_CLASS(CGridCellNumeric)); |
| | | //pGridCtrl->SetCellType(newRowIndex, 5, RUNTIME_CLASS(CGridCellNumeric)); |
| | | pGridCtrl->SetItemState(newRowIndex, 4, GVIS_READONLY); // 第五列只读 |
| | | pGridCtrl->SetItemState(newRowIndex, 5, GVIS_READONLY); // 第六列只读 |
| | | |
| | | pGridCtrl->ExpandColumnsToFit(FALSE); |
| | | pGridCtrl->ExpandLastColumn(); |
| | | pGridCtrl->Invalidate(); |
| | | pGridCtrl->UpdateWindow(); |
| | | } |
| | |
| | | pGridCtrl->DeleteRow(*it); |
| | | } |
| | | |
| | | pGridCtrl->ExpandColumnsToFit(FALSE); |
| | | pGridCtrl->ExpandLastColumn(); |
| | | pGridCtrl->Invalidate(); |
| | | pGridCtrl->UpdateWindow(); |
| | | } |
| | |
| | | |
| | | BEGIN_MESSAGE_MAP(CUserManagerDlg, CDialogEx) |
| | | ON_WM_SIZE() |
| | | ON_NOTIFY(GVN_COMBOSELCHANGE, IDC_CUSTOM_USER, &CUserManagerDlg::OnGridComboSelChange) |
| | | ON_BN_CLICKED(IDC_BUTTON_ADD, &CUserManagerDlg::OnBnClickedButtonAdd) |
| | | ON_BN_CLICKED(IDC_BUTTON_DEL, &CUserManagerDlg::OnBnClickedButtonDel) |
| | | ON_BN_CLICKED(IDOK, &CUserManagerDlg::OnBnClickedOk) |
| | |
| | | |
| | | // 遍历对话框中的所有控件 |
| | | AdjustControls(rect.Width(), rect.Height()); |
| | | } |
| | | |
| | | void CUserManagerDlg::OnGridComboSelChange(NMHDR* pNMHDR, LRESULT* pResult) |
| | | { |
| | | NM_GRIDVIEW* pItem = (NM_GRIDVIEW*)pNMHDR; |
| | | |
| | | int nRow = pItem->iRow; |
| | | int nCol = pItem->iColumn; |
| | | |
| | | // 第4列为权限列, 第8列为角色描述列, 从映射中查找对应描述 |
| | | if (nCol == 3) { |
| | | if (m_gridUserManager.GetItemBkColour(nRow, nCol) == CURR_USER_BK_COLOR) { |
| | | AfxMessageBox(_T("当前登录用户权限不能修改!")); |
| | | } |
| | | else { |
| | | CString selectedRole = m_gridUserManager.GetItemText(nRow, nCol); |
| | | auto it = m_mapRoleDescriptions.find(selectedRole); |
| | | if (it != m_mapRoleDescriptions.end()) { |
| | | m_gridUserManager.SetItemText(nRow, 7, it->second); |
| | | m_gridUserManager.RedrawCell(nRow, 7); |
| | | } |
| | | else { |
| | | m_gridUserManager.SetItemText(nRow, 7, _T("")); |
| | | m_gridUserManager.RedrawCell(nRow, 7); |
| | | } |
| | | } |
| | | } |
| | | |
| | | *pResult = 0; |
| | | } |
| | | |
| | | void CUserManagerDlg::OnBnClickedButtonAdd() |
| | |
| | | { |
| | | // TODO: 在此添加控件通知处理程序代码 |
| | | std::vector<std::vector<std::string>> vecData; |
| | | std::set<std::string> usernameSet; // 用于存储用户名,检查是否重复 |
| | | |
| | | int nCurrUserRow = -1; |
| | | UserManager& userManager = UserManager::getInstance(); |
| | | for (int i = 1; i < m_gridUserManager.GetRowCount(); ++i) { |
| | | std::vector<std::string> rowData; |
| | | for (int j = 1; j < m_gridUserManager.GetColumnCount(); ++j) { |
| | | CString cellText = m_gridUserManager.GetItemText(i, 1); |
| | | std::string username = CT2A(cellText.GetString()); |
| | | |
| | | cellText = m_gridUserManager.GetItemText(i, 2); |
| | | std::string userpass = CT2A(cellText.GetString()); |
| | | |
| | | if (username.empty() || userpass.empty()) { |
| | | AfxMessageBox(_T("用户名和密码不能为空!")); |
| | | return; |
| | | } |
| | | |
| | | if (usernameSet.find(username) != usernameSet.end()) { |
| | | CString message; |
| | | message.Format(_T("用户名 [%s] 重复,请修改后保存!"), cellText); |
| | | AfxMessageBox(message, MB_ICONEXCLAMATION); |
| | | return; |
| | | } |
| | | usernameSet.insert(username); |
| | | |
| | | if (nCurrUserRow == -1 && m_gridUserManager.GetItemBkColour(i, 0) == CURR_USER_BK_COLOR){ |
| | | nCurrUserRow = i; |
| | | if (username.compare(userManager.getCurrentUser()) != 0) { |
| | | userManager.setCurrentUser(username); |
| | | } |
| | | |
| | | if (userpass.compare(userManager.getCurrentPass()) != 0) { |
| | | userManager.setCurrentPass(userpass); |
| | | } |
| | | |
| | | userManager.clearSession(); |
| | | userManager.saveSession(); |
| | | } |
| | | |
| | | for (int j = 1; j < m_gridUserManager.GetColumnCount() - 1; ++j) { |
| | | CString cellText = m_gridUserManager.GetItemText(i, j); |
| | | std::string cellString = CT2A(cellText.GetString()); |
| | | |
| | | // 第4列是权限,转换为数字字符串 |
| | | if (j == 3) { |
| | | if (cellText == _T("管理员")) |
| | | cellString = "0"; |
| | |
| | | vecData.push_back(rowData); |
| | | } |
| | | |
| | | UserManager::getInstance().setUsers(vecData); |
| | | userManager.setUsers(vecData); |
| | | CDialogEx::OnOK(); |
| | | } |