#include "../pch/pch.h" #include "CLoginWnd.h" #include "CUpdateWnd.h" #include "CMessageboxWnd.h" void CLoginWnd::Init() { CLabelUI* version = static_cast(m_pm.FindControl(_T("login_version"))); version->SetText((L"智铺子收银软件 " + CLewaimaiString::UTF8ToUnicode(CSystem::GetVersion())).c_str()); std::map users = CSetting::getUsers(); CComboUI* pCom = static_cast(m_pm.FindControl(_T("accountcombo"))); for(std::map::iterator it = users.begin(); it != users.end(); it++) { std::string username = it->first; CListLabelElementUI* elem = new CListLabelElementUI(); elem->SetText(CLewaimaiString::UTF8ToUnicode(username).c_str()); pCom->Add(elem); } CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if(CSetting::GetParam("setting_is_remember_password") == "1") { pRemember->Selected(true, false); } else { pRemember->Selected(false, false); } if(CSetting::GetParam("setting_is_auto_login") == "1") { //自动登录开启了,记住密码一定要开启 pAuto->Selected(true, false); pRemember->Selected(true, false); } else { pAuto->Selected(false, false); } std::string last_login_username = CSetting::GetParam("last_login_username"); std::string password = CSetting::GetUser(last_login_username); CEditUI* pAccountEdit = static_cast(m_pm.FindControl(_T("accountedit"))); CEditUI* pPasswordEdit = static_cast(m_pm.FindControl(_T("pwdedit"))); pAccountEdit->SetText(CLewaimaiString::UTF8ToUnicode(last_login_username).c_str()); pPasswordEdit->SetText(CLewaimaiString::UTF8ToUnicode(password).c_str()); if(CSetting::GetParam("setting_is_auto_login") == "1") { StartLogin(); } else { CVerticalLayoutUI* pInput = static_cast(m_pm.FindControl(_T("login_input"))); pInput->SetVisible(true); CVerticalLayoutUI* pLoading = static_cast(m_pm.FindControl(_T("login_loading"))); pLoading->SetVisible(false); } if(m_mode == 2) { PostMessage(WM_LOGIN_AGAIN_OUT); } } void CLoginWnd::Notify(TNotifyUI& msg) { if(msg.sType == _T("click")) { if(msg.pSender->GetName() == _T("closebtn")) { PostQuitMessage(0); return; } else if(msg.pSender->GetName() == _T("loginBtn")) { StartLogin(); return; } else if(msg.pSender->GetName() == _T("login_auto_login")) { CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if(!pAuto->IsSelected()) { pRemember->Selected(true, false); } } else if(msg.pSender->GetName() == _T("login_remember_password")) { CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if(pRemember->IsSelected()) { pAuto->Selected(false, false); } } else if(msg.pSender->GetName() == _T("guanwang")) { ShellExecute(NULL, _T("open"), _T("explorer.exe"), _T("https://www.zhipuzi.com"), NULL, SW_SHOW); } } else if(msg.sType == _T("itemselect")) { CDuiString name = msg.pSender->GetName(); if(name == _T("accountcombo")) { CComboUI* pCom = static_cast(m_pm.FindControl(_T("accountcombo"))); std::wstring name = pCom->GetItemAt(pCom->GetCurSel())->GetText(); std::string password = CSetting::GetUser(CLewaimaiString::UnicodeToUTF8(name)); std::wstring wspassword = CLewaimaiString::UTF8ToUnicode(password); CEditUI* pEdit = static_cast(m_pm.FindControl(_T("accountedit"))); pEdit->SetText(name.c_str()); CEditUI* pPassword = static_cast(m_pm.FindControl(_T("pwdedit"))); pPassword->SetText(wspassword.c_str()); } } } LRESULT CLoginWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { LONG styleValue = ::GetWindowLong(*this, GWL_STYLE); styleValue &= ~WS_CAPTION; ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN); // 把自己的窗口句柄与窗口绘制管理器挂接在一起 m_pm.Init(m_hWnd); m_pm.AddPreMessageFilter(this); CDialogBuilder builder; CControlUI* pRoot = builder.Create(_T("login.xml"), (UINT)0, NULL, &m_pm); ASSERT(pRoot && "Failed to parse XML"); // 把这些控件绘制到本窗口上 m_pm.AttachDialog(pRoot); // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数 m_pm.AddNotifier(this); Init(); return 0; } LRESULT CLoginWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { bHandled = FALSE; return 0; } /* *这个是窗口被销毁的时候调用的 **/ LRESULT CLoginWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { bHandled = FALSE; return 0; } LRESULT CLoginWnd::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if(::IsIconic(*this)) { bHandled = FALSE; } return (wParam == 0) ? TRUE : FALSE; } LRESULT CLoginWnd::OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return 0; } LRESULT CLoginWnd::OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { //在这里设置焦点才有用 CEditUI* pAccountEdit = static_cast(m_pm.FindControl(_T("accountedit"))); if(pAccountEdit) { pAccountEdit->SetFocus(); } return 0; } LRESULT CLoginWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam); ::ScreenToClient(*this, &pt); RECT rcClient; ::GetClientRect(*this, &rcClient); RECT rcCaption = m_pm.GetCaptionRect(); if(pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \ && pt.y >= rcCaption.top && pt.y < rcCaption.bottom) { CControlUI* pControl = static_cast(m_pm.FindControl(pt)); if(pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0) { return HTCAPTION; } } return HTCLIENT; } LRESULT CLoginWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { SIZE szRoundCorner = m_pm.GetRoundCorner(); if(!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) { CDuiRect rcWnd; ::GetWindowRect(*this, &rcWnd); rcWnd.Offset(-rcWnd.left, -rcWnd.top); rcWnd.right++; rcWnd.bottom++; HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy); ::SetWindowRgn(*this, hRgn, TRUE); ::DeleteObject(hRgn); } bHandled = FALSE; return 0; } LRESULT CLoginWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT lRes = 0; BOOL bHandled = TRUE; switch(uMsg) { case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break; case WM_CLOSE: lRes = OnClose(uMsg, wParam, lParam, bHandled); break; case WM_DESTROY: lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break; case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break; case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break; case WM_NCPAINT: lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break; case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break; case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break; default: bHandled = FALSE; } if(bHandled) { return lRes; } if(m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) { return lRes; } return CWindowWnd::HandleMessage(uMsg, wParam, lParam); } LRESULT CLoginWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled) { if(uMsg == WM_KEYDOWN) { if(wParam == VK_RETURN) { CEditUI* pEdit = static_cast(m_pm.FindControl(_T("accountedit"))); if(pEdit->GetText().IsEmpty()) { pEdit->SetFocus(); } else { pEdit = static_cast(m_pm.FindControl(_T("pwdedit"))); if(pEdit->GetText().IsEmpty()) { pEdit->SetFocus(); } else { this->HandleLogin(); } } return true; } else if(wParam == VK_ESCAPE) { PostQuitMessage(0); return true; } } else if(uMsg == WM_LOGIN_SUCCESS) { LoginSuccess(); return true; } else if(uMsg == WM_LOGIN_ERROR) { LoginError(); return true; } else if(uMsg == WM_NEED_UPDATE) { //说明需要升级了 Update(); return true; } else if(uMsg == WM_LOGIN_AGAIN_OUT) { ShowLoginAgainOut(); return true; } return false; } void CLoginWnd::StartLogin() { //隐藏密码输入框,显示进度条 CVerticalLayoutUI* pInput = static_cast(m_pm.FindControl(_T("login_input"))); pInput->SetVisible(false); CVerticalLayoutUI* pLoading = static_cast(m_pm.FindControl(_T("login_loading"))); pLoading->SetVisible(true); CLabelUI* pLoginResultLabel = static_cast(m_pm.FindControl(_T("loginresult"))); pLoginResultLabel->SetVisible(false); //开启一个线程,开始处理登录 std::thread(&CLoginWnd::HandleLogin, this).detach(); } void CLoginWnd::HandleLogin() { ////真正登录前,先检测是否有需要更新 //CSystem::my_sleep(1); ////如果需要更新,那么就提示 //PostMessage(WM_NEED_UPDATE); //return; //判断账号密码是否正确 std::wstring account, password; CEditUI* pAccountEdit = static_cast(m_pm.FindControl(_T("accountedit"))); if(pAccountEdit) { account = pAccountEdit->GetText().GetData(); } CEditUI* pPasswordEdit = static_cast(m_pm.FindControl(_T("pwdedit"))); if(pPasswordEdit) { password = pPasswordEdit->GetText().GetData(); } LOG_INFO("account:" << account.c_str() << ", password:" << password.c_str()); string s_account = CLewaimaiString::UnicodeToUTF8(account); string s_password = CLewaimaiString::UnicodeToUTF8(password); CLewaimaiString::trim(s_account); CLewaimaiString::trim(s_password); CLabelUI* pLoginResultLabel = static_cast(m_pm.FindControl(_T("loginresult"))); if(s_account.compare("") == 0) { pLoginResultLabel->SetText(std::wstring(_T("用户名不能为空")).c_str()); pLoginResultLabel->SetVisible(true); PostMessage(WM_LOGIN_ERROR); return; } if(s_password.compare("") == 0) { pLoginResultLabel->SetText(std::wstring(_T("密码不能为空")).c_str()); pLoginResultLabel->SetVisible(true); PostMessage(WM_LOGIN_ERROR); return; } CZhipuziHttpClient::Init(s_account, s_password); std::string errmsg; bool res = CZhipuziHttpClient::Login(errmsg); if(res) { CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if(pAuto->IsSelected()) { CSetting::SetParam("setting_is_auto_login", "1", false); CSetting::SetParam("setting_is_remember_password", "1", false); //相当于开启自动登录,默认就是开启了记住密码了 CSetting::SetUser(s_account, s_password, true); } else { CSetting::SetParam("setting_is_auto_login", "0", false); if(pRemember->IsSelected()) { CSetting::SetParam("setting_is_remember_password", "1", false); } else { CSetting::SetParam("setting_is_remember_password", "0", false); } if(pRemember->IsSelected()) { CSetting::SetUser(s_account, s_password, true); } else { CSetting::SetUser(s_account, "", true); } } //在这里设置完参数后,统一保存到数据库 CSetting::SetParam("last_login_username", s_account, true); //把用户名和密码保存起来 CSetting::SetLoginInfo(s_account, s_password); PostMessage(WM_LOGIN_SUCCESS); return; } else { //登录失败了 CLabelUI* pLoginResultLabel = static_cast(m_pm.FindControl(_T("loginresult"))); pLoginResultLabel->SetText(std::wstring(_T("登录失败:") + CLewaimaiString::UTF8ToUnicode(errmsg)).c_str()); pLoginResultLabel->SetVisible(true); PostMessage(WM_LOGIN_ERROR); } } void CLoginWnd::LoginSuccess() { CMainWnd* pFrame = new CMainWnd(); if(pFrame == NULL) { return; } pFrame->SetIcon(IDI_ICON_DUILIB); pFrame->Create(NULL, _T("智铺子收银软件"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 1024, 768); pFrame->CenterWindow(); ::ShowWindow(*pFrame, SW_SHOWMAXIMIZED); Close(); } void CLoginWnd::LoginError() { CVerticalLayoutUI* pInput = static_cast(m_pm.FindControl(_T("login_input"))); pInput->SetVisible(true); CVerticalLayoutUI* pLoading = static_cast(m_pm.FindControl(_T("login_loading"))); pLoading->SetVisible(false); CLabelUI* pLoginResultLabel = static_cast(m_pm.FindControl(_T("loginresult"))); pLoginResultLabel->SetVisible(true); } void CLoginWnd::Update() { CUpdateWnd* pFrame = new CUpdateWnd(); if(pFrame == NULL) { return; } TCHAR lpTempPathBuffer[MAX_PATH]; DWORD dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer); std::string url = "http://down10d.zol.com.cn/zoldownload_os/nsis3.04setup@81_262627.exe"; std::string filename = CLewaimaiString::UnicodeToANSI(lpTempPathBuffer); pFrame->InitData(url, filename); pFrame->SetIcon(IDI_ICON_DUILIB); pFrame->Create(NULL, _T("自动更新"), UI_WNDSTYLE_DIALOG, 0, 0, 0, 0, 0, NULL); pFrame->CenterWindow(); ::ShowWindow(*pFrame, SW_SHOWNORMAL); Close(); } void CLoginWnd::ShowLoginAgainOut() { //这种模式是本人强制挤下线了 CMessageboxWnd* pMessagebox = new CMessageboxWnd; pMessagebox->Create(m_hWnd, _T(""), UI_WNDSTYLE_DIALOG, WS_EX_WINDOWEDGE); pMessagebox->SetIcon(IDI_ICON_DUILIB); pMessagebox->CenterWindow(); UINT ret = pMessagebox->ShowModal(); }