CLoginWnd.cpp 14 KB

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