LoginFrm.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. {*******************************************************************************
  2. 单元名称: ScLoginFrm.pas
  3. 单元说明: 网络版PHP Web页面登录方式。
  4. 作者时间: Chenshilong, 2012-5-13
  5. *******************************************************************************}
  6. unit LoginFrm;
  7. interface
  8. uses
  9. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  10. Dialogs, JimCombos, ExtCtrls, StdCtrls, cxLookAndFeelPainters,
  11. cxTextEdit, cxButtons, cxControls, cxEdit,
  12. cxMaskEdit, cxDropDownEdit, HookEdit, cxContainer, XPMenu, cslLabel,
  13. IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  14. CslButton, OleCtrls, SHDocVw, DB, ADODB, Menus;
  15. type
  16. TLoginFrm = class(TForm)
  17. edtPW: TEdit;
  18. lblForgetPW: TcslLabel;
  19. lblVer: TLabel;
  20. shpAccount: TShape;
  21. imgBG: TImage;
  22. btnLogin: TCslButton;
  23. lblReg: TCslLabel;
  24. btnClose: TCslButton;
  25. lblProductName: TLabel;
  26. shpPW: TShape;
  27. wbLogin: TWebBrowser;
  28. pnlLocalServer: TPanel;
  29. btnLocalLogin: TCslButton;
  30. lblLocalHelp: TCslLabel;
  31. lblLocalServer: TCslLabel;
  32. cbUser: TcxComboBox;
  33. pmLogin: TPopupMenu;
  34. nClearAccount: TMenuItem;
  35. lblHint: TLabel;
  36. procedure edtPWKeyDown(Sender: TObject; var Key: Word;
  37. Shift: TShiftState);
  38. procedure FormCreate(Sender: TObject);
  39. procedure btnLoginClick(Sender: TObject);
  40. procedure img1Click(Sender: TObject);
  41. procedure shpAccountMouseDown(Sender: TObject; Button: TMouseButton;
  42. Shift: TShiftState; X, Y: Integer);
  43. procedure shpPWMouseDown(Sender: TObject; Button: TMouseButton;
  44. Shift: TShiftState; X, Y: Integer);
  45. procedure btnCloseClick(Sender: TObject);
  46. procedure FormDestroy(Sender: TObject);
  47. procedure cbUserKeyDown(Sender: TObject; var Key: Word;
  48. Shift: TShiftState);
  49. procedure cbUserClick(Sender: TObject);
  50. procedure nClearAccountClick(Sender: TObject);
  51. private
  52. { Private declarations }
  53. FUsersCon: TADOConnection;
  54. FUsersQry: TADOQuery;
  55. procedure ReadCloudUser;
  56. procedure WriteCloudUser;
  57. public
  58. { Public declarations }
  59. end;
  60. function LoginForm: Boolean;
  61. implementation
  62. uses
  63. ScUtils, ShellAPI, IniFiles, ScConfig, Md5, PHPWebDm, ConstUnit;
  64. {$R *.dfm}
  65. function LoginForm: Boolean;
  66. var
  67. Form: TLoginFrm;
  68. begin
  69. Result := False;
  70. Form := TLoginFrm.Create(nil);
  71. try
  72. if Form.ShowModal = mrOK then
  73. begin
  74. Result := True;
  75. Form.WriteCloudUser;
  76. end;
  77. finally
  78. Form.Free;
  79. end;
  80. end;
  81. { TLoginFrm }
  82. procedure TLoginFrm.edtPWKeyDown(Sender: TObject; var Key: Word;
  83. Shift: TShiftState);
  84. begin
  85. if Key = VK_Return then
  86. btnLoginClick(Sender);
  87. end;
  88. procedure TLoginFrm.FormCreate(Sender: TObject);
  89. var SL: TStringList;
  90. ini: TIniFile;
  91. begin
  92. lblHint.Caption := '';
  93. SetClassLong(Handle, GCL_STYLE, GetClassLong(Handle, GCL_STYLE) or CS_DROPSHADOW);
  94. Set8087CW(Default8087CW or $0004);
  95. Set8087CW(Longword($133f));
  96. ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Cloud.ini');
  97. try
  98. G_Server := ini.ReadString('URL', 'Server', '');
  99. G_ServerType := ini.ReadString('URL', 'ServerType', '');
  100. G_CompanyName := ini.ReadString('URL', 'CompanyName', '');
  101. G_MeasureURL := 'http://' + G_Server + ini.ReadString('URL', 'MeasureURL', '');
  102. finally
  103. ini.Free;
  104. end;
  105. if Trim(G_CompanyName) <> '' then
  106. lblProductName.Caption := G_CompanyName
  107. else
  108. lblProductName.Caption := G_ProductName;
  109. // 服务器:1公有 2私有
  110. if G_ServerType = '2' then
  111. begin
  112. pnlLocalServer.Visible := True;
  113. lblLocalServer.URL := G_Server;
  114. lblLocalServer.Hint := G_Server;
  115. end
  116. else if G_ServerType = '1' then
  117. begin
  118. pnlLocalServer.Visible := False;
  119. end;
  120. lblReg.URL := PHPWeb.RegURL;
  121. lblForgetPW.URL := PHPWeb.PwdURL;
  122. lblVer.Caption := G_ProductName + ' ' + ScGetVersion;
  123. // lblVer.Left := lblProductName.Left + lblProductName.Width + 7;
  124. ReadCloudUser;
  125. SL := TStringList.Create;
  126. try
  127. SL.Clear;
  128. SL.Add('about: <body><font size=4 color=Green>') ;
  129. SL.Add('正在打开网页请稍候...');
  130. SL.Add('</font></body>');
  131. wbLogin.Navigate(SL.Text);
  132. wbLogin.Navigate(PHPWeb.LoginBannerURL);
  133. finally
  134. end;
  135. end;
  136. procedure TLoginFrm.btnLoginClick(Sender: TObject);
  137. var sInfo, sURL, sCheckCode, sMD5PW: string;
  138. begin
  139. lblHint.Caption := '';
  140. lblHint.Update;
  141. if Trim(cbUser.Text) = '' then
  142. begin
  143. lblHint.Caption := '请您输入账号后再登录';
  144. lblHint.Update;
  145. cbUser.SetFocus;
  146. Exit;
  147. end;
  148. if Trim(edtPW.Text) = '' then
  149. begin
  150. lblHint.Caption := '请您输入密码后再登录';
  151. lblHint.Update;
  152. edtPW.SetFocus;
  153. Exit;
  154. end;
  155. case PHPWeb.Login(cbUser.Text, edtPW.Text, sInfo, sCheckCode) of
  156. ltCon:
  157. begin
  158. if Trim(sInfo) <> '' then
  159. Application.MessageBox(PChar(sInfo), '新版本提示', MB_OK + MB_ICONINFORMATION);
  160. ModalResult := mrOk;
  161. end;
  162. ltLoginFail:
  163. begin
  164. lblHint.Caption := sInfo;
  165. lblHint.Update;
  166. edtPW.SetFocus;
  167. edtPW.SelectAll;
  168. ModalResult := mrNone;
  169. end;
  170. ltUpdate:
  171. begin
  172. // 无法对PHP返回的字符串进行排版。这里使用Delphi自身的字符串。
  173. sInfo := '尊敬的用户:' + #13#13 +
  174. '系统检测出您是通过SmartCost旧版本程序注册了本帐户(' + Trim(cbUser.Text) +
  175. '),因系统升级,为保障您的帐户安全和更好的为您服务,' + #13#13 +
  176. '我们需要对您的邮箱进行有效性验证,并需要您重置该帐户的密码以保证您的帐号在新程序上能够正常使用。' + #13#13 +
  177. '请点击“确定”按钮打开密码重置页面。';
  178. Application.MessageBox(PChar(sInfo), '升级提示', MB_OK + MB_ICONINFORMATION);
  179. edtPW.SetFocus;
  180. edtPW.SelectAll;
  181. ModalResult := mrNone;
  182. Screen.Cursor := crHourGlass;
  183. try
  184. sURL := ConfigInfo.OldUserResetPwdURL + '?CheckCode=' + sCheckCode;
  185. ShellExecute(Application.Handle, 'open', PChar(sURL), nil, nil, SW_SHOWNORMAL);
  186. finally
  187. Screen.Cursor := crDefault;
  188. end;
  189. end;
  190. ltIncomplete:
  191. begin
  192. Application.MessageBox(PChar(sInfo), '系统提示', MB_OK + MB_ICONINFORMATION);
  193. ModalResult := mrNone;
  194. Screen.Cursor := crHourGlass;
  195. try
  196. sMD5PW := GetMD5(edtPW.Text);
  197. sURL := Format('%s?Account=%s&Pwd=%s', [ConfigInfo.WebLoginURL,
  198. cbUser.Text, sMD5PW]);
  199. ShellExecute(Application.Handle, 'open', PChar(sURL), nil, nil, SW_SHOWNORMAL);
  200. finally
  201. Screen.Cursor := crDefault;
  202. end;
  203. end;
  204. ltDisCon:
  205. begin
  206. Application.MessageBox('网络错误,请稍后重试!', '操作提醒', MB_OK + MB_ICONWARNING);
  207. ModalResult := mrNone;
  208. end;
  209. end;
  210. end;
  211. procedure TLoginFrm.img1Click(Sender: TObject);
  212. begin
  213. Close;
  214. end;
  215. procedure TLoginFrm.shpAccountMouseDown(Sender: TObject; Button: TMouseButton;
  216. Shift: TShiftState; X, Y: Integer);
  217. begin
  218. cbUser.SetFocus;
  219. end;
  220. procedure TLoginFrm.shpPWMouseDown(Sender: TObject; Button: TMouseButton;
  221. Shift: TShiftState; X, Y: Integer);
  222. begin
  223. edtPW.SetFocus;
  224. end;
  225. procedure TLoginFrm.btnCloseClick(Sender: TObject);
  226. begin
  227. Close;
  228. end;
  229. procedure TLoginFrm.FormDestroy(Sender: TObject);
  230. begin
  231. FUsersCon.Close;
  232. FUsersCon.Free;
  233. FUsersQry.Free;
  234. end;
  235. procedure TLoginFrm.ReadCloudUser;
  236. var s: string;
  237. begin
  238. FUsersCon := TADOConnection.Create(nil);
  239. FUsersCon.LoginPrompt := False;
  240. s := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=CloudUser.dat;Persist Security Info=False';
  241. FUsersCon.ConnectionString := s;
  242. FUsersQry := TADOQuery.Create(nil);
  243. FUsersQry.Connection := FUsersCon;
  244. FUsersQry.SQL.Text := 'Select Top 15 * From Users Order by LoginTimes Desc';
  245. FUsersQry.Open;
  246. cbUser.Properties.Items.Clear;
  247. if FUsersQry.RecordCount > 0 then
  248. begin
  249. FUsersQry.First;
  250. while not FUsersQry.Eof do
  251. begin
  252. cbUser.Properties.Items.Add(FUsersQry.FieldByName('Account').AsString);
  253. FUsersQry.Next;
  254. end;
  255. end;
  256. FUsersQry.Close;
  257. end;
  258. procedure TLoginFrm.WriteCloudUser;
  259. begin
  260. FUsersQry.Close;
  261. FUsersQry.SQL.Text := Format('Select * From Users where UserID = %d', [PHPWeb.UserID]);
  262. FUsersQry.Open;
  263. if FUsersQry.RecordCount > 0 then
  264. begin
  265. FUsersQry.Close;
  266. FUsersQry.SQL.Text := Format('Update Users Set LoginTimes=LoginTimes+1 where UserID = %d', [PHPWeb.UserID]);
  267. FUsersQry.ExecSQL;
  268. end
  269. else
  270. begin
  271. FUsersQry.Close;
  272. FUsersQry.SQL.Text := Format('Insert into Users(UserID,Account,RealName,LoginTimes) Values(%d,''%s'',''%s'',1)',
  273. [PHPWeb.UserID, PHPWeb.Account, PHPWeb.RealName]);
  274. FUsersQry.ExecSQL;
  275. end;
  276. FUsersQry.Close;
  277. end;
  278. procedure TLoginFrm.cbUserKeyDown(Sender: TObject; var Key: Word;
  279. Shift: TShiftState);
  280. begin
  281. if Key = VK_Return then
  282. begin
  283. edtPW.SetFocus;
  284. edtPW.SelectAll;
  285. end;
  286. end;
  287. procedure TLoginFrm.cbUserClick(Sender: TObject);
  288. // For Test Quickly. chenshilong, 2015-09-30
  289. //const
  290. // AccArr: array[1..11, 1..3] of string = (
  291. // ('1', '2636698008@qq.com', 'smartcost3850887'), // 纵横销售演示服务器
  292. // ('2', '1971614655@qq.com', '357134933..'),
  293. // ('3', '1835082984@qq.com', '123456'),
  294. // ('4', '2417587264@qq.com', 'koukou0708'),
  295. // ('5', '2609827960@qq.com', 'missling'),
  296. // ('6', '1240621850@qq.com', '87654321'),
  297. // ('7', '1014149875@qq.com', 'zhbwoai'),
  298. // ('8', '1525739553@qq.com', '123456'),
  299. // ('9', '1391010261@qq.com', 'missling'),
  300. // ('10', '916960227@qq.com', '123456'),
  301. // ('11', '916960227@qq.com', '12345678') // 测试服务器
  302. // );
  303. //
  304. //var
  305. // i: Integer;
  306. // bEx: Boolean;
  307. begin
  308. // if G_IsTest then
  309. // begin
  310. // bEx := False;
  311. // for i := Low(AccArr) to High(AccArr) do
  312. // begin
  313. // if AccArr[i, 1] = cbUser.Text then
  314. // begin
  315. // cbUser.Text := AccArr[i, 2];
  316. // edtPW.Text := AccArr[i, 3];
  317. // bEx := True;
  318. // Break;
  319. // end;
  320. // end;
  321. // if not bEx then
  322. // begin
  323. // cbUser.Clear;
  324. // edtPW.Clear;
  325. // end;
  326. // end;
  327. end;
  328. procedure TLoginFrm.nClearAccountClick(Sender: TObject);
  329. begin
  330. if Application.MessageBox('确定要清空所有记录的帐户信息吗?', '询问', MB_YESNO + MB_ICONQUESTION) = ID_No then
  331. Exit;
  332. FUsersQry.Close;
  333. FUsersQry.SQL.Text := 'Delete * From Users';
  334. FUsersQry.ExecSQL;
  335. FUsersQry.Close;
  336. cbUser.Properties.Items.Clear;
  337. end;
  338. end.