dyAuth.dpr 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. library dyAuth;
  2. { Important note about DLL memory management: ShareMem must be the
  3. first unit in your library's USES clause AND your project's (select
  4. Project-View Source) USES clause if your DLL exports any procedures or
  5. functions that pass strings as parameters or function results. This
  6. applies to all strings passed to and from your DLL--even those that
  7. are nested in records and classes. ShareMem is the interface unit to
  8. the BORLNDMM.DLL shared memory manager, which must be deployed along
  9. with your DLL. To avoid using BORLNDMM.DLL, pass string information
  10. using PChar or ShortString parameters.
  11. Important note about VCL usage: when this DLL will be implicitly
  12. loaded and this DLL uses TWicImage / TImageCollection created in
  13. any unit initialization section, then Vcl.WicImageInit must be
  14. included into your library's USES clause. }
  15. uses
  16. System.SysUtils,
  17. System.Classes,
  18. IdHTTP,
  19. IdSSLOpenSSL,
  20. DateUtils,
  21. Neon.Core.Attributes in 'Neon.Core.Attributes.pas',
  22. Neon.Core.DynamicTypes in 'Neon.Core.DynamicTypes.pas',
  23. Neon.Core.Nullables in 'Neon.Core.Nullables.pas',
  24. Neon.Core.Persistence.JSON in 'Neon.Core.Persistence.JSON.pas',
  25. Neon.Core.Persistence in 'Neon.Core.Persistence.pas',
  26. Neon.Core.Persistence.Swagger in 'Neon.Core.Persistence.Swagger.pas',
  27. Neon.Core.Serializers.DB in 'Neon.Core.Serializers.DB.pas',
  28. Neon.Core.Serializers.RTL in 'Neon.Core.Serializers.RTL.pas',
  29. Neon.Core.Serializers.VCL in 'Neon.Core.Serializers.VCL.pas',
  30. Neon.Core.TypeInfo in 'Neon.Core.TypeInfo.pas',
  31. Neon.Core.Types in 'Neon.Core.Types.pas',
  32. Neon.Core.Utils in 'Neon.Core.Utils.pas',
  33. U_CommonData in 'U_CommonData.pas';
  34. {$R *.res}
  35. function UnixMillisecondsToTDateTime(UnixTimeStampMS: Int64): TDateTime;
  36. begin
  37. // 将毫秒转换为秒,然后使用标准转换函数
  38. Result := UnixToDateTime(UnixTimeStampMS div 1000);
  39. // 添加剩余的毫秒部分
  40. // TDateTime中1天=86400秒,1秒=1000毫秒
  41. // 所以1毫秒 = 1/86400000 天
  42. Result := Result + (UnixTimeStampMS mod 1000) / 86400000.0;
  43. // 备注:也可以使用完整公式直接转换
  44. // Result := UnixTimeStampMS / 86400000.0 + 25569.0;
  45. end;
  46. function InternalPerformHttpPost(const AUrl: string; const printerUniqueCode: string; out AResponse: string): Boolean;
  47. var
  48. HTTP: TIdHTTP;
  49. RequestList: TStringList; // 请求信息
  50. begin
  51. Result := False;
  52. AResponse := '';
  53. HTTP := TIdHTTP.Create(nil);
  54. RequestList := TStringList.Create;
  55. try
  56. // 配置HTTP组件
  57. HTTP.HTTPOptions := HTTP.HTTPOptions + [hoForceEncodeParams];
  58. HTTP.Request.UserAgent := 'Delphi HTTP Client DLL';
  59. RequestList.Add('printerUniqueCode=' + printerUniqueCode);
  60. try
  61. AResponse := HTTP.Post(AUrl, RequestList);
  62. Result := True;
  63. except
  64. on E: Exception do
  65. AResponse := 'HTTP POST Error: ' + E.Message;
  66. end;
  67. finally
  68. HTTP.Free;
  69. RequestList.Free;
  70. end;
  71. end;
  72. function CheckPrinterAuth(const printerUniqueCode: PWideChar; out AResponse: TCheckResult): Boolean; stdcall;
  73. var
  74. UrlStr, ResponseStr: string;
  75. ResByteArray: TByteArray;
  76. Response: THttpResponse;
  77. printerClient: TPrinterClient;
  78. ch: String;
  79. index: Integer;
  80. begin
  81. Result := False;
  82. UrlStr := 'http://192.168.1.44:18652/apis0/authorize/checkPrinterPerms';
  83. if InternalPerformHttpPost(UrlStr, string(printerUniqueCode), ResponseStr) then begin
  84. Response := TNeon.JSONToObject<THttpResponse>(ResponseStr);
  85. try
  86. if Response.httpCode = 200 then begin
  87. printerClient := TNeon.JSONToObject<TPrinterClient>(Response.data);
  88. try
  89. AResponse.expireDate := printerClient.authorizeDateLong;
  90. AResponse.remainDays := DaysBetween(Now, UnixMillisecondsToTDateTime(AResponse.expireDate));
  91. index := 0;
  92. for ch in printerClient.permissions do begin
  93. AResponse.permissions[index] := StrToInt(ch);
  94. Inc(index);
  95. end;
  96. finally
  97. printerClient.Free;
  98. end;
  99. end
  100. else begin
  101. Result := False;
  102. Exit;
  103. end;
  104. Result := True;
  105. finally
  106. Response.Free;
  107. end;
  108. end
  109. else begin
  110. Result := False;
  111. end;
  112. end;
  113. function TestExport(const i: Integer): TCheckResult; stdcall;
  114. begin
  115. Result.remainDays := i;
  116. Result.expireDate := 1000;
  117. Result.permissions[0] := 0;
  118. Result.permissions[1] := 1;
  119. Result.permissions[2] := 2;
  120. Result.permissions[3] := 3;
  121. Result.permissions[4] := 4;
  122. end;
  123. // 导出函数列表
  124. exports
  125. CheckPrinterAuth, TestExport;
  126. begin
  127. WriteLn('Testing HTTP Client DLL...');
  128. // 测试URL,可替换为其他有效地址
  129. var
  130. Url := 'http://192.168.1.44:18652/apis0/authorize/checkPrinterPerms';
  131. var
  132. uniqueCode := 'aaa';
  133. var
  134. Response: string;
  135. if InternalPerformHttpPost(Url, uniqueCode, Response) then begin
  136. WriteLn('Success:');
  137. WriteLn(Response);
  138. end
  139. else begin
  140. WriteLn('Failed:');
  141. WriteLn(Response);
  142. end;
  143. WriteLn('Press Enter to exit...');
  144. ReadLn; // 等待用户按键,以便查看输出
  145. end.