CLoginWnd.cpp 16 KB

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