CLoginWnd.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include "../pch/pch.h"
  2. #include "CLoginWnd.h"
  3. void CLoginWnd::Init()
  4. {
  5. std::map<string, string> users = CSetting::getUsers();
  6. CComboUI* pCom = static_cast<CComboUI*>(m_pm.FindControl(_T("accountcombo")));
  7. for (std::map<string, string>::iterator it = users.begin(); it != users.end(); it++)
  8. {
  9. std::string username = it->first;
  10. CListLabelElementUI* elem = new CListLabelElementUI();
  11. elem->SetText(CLewaimaiString::UTF8ToUnicode(username).c_str());
  12. pCom->Add(elem);
  13. }
  14. CCheckBoxUI* pAuto = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_auto_login")));
  15. CCheckBoxUI* pRemember = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_remember_password")));
  16. if (CSetting::GetParam("setting_is_remember_password") == "1")
  17. {
  18. pRemember->Selected(true, false);
  19. }
  20. else
  21. {
  22. pRemember->Selected(false, false);
  23. }
  24. if (CSetting::GetParam("setting_is_auto_login") == "1")
  25. {
  26. //自动登录开启了,记住密码一定要开启
  27. pAuto->Selected(true, false);
  28. pRemember->Selected(true, false);
  29. }
  30. else
  31. {
  32. pAuto->Selected(false, false);
  33. }
  34. std::string last_login_username = CSetting::GetParam("last_login_username");
  35. std::string password = CSetting::GetUser(last_login_username);
  36. CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
  37. CEditUI* pPasswordEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("pwdedit")));
  38. pAccountEdit->SetText(CLewaimaiString::UTF8ToUnicode(last_login_username).c_str());
  39. pPasswordEdit->SetText(CLewaimaiString::UTF8ToUnicode(password).c_str());
  40. if (CSetting::GetParam("setting_is_auto_login") == "1")
  41. {
  42. HandleLogin();
  43. }
  44. }
  45. void CLoginWnd::Notify(TNotifyUI& msg)
  46. {
  47. if(msg.sType == _T("click"))
  48. {
  49. if(msg.pSender->GetName() == _T("closebtn"))
  50. {
  51. PostQuitMessage(0);
  52. return;
  53. }
  54. else if(msg.pSender->GetName() == _T("loginBtn"))
  55. {
  56. HandleLogin();
  57. return;
  58. }
  59. else if (msg.pSender->GetName() == _T("login_auto_login"))
  60. {
  61. CCheckBoxUI* pAuto = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_auto_login")));
  62. CCheckBoxUI* pRemember = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_remember_password")));
  63. if (!pAuto->IsSelected())
  64. {
  65. pRemember->Selected(true, false);
  66. }
  67. }
  68. else if (msg.pSender->GetName() == _T("login_remember_password"))
  69. {
  70. CCheckBoxUI* pAuto = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_auto_login")));
  71. CCheckBoxUI* pRemember = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_remember_password")));
  72. if (pRemember->IsSelected())
  73. {
  74. pAuto->Selected(false, false);
  75. }
  76. }
  77. else if (msg.pSender->GetName() == _T("guanwang"))
  78. {
  79. ShellExecute(NULL, _T("open"), _T("explorer.exe"), _T("https://www.zhipuzi.com"), NULL, SW_SHOW);
  80. }
  81. }
  82. else if (msg.sType == _T("itemselect"))
  83. {
  84. CDuiString name = msg.pSender->GetName();
  85. if (name == _T("accountcombo"))
  86. {
  87. CComboUI* pCom = static_cast<CComboUI*>(m_pm.FindControl(_T("accountcombo")));
  88. std::wstring name = pCom->GetItemAt(pCom->GetCurSel())->GetText();
  89. std::string password = CSetting::GetUser(CLewaimaiString::UnicodeToUTF8(name));
  90. std::wstring wspassword = CLewaimaiString::UTF8ToUnicode(password);
  91. CEditUI* pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
  92. pEdit->SetText(name.c_str());
  93. CEditUI* pPassword = static_cast<CEditUI*>(m_pm.FindControl(_T("pwdedit")));
  94. pPassword->SetText(wspassword.c_str());
  95. }
  96. }
  97. }
  98. LRESULT CLoginWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  99. {
  100. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  101. styleValue &= ~WS_CAPTION;
  102. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  103. // 把自己的窗口句柄与窗口绘制管理器挂接在一起
  104. m_pm.Init(m_hWnd);
  105. m_pm.AddPreMessageFilter(this);
  106. CDialogBuilder builder;
  107. CControlUI* pRoot = builder.Create(_T("login.xml"), (UINT)0, NULL, &m_pm);
  108. ASSERT(pRoot && "Failed to parse XML");
  109. // 把这些控件绘制到本窗口上
  110. m_pm.AttachDialog(pRoot);
  111. // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
  112. m_pm.AddNotifier(this);
  113. Init();
  114. return 0;
  115. }
  116. LRESULT CLoginWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  117. {
  118. bHandled = FALSE;
  119. return 0;
  120. }
  121. /*
  122. *这个是窗口被销毁的时候调用的
  123. **/
  124. LRESULT CLoginWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  125. {
  126. bHandled = FALSE;
  127. return 0;
  128. }
  129. LRESULT CLoginWnd::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  130. {
  131. if(::IsIconic(*this))
  132. {
  133. bHandled = FALSE;
  134. }
  135. return (wParam == 0) ? TRUE : FALSE;
  136. }
  137. LRESULT CLoginWnd::OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  138. {
  139. return 0;
  140. }
  141. LRESULT CLoginWnd::OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  142. {
  143. //在这里设置焦点才有用
  144. CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
  145. if (pAccountEdit)
  146. {
  147. pAccountEdit->SetFocus();
  148. }
  149. return 0;
  150. }
  151. LRESULT CLoginWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  152. {
  153. POINT pt;
  154. pt.x = GET_X_LPARAM(lParam);
  155. pt.y = GET_Y_LPARAM(lParam);
  156. ::ScreenToClient(*this, &pt);
  157. RECT rcClient;
  158. ::GetClientRect(*this, &rcClient);
  159. RECT rcCaption = m_pm.GetCaptionRect();
  160. if(pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  161. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom)
  162. {
  163. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  164. if(pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0)
  165. {
  166. return HTCAPTION;
  167. }
  168. }
  169. return HTCLIENT;
  170. }
  171. LRESULT CLoginWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  172. {
  173. SIZE szRoundCorner = m_pm.GetRoundCorner();
  174. if(!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0))
  175. {
  176. CDuiRect rcWnd;
  177. ::GetWindowRect(*this, &rcWnd);
  178. rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  179. rcWnd.right++;
  180. rcWnd.bottom++;
  181. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
  182. ::SetWindowRgn(*this, hRgn, TRUE);
  183. ::DeleteObject(hRgn);
  184. }
  185. bHandled = FALSE;
  186. return 0;
  187. }
  188. LRESULT CLoginWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  189. {
  190. LRESULT lRes = 0;
  191. BOOL bHandled = TRUE;
  192. switch(uMsg)
  193. {
  194. case WM_CREATE:
  195. lRes = OnCreate(uMsg, wParam, lParam, bHandled);
  196. break;
  197. case WM_CLOSE:
  198. lRes = OnClose(uMsg, wParam, lParam, bHandled);
  199. break;
  200. case WM_DESTROY:
  201. lRes = OnDestroy(uMsg, wParam, lParam, bHandled);
  202. break;
  203. case WM_NCACTIVATE:
  204. lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
  205. break;
  206. case WM_NCCALCSIZE:
  207. lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled);
  208. break;
  209. case WM_NCPAINT:
  210. lRes = OnNcPaint(uMsg, wParam, lParam, bHandled);
  211. break;
  212. case WM_NCHITTEST:
  213. lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
  214. break;
  215. case WM_SIZE:
  216. lRes = OnSize(uMsg, wParam, lParam, bHandled);
  217. break;
  218. default:
  219. bHandled = FALSE;
  220. }
  221. if(bHandled)
  222. {
  223. return lRes;
  224. }
  225. if(m_pm.MessageHandler(uMsg, wParam, lParam, lRes))
  226. {
  227. return lRes;
  228. }
  229. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  230. }
  231. LRESULT CLoginWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
  232. {
  233. if(uMsg == WM_KEYDOWN)
  234. {
  235. if(wParam == VK_RETURN)
  236. {
  237. CEditUI* pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
  238. if(pEdit->GetText().IsEmpty())
  239. {
  240. pEdit->SetFocus();
  241. }
  242. else
  243. {
  244. pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("pwdedit")));
  245. if(pEdit->GetText().IsEmpty())
  246. {
  247. pEdit->SetFocus();
  248. }
  249. else
  250. {
  251. this->HandleLogin();
  252. }
  253. }
  254. return true;
  255. }
  256. else if(wParam == VK_ESCAPE)
  257. {
  258. PostQuitMessage(0);
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. void CLoginWnd::HandleLogin()
  265. {
  266. //判断账号密码是否正确
  267. std::wstring account, password;
  268. CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
  269. if(pAccountEdit)
  270. {
  271. account = pAccountEdit->GetText().GetData();
  272. }
  273. CEditUI* pPasswordEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("pwdedit")));
  274. if(pPasswordEdit)
  275. {
  276. password = pPasswordEdit->GetText().GetData();
  277. }
  278. LOG_INFO("account:" << account.c_str() << ", password:" << password.c_str());
  279. string s_account = CLewaimaiString::UnicodeToUTF8(account);
  280. string s_password = CLewaimaiString::UnicodeToUTF8(password);
  281. CLewaimaiString::trim(s_account);
  282. CLewaimaiString::trim(s_password);
  283. CLabelUI* pLoginResultLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("loginresult")));
  284. if (s_account.compare("") == 0)
  285. {
  286. pLoginResultLabel->SetText(std::wstring(_T("用户名不能为空")).c_str());
  287. pLoginResultLabel->SetVisible(true);
  288. return;
  289. }
  290. if (s_password.compare("") == 0)
  291. {
  292. pLoginResultLabel->SetText(std::wstring(_T("密码不能为空")).c_str());
  293. pLoginResultLabel->SetVisible(true);
  294. return;
  295. }
  296. CZhipuziHttpClient::Init(s_account, s_password);
  297. std::string errmsg;
  298. bool res = CZhipuziHttpClient::Login(errmsg);
  299. if(res)
  300. {
  301. CCheckBoxUI* pAuto = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_auto_login")));
  302. CCheckBoxUI* pRemember = static_cast<CCheckBoxUI*>(m_pm.FindControl(_T("login_remember_password")));
  303. if (pAuto->IsSelected())
  304. {
  305. CSetting::SetParam("setting_is_auto_login", "1", false);
  306. CSetting::SetParam("setting_is_remember_password", "1", false);
  307. //相当于开启自动登录,默认就是开启了记住密码了
  308. CSetting::SetUser(s_account, s_password, true);
  309. }
  310. else
  311. {
  312. CSetting::SetParam("setting_is_auto_login", "0", false);
  313. if (pRemember->IsSelected())
  314. {
  315. CSetting::SetParam("setting_is_remember_password", "1", false);
  316. }
  317. else
  318. {
  319. CSetting::SetParam("setting_is_remember_password", "0", false);
  320. }
  321. if (pRemember->IsSelected())
  322. {
  323. CSetting::SetUser(s_account, s_password, true);
  324. }
  325. else
  326. {
  327. CSetting::SetUser(s_account, "", true);
  328. }
  329. }
  330. //在这里设置完参数后,统一保存到数据库
  331. CSetting::SetParam("last_login_username", s_account, true);
  332. //把用户名和密码保存起来
  333. CSetting::SetLoginInfo(s_account, s_password);
  334. CMainWnd* pFrame = new CMainWnd();
  335. if(pFrame == NULL)
  336. {
  337. return;
  338. }
  339. pFrame->SetIcon(IDI_ICON_DUILIB);
  340. pFrame->Create(NULL, _T("智铺子收银软件"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 1024, 768);
  341. pFrame->CenterWindow();
  342. ::ShowWindow(*pFrame, SW_SHOWMAXIMIZED);
  343. Close();
  344. }
  345. else
  346. {
  347. //登录失败了
  348. CLabelUI* pLoginResultLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("loginresult")));
  349. pLoginResultLabel->SetText(std::wstring(_T("登录失败:") + CLewaimaiString::UTF8ToUnicode(errmsg)).c_str());
  350. pLoginResultLabel->SetVisible(true);
  351. }
  352. }