chenluhua1980
2026-01-08 e878ac7788fef44639145ffd971ab0eb06d692ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "stdafx.h"
#include "CUserEdit2Dlg.h"
#include "CUserManager2.h"
#include "resource.h"
 
IMPLEMENT_DYNAMIC(CUserEdit2Dlg, CDialogEx)
 
CUserEdit2Dlg::CUserEdit2Dlg(bool editMode, CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG_USER_EDIT2, pParent)
{
    m_bEditMode = editMode;
}
 
CUserEdit2Dlg::~CUserEdit2Dlg()
{
}
 
void CUserEdit2Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT_USER_ACCOUNT, m_strUsername);
    DDX_Text(pDX, IDC_EDIT_USER_DISPLAY, m_strDisplayName);
    DDX_Text(pDX, IDC_EDIT_USER_PASSWORD, m_strPassword);
    DDX_CBString(pDX, IDC_COMBO_USER_ROLE, m_strRole);
    DDX_Check(pDX, IDC_CHECK_USER_ENABLED, m_bEnabled);
}
 
BEGIN_MESSAGE_MAP(CUserEdit2Dlg, CDialogEx)
END_MESSAGE_MAP()
 
BOOL CUserEdit2Dlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    if (m_bEditMode) {
        if (auto pEdit = GetDlgItem(IDC_EDIT_USER_ACCOUNT)) {
            pEdit->EnableWindow(FALSE);
        }
    }
 
    UpdateData(FALSE);
 
    auto roles = CUserManager2::getInstance().getRoles();
    CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_USER_ROLE);
    if (pCombo) {
        int selected = -1;
        for (const auto& role : roles) {
            CString text(role.name.c_str());
            int idx = pCombo->AddString(text);
            if (selected == -1 && m_strRole.CompareNoCase(text) == 0) {
                selected = idx;
            }
        }
 
        if (selected >= 0) {
            pCombo->SetCurSel(selected);
        }
        else if (pCombo->GetCount() > 0) {
            pCombo->SetCurSel(0);
            CString text;
            pCombo->GetLBText(0, text);
            if (m_strRole.IsEmpty()) {
                m_strRole = text;
            }
        }
    }
 
    if (auto pPwd = GetDlgItem(IDC_EDIT_USER_PASSWORD)) {
        pPwd->EnableWindow(!m_bEditMode);
        if (m_bEditMode) {
            pPwd->SetWindowText(_T(""));
        }
    }
 
    return TRUE;
}
 
void CUserEdit2Dlg::OnOK()
{
    UpdateData(TRUE);
 
    CString user = m_strUsername;
    user.Trim();
    CString role = m_strRole;
    role.Trim();
 
    CString password = m_strPassword;
    password.Trim();
 
    if (m_bEditMode) {
        password.Empty();
    }
 
    if (!m_bEditMode) {
        if (user.IsEmpty()) {
            AfxMessageBox(_T("请输入账号"));
            return;
        }
 
        if (password.IsEmpty()) {
            AfxMessageBox(_T("请输入密码"));
            return;
        }
    }
 
    if (role.IsEmpty()) {
        AfxMessageBox(_T("请选择角色"));
        return;
    }
 
    if (auto pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_USER_ROLE)) {
        int sel = pCombo->GetCurSel();
        if (sel != CB_ERR) {
            CString text;
            pCombo->GetLBText(sel, text);
            if (!text.IsEmpty()) {
                m_strRole = text;
            }
        }
    }
 
    m_strUsername = user;
    m_strPassword = password;
    CDialogEx::OnOK();
}