CLoginWnd.cpp 11 KB

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