PHPWebDm.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. {*******************************************************************************
  2. 单元名称: PHPWebDm.pas
  3. 单元说明: Delphi 同PHP web服务器通信接口。 计量的PHP比造价的PHP多了很多
  4. 作者时间: Chenshilong, 2012-5-13
  5. *******************************************************************************}
  6. unit PHPWebDm;
  7. interface
  8. uses
  9. SysUtils, Classes, IdBaseComponent, IdComponent, IdTCPConnection,
  10. IdTCPClient, IdHTTP, CslJson;
  11. type
  12. // Web连接成功、Web连接失败、登录失败、旧用户需要升级、资料不全
  13. TLoginType = (ltCon, ltDisCon, ltLoginFail, ltUpdate, ltIncomplete);
  14. TPHPWeb = class(TDataModule)
  15. IdHTTP: TIdHTTP;
  16. procedure DataModuleCreate(Sender: TObject);
  17. private
  18. // 远程服务器上时间
  19. FServerDateTime: TDateTime;
  20. FUserID: Integer;
  21. FAccount: string;
  22. FRealName: string;
  23. FUserFlag: Integer; // 用户标志位,用于区分出一部分用户进行特殊控制。
  24. FMeasureURL: string; // http://d.jl.smartcost.com.cn/api/client/
  25. FLoginURL: string;
  26. FLoginBannerURL: string;
  27. FCheckOnLineURL: string;
  28. FPassportURL: string;
  29. FPwdURL: string;
  30. FRegURL: string;
  31. FWebLoginURL: string;
  32. FServerDateTimeURL: string;
  33. FWebSoftURL: string;
  34. FLogoutURL: string;
  35. FRole: string;
  36. FCompany: string;
  37. FUserImageURL: string;
  38. FLoginCloudURL: string;
  39. procedure SetUserID(const Value: Integer);
  40. procedure SetAccount(const Value: string);
  41. procedure SetRealName(const Value: string);
  42. {-------------------------------------------------------------------------------
  43. 方法: CustomSearch()
  44. 描述: 通用的查询接口,集各种复杂情况之大成,宇宙级牛B综合算法,哦耶! ^__^
  45. 【函数返回值】
  46. -1: 网络不通;
  47. 0: 网络通,但登录或查询出错、网页出错等,无法返回约定的
  48. Json格式,通常返回一篇乱码;
  49. 1: 成功(包括返回零记录)
  50. 【参数】
  51. AURL: 请求的网址。
  52. AInFields: 请求的键名字。
  53. AInValues: 请求的键值。
  54. AResultType: 返回值类型:0 返回一维数组;1 返回二维数组;2 两者都。
  55. AOutStrs: 用于存储返回的零散值。需要赋字段名传入。
  56. AOutRecords: 用于存储返回的多条记录、数据表。只需声明,无需定义大
  57. 小和赋值,返回Json的数组,数组的值为'info'对象的值。
  58. 【要求】
  59. ①返回的Json数据中必须有status、msg两个键名字(约定格式)。
  60. ②AResults至少有一个元素,用来存储当返回值为0时查询出错的原因。
  61. 作者: Chenshilong, 2014-07-13
  62. -------------------------------------------------------------------------------}
  63. function CustomSearch(AURL: string; AInFields, AInValues: array of string;
  64. AResultType: Integer; var AOutStrs: array of string;
  65. var AOutRecords: TOVArr): Integer;
  66. public
  67. function SystemDateTime: TDateTime;
  68. function Search(AURL: string; AInFields, AInValues: array of string; var AOutStrs: array of string): Integer; overload;
  69. function Search(AURL: string; AInFields, AInValues: array of string; var AOutRecords: TOVArr): Integer; overload;
  70. function Search(AURL: string; AInFields, AInValues: array of string; var AOutStrs: array of string ; var AOutRecords: TOVArr): Integer; overload;
  71. function Search(AURL: string; AInFields, AInValues: array of string; AResultType: Integer; var AOutStrs: array of string ; var AOutRecords: TOVArr): Integer; overload;
  72. // ALoginType, 登录类型,1 正常情况下的用户名密码;2 紫光平台调用仅用户名无密码
  73. function Login(AAccount, APW: string; ALoginType: Integer; var AInfo, ACheckCode: string): TLoginType;
  74. function ConnectServer(AIP: string; var ACompanyName: string): Integer; // 新装软件,先弹出设置IP窗口
  75. function UpDataFile(AUserID, ATenderID, APhaseNo: Integer; AFile, AMD5_JL: string;
  76. AIsSubmit: Boolean; var AResultStr: string; ACheckPassed: Boolean; ACheckerMemo: string): Boolean;
  77. function UpAttachmentFile(AUperID, AWebID, ABillID: Integer; AFile,
  78. ACategory, AMemo, APhaseName: string; var ANewFileName: string): Boolean;
  79. function GetAttachmentFileList(AWebID: Integer; var vArr: TOVArr): Boolean;
  80. function zip(AFileArr: array of string): string;
  81. function CheckZip(AZipFile: string; AFileCount: Integer): Boolean; // 检测zip是否能够正确解压出所有文件。
  82. function TempName(ALength: Integer = 12): string;
  83. function WebPath: string;
  84. function UserPath: string;
  85. // 下载文件:源文件网址;存储到本地的位置
  86. function DownFile(ASourceURL: string; var ALocalFile: string): Boolean;
  87. // 用于调试输出值,解决灵异现象(有些现象调试环境正常,运行环境时有时无)
  88. procedure Debug(AFileName, AStr: string); overload;
  89. procedure Debug(AFileName: string; AInFields, AInValues: array of string); overload;
  90. function NetError(AMid: string): string;
  91. function PageError(AMid: string): string;
  92. function ExistInServer(AWebID: Integer): Boolean;
  93. function GetNameFromURLProtocol(AURLProtocol: string): string;
  94. property Account: string read FAccount write SetAccount;
  95. property UserID: Integer read FUserID write SetUserID;
  96. property RealName: string read FRealName write SetRealName;
  97. property UserFlag: Integer read FUserFlag write FUserFlag;
  98. property Company: string read FCompany write FCompany;
  99. property Role: string read FRole write FRole;
  100. property MeasureURL: string read FMeasureURL;
  101. property WebSoftURL: string read FWebSoftURL;
  102. property PassportURL: string read FPassportURL;
  103. property LoginURL: string read FLoginURL;
  104. property WebLoginURL: string read FWebLoginURL;
  105. property LoginBannerURL: string read FLoginBannerURL;
  106. property LogoutURL: string read FLogoutURL;
  107. property RegURL: string read FRegURL;
  108. property PwdURL: string read FPwdURL;
  109. property CheckOnLineURL: string read FCheckOnLineURL;
  110. property ServerDateTimeURL: string read FServerDateTimeURL;
  111. property UserImageURL: string read FUserImageURL write FUserImageURL;
  112. property LoginCloudURL: string read FLoginCloudURL write FLoginCloudURL;
  113. end;
  114. function PHPWeb: TPHPWeb;
  115. // 全国 广东 标后预算 定额排版 港口水工 内河航运 疏浚 清单编制
  116. function ExeCategoryName: string;
  117. implementation
  118. uses
  119. MD5Unit, IdMultipartFormData, VCLUnZip, VCLZip,
  120. IniFiles, Forms, UtilMethods, Variants, ConstUnit{, Controls, Windows};
  121. var g_PHPWeb: TPHPWeb;
  122. {$R *.dfm}
  123. { TPHPWeb }
  124. function PHPWeb: TPHPWeb;
  125. begin
  126. if g_PHPWeb = nil then
  127. g_PHPWeb := TPHPWeb.Create(nil);
  128. Result := g_PHPWeb;
  129. end;
  130. function ExeCategoryName: string;
  131. begin
  132. Result := 'JLZF';
  133. end;
  134. function TPHPWeb.Login(AAccount, APW: string; ALoginType: Integer; var AInfo, ACheckCode: string): TLoginType;
  135. var vArr: array of string;
  136. begin
  137. vArr := VarArrayOf(['uid', 'name', 'email', 'ucompany', 'jobtitle', 'avatar', 'msg']);
  138. case Search(FMeasureURL + 'signin', ['v3name', 'v3pass', 'logintype'], [AnsiToUtf8(AAccount), APW, IntToStr(ALoginType)], vArr) of
  139. -1: Result := ltDisCon;
  140. 0:
  141. begin
  142. AInfo := vArr[0];
  143. Result := ltLoginFail;
  144. end;
  145. 1:
  146. begin
  147. FUserID := StrToInt(vArr[0]);
  148. FRealName := vArr[1];
  149. FAccount := AAccount; // vArr[2]
  150. FCompany := vArr[3];
  151. FRole := vArr[4];
  152. FUserImageURL := vArr[5];
  153. AInfo := vArr[6];
  154. Result := ltCon;
  155. end;
  156. end;
  157. end;
  158. procedure TPHPWeb.SetUserID(const Value: Integer);
  159. begin
  160. FUserID := Value;
  161. end;
  162. procedure TPHPWeb.SetAccount(const Value: string);
  163. begin
  164. FAccount := Value;
  165. end;
  166. procedure TPHPWeb.DataModuleCreate(Sender: TObject);
  167. var ini: TIniFile;
  168. begin
  169. FServerDateTime := -1;
  170. ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Cloud.ini');
  171. try
  172. // FMeasureURL := ini.ReadString('URL', 'MeasureURL', '');
  173. FMeasureURL := 'http://' + ini.ReadString('URL', 'Server', '') +
  174. ini.ReadString('URL', 'MeasureURL', '');
  175. FWebSoftURL := ini.ReadString('URL', 'WebSoftURL', '');
  176. FPassportURL := ini.ReadString('URL', 'PassportURL', '');
  177. FLoginCloudURL := ini.ReadString('URL', 'LoginCloudURL', '');
  178. FLoginURL := FWebSoftURL + ini.ReadString('URL', 'LoginURL', '');
  179. FWebLoginURL := FWebSoftURL + ini.ReadString('URL', 'FWebLoginURL', '');
  180. FLoginBannerURL := FWebSoftURL + ini.ReadString('URL', 'LoginBannerURL', '');
  181. FLogoutURL := FWebSoftURL + ini.ReadString('URL', 'LogoutURL', '');
  182. FRegURL := FPassportURL + ini.ReadString('URL', 'RegURL', '');
  183. FPwdURL := FPassportURL + ini.ReadString('URL', 'PwdURL', '');
  184. FCheckOnLineURL := FWebSoftURL + ini.ReadString('URL', 'CheckOnLineURL', '');
  185. FServerDateTimeURL := FWebSoftURL + ini.ReadString('URL', 'ServerDateTimeURL', '');
  186. finally
  187. ini.Free;
  188. end;
  189. end;
  190. procedure TPHPWeb.SetRealName(const Value: string);
  191. begin
  192. FRealName := Value;
  193. end;
  194. function TPHPWeb.zip(AFileArr: array of string): string;
  195. var vZip: TVCLZip;
  196. sAppFile, sZipFile: string;
  197. i: Integer;
  198. begin
  199. Result := 'Error';
  200. sAppFile := AFileArr[Low(AFileArr)];
  201. sZipFile := ExtractFilePath(sAppFile) + ExtractFileNameWithoutExt(sAppFile) + '.up';
  202. if FileExists(sZipFile) then
  203. DeleteFile(sZipFile);
  204. vZip := TVCLZip.Create(nil);
  205. try
  206. for i := Low(AFileArr) to High(AFileArr) do
  207. begin
  208. if FileExists(AFileArr[i]) then
  209. vZip.FilesList.Add(AFileArr[i]);
  210. end;
  211. vZip.ZipName := sZipFile;
  212. // PHP不支持解压加密zip文件
  213. //vZip.Password := 'jlzf.Sc.2014';
  214. vZip.Recurse := True;
  215. vZip.ZipComment := '纵横计量支付';
  216. vZip.OverwriteMode := Always;
  217. vZip.Zip;
  218. Result := sZipFile;
  219. for i := Low(AFileArr) to High(AFileArr) do
  220. DeleteFile(AFileArr[i]);
  221. finally
  222. vZip.Free;
  223. end;
  224. end;
  225. function TPHPWeb.CheckZip(AZipFile: string; AFileCount: Integer): Boolean;
  226. var vUnZip: TVCLUnZip;
  227. begin
  228. vUnZip := TVCLUnZip.Create(nil);
  229. try
  230. vUnZip.ZipName := AZipFile;
  231. vUnZip.DestDir := 'C:\Temp\MeasureTemp\';
  232. vUnZip.DoAll := True;
  233. vUnZip.RecreateDirs := True;
  234. vUnZip.RetainAttributes := True;
  235. vUnZip.OverwriteMode := Always;
  236. Result := (AFileCount = vUnZip.UnZip);
  237. finally
  238. DeleteFolder(vUnZip.DestDir);
  239. vUnZip.Free;
  240. end;
  241. end;
  242. function TPHPWeb.UpDataFile(AUserID, ATenderID, APhaseNo: Integer; AFile, AMD5_JL: string;
  243. AIsSubmit: Boolean; var AResultStr: string; ACheckPassed: Boolean; ACheckerMemo: string): Boolean;
  244. var
  245. IdDataStream: TIdMultiPartFormDataStream;
  246. sResult, sURL: string;
  247. J: TCslJson;
  248. sZipMD5: string;
  249. begin
  250. Result := False;
  251. if not FileExists(AFile) then Exit;
  252. IdDataStream := TIdMultiPartFormDataStream.Create;
  253. try
  254. if ACheckPassed then // 审核通过
  255. begin
  256. if AIsSubmit then
  257. begin
  258. sURL := Format('%speriod/%d/%d/%d/creat', [FMeasureURL, AUserID, ATenderID, APhaseNo])
  259. end
  260. else
  261. begin
  262. sURL := FMeasureURL + 'user/audit/measure';
  263. IdDataStream.AddFormField('userid', IntToStr(AUserID));
  264. IdDataStream.AddFormField('tenderid', IntToStr(ATenderID));
  265. IdDataStream.AddFormField('phaseno', IntToStr(APhaseNo));
  266. IdDataStream.AddFormField('CheckerMemo', ReplaceCharsForJson(ACheckerMemo));
  267. end;
  268. end
  269. else // 审核不通过
  270. begin
  271. sURL := Format('%suser/set/%d/%d/%d/checkno', [FMeasureURL, AUserID, ATenderID, APhaseNo]);
  272. IdDataStream.AddFormField('CheckerMemo', ReplaceCharsForJson(ACheckerMemo));
  273. end;
  274. IdDataStream.AddFile('upfile', AFile, 'text/plain');
  275. IdDataStream.AddFormField('upfile', AFile);
  276. IdDataStream.AddFormField('MD5_JL', AMD5_JL);
  277. sZipMD5 := MD5_File(AFile);
  278. IdDataStream.AddFormField('MD5_Zip', sZipMD5);
  279. IdDataStream.Position := 0;
  280. try
  281. sResult := IdHTTP.Post(sURL, IdDataStream);
  282. except
  283. Abort;
  284. end;
  285. finally
  286. IdDataStream.Free;
  287. DeleteFile(AFile);
  288. end;
  289. J := TCslJson.Create;
  290. try
  291. J.Text := Utf8ToAnsi(sResult);
  292. if SameText(J.Value['status'], 'true') then
  293. Result := True
  294. else
  295. begin
  296. Result := False;
  297. AResultStr := J.Value['msg'];
  298. end;
  299. finally
  300. J.Free;
  301. end;
  302. end;
  303. function TPHPWeb.Search(AURL: string; AInFields, AInValues: array of string; var AOutStrs: array of string): Integer;
  304. var vA: TOVArr;
  305. begin
  306. Result := CustomSearch(AURL, AInFields, AInValues, 0, AOutStrs, vA);
  307. end;
  308. function TPHPWeb.Search(AURL: string; AInFields, AInValues: array of string; var AOutRecords: TOVArr): Integer;
  309. var vA: array of string;
  310. begin
  311. Result := CustomSearch(AURL, AInFields, AInValues, 1, vA, AOutRecords);
  312. end;
  313. function TPHPWeb.Search(AURL: string; AInFields, AInValues: array of string; var AOutStrs: array of string; var AOutRecords: TOVArr): Integer;
  314. begin
  315. Result := CustomSearch(AURL, AInFields, AInValues, 2, AOutStrs, AOutRecords);
  316. end;
  317. function TPHPWeb.Search(AURL: string; AInFields, AInValues: array of string; AResultType: Integer; var AOutStrs: array of string; var AOutRecords: TOVArr): Integer;
  318. begin
  319. Result := CustomSearch(AURL, AInFields, AInValues, 3, AOutStrs, AOutRecords);
  320. end;
  321. function TPHPWeb.TempName(ALength: Integer): string;
  322. const
  323. CodedBuf: array[0..35] of Char = ('0', '1', '2', '3', '4', '5',
  324. '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  325. 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  326. 'W', 'X', 'Y', 'Z');
  327. begin
  328. Result := '';
  329. Randomize;
  330. while Length(Result) < ALength do
  331. Result := Result + CodedBuf[Random(36)];
  332. end;
  333. function TPHPWeb.WebPath: string;
  334. begin
  335. Result := ExtractFilePath(Application.ExeName) + 'Web\';
  336. if not DirectoryExists(Result) then
  337. ForceDirectories(Result);
  338. end;
  339. function TPHPWeb.UserPath: string;
  340. begin
  341. Result := ExtractFilePath(Application.ExeName) + 'Web\Users\' + IntToStr(FUserID) + '\';
  342. if not DirectoryExists(Result) then
  343. ForceDirectories(Result);
  344. end;
  345. function TPHPWeb.DownFile(ASourceURL: string; var ALocalFile: string): Boolean;
  346. var
  347. vStream: TMemoryStream;
  348. iFlag: Integer;
  349. bOK: Boolean;
  350. procedure SafeDownFile(ASourceURL: string; var ALocalFile: string);
  351. begin
  352. Inc(iFlag);
  353. bOK := False;
  354. try
  355. IdHTTP.Get(ASourceURL, vStream);
  356. vStream.SaveToFile(ALocalFile);
  357. bOK := True;
  358. except
  359. bOK := False;
  360. end;
  361. end;
  362. begin
  363. iFlag := 0;
  364. bOK := False;
  365. Result := False;
  366. if Trim(ASourceURL) = '' then Exit;
  367. if Trim(ALocalFile) = '' then Exit;
  368. if (ExtractFileExt(ALocalFile) = '.rmf') and FileExists(ALocalFile) then
  369. DeleteFile(ALocalFile);
  370. vStream := TMemoryStream.Create;
  371. try
  372. while (not bOK) and (iFlag < 3) do // 尝试3次
  373. SafeDownFile(ASourceURL, ALocalFile);
  374. Result := bOK;
  375. finally
  376. vStream.Free;
  377. end;
  378. end;
  379. function TPHPWeb.CustomSearch(AURL: string; AInFields,
  380. AInValues: array of string; AResultType: Integer;
  381. var AOutStrs: array of string; var AOutRecords: TOVArr): Integer;
  382. var
  383. i: Integer;
  384. postList: TStrings;
  385. ssResponse: TStringStream;
  386. bDone: Boolean;
  387. vJson: TCslJson;
  388. begin
  389. Result := -1;
  390. postList := TStringList.Create;
  391. ssResponse := TStringStream.Create('');
  392. try
  393. try
  394. for i := Low(AInFields) to High(AInFields) do
  395. postList.Add(Format('%s=%s', [AInFields[i], AInValues[i]]));
  396. IdHTTP.Post(AURL, postList, ssResponse);
  397. bDone := True;
  398. except
  399. bDone := False;
  400. end;
  401. if (bDone) and (Pos('200 OK', IdHTTP.ResponseText) <> 0) then // 网络已通
  402. begin
  403. vJson := TCslJson.Create;
  404. try
  405. vJson.Text := Utf8ToAnsi(ssResponse.DataString);
  406. if SameText(vJson.Value['status'], 'true') then // 数据读取成功
  407. begin
  408. case AResultType of
  409. 0:
  410. begin
  411. for i := Low(AOutStrs) to High(AOutStrs) do
  412. AOutStrs[i] := vJson.Value[AOutStrs[i]];
  413. end;
  414. 1:
  415. begin
  416. AOutRecords := vJson.ArrayValues('info');
  417. end;
  418. 2:
  419. begin
  420. for i := Low(AOutStrs) to High(AOutStrs) do
  421. AOutStrs[i] := vJson.Value[AOutStrs[i]];
  422. AOutRecords := vJson.ArrayValues('info');
  423. end;
  424. 3:
  425. begin
  426. vJson.ChildValues('info', AOutStrs);
  427. AOutRecords := vJson.ArrayValues('auditinfo');
  428. end;
  429. end;
  430. Result := 1;
  431. end
  432. else // 数据读取失败
  433. begin
  434. case AResultType of
  435. 0:
  436. begin
  437. AOutStrs[Low(AOutStrs)] := vJson.Value['msg'];
  438. end;
  439. 1:
  440. begin
  441. SetLength(AOutRecords, 1, 1);
  442. AOutRecords[0, 0] := vJson.Value['msg'];
  443. end;
  444. 2, 3:
  445. begin
  446. AOutStrs[Low(AOutStrs)] := vJson.Value['msg'];
  447. SetLength(AOutRecords, 1, 1);
  448. AOutRecords[0, 0] := vJson.Value['msg'];
  449. end;
  450. end;
  451. Result := 0;
  452. end;
  453. finally
  454. vJson.Free;
  455. end;
  456. end;
  457. finally
  458. postList.Free;
  459. ssResponse.Free;
  460. end;
  461. end;
  462. function TPHPWeb.SystemDateTime: TDateTime;
  463. begin
  464. Result := Now;
  465. end;
  466. function TPHPWeb.NetError(AMid: string): string;
  467. begin
  468. Result := Format('当前网络状态较差(-1),%s,请重新尝试或重新登录后再尝试!', [AMid]);
  469. end;
  470. function TPHPWeb.PageError(AMid: string): string;
  471. begin
  472. Result := Format('Web页返回错误(000J),%s,请重新尝试或联系纵横客服。', [AMid]);
  473. end;
  474. procedure TPHPWeb.Debug(AFileName, AStr: string);
  475. var vSL: TStringList;
  476. sAdd: string;
  477. begin
  478. vSL := TStringList.Create;
  479. try
  480. sAdd := Format('%s%d %s %s', [#10#13#10#13#10#13#10#13#10#13,
  481. UserID, RealName, Account]);
  482. vSL.Add(AStr + sAdd);
  483. if Pos('Debug_', AFileName) = 0 then
  484. AFileName := 'Debug_' + AFileName;
  485. vSL.SaveToFile(WebPath + AFileName + '.txt');
  486. finally
  487. vSL.Free;
  488. end;
  489. end;
  490. procedure TPHPWeb.Debug(AFileName: string; AInFields, AInValues: array of string);
  491. var vSL: TStringList;
  492. sAdd: string;
  493. i: Integer;
  494. begin
  495. if G_IsTest then
  496. begin
  497. vSL := TStringList.Create;
  498. try
  499. sAdd := Format('%s%d %s %s', [#10#13#10#13#10#13#10#13#10#13, UserID, RealName, Account]);
  500. for i := Low(AInFields) to High(AInFields) do
  501. vSL.Add(AInFields[i] + #9#9 + AInValues[i]);
  502. vSL.Add(sAdd);
  503. if Pos('Debug_', AFileName) = 0 then
  504. AFileName := 'Debug_' + AFileName;
  505. vSL.SaveToFile(WebPath + AFileName + '.txt');
  506. finally
  507. vSL.Free;
  508. end;
  509. end;
  510. end;
  511. function TPHPWeb.ConnectServer(AIP: string; var ACompanyName: string): Integer;
  512. var vArr: array of string;
  513. sURL: string;
  514. begin
  515. sURL := Format('http://%s/api/client/software/title', [AIP]);
  516. vArr := VarArrayOf(['msg']);
  517. Result := Search(sURL, [], [], vArr);
  518. case Result of
  519. -1: ACompanyName := '地址不存在';
  520. 0: ACompanyName := '服务器Web页错误';
  521. 1: ACompanyName := vArr[0];
  522. end;
  523. end;
  524. function TPHPWeb.UpAttachmentFile(AUperID, AWebID, ABillID: Integer; AFile,
  525. ACategory, AMemo, APhaseName: string; var ANewFileName: string): Boolean;
  526. var
  527. vMPFDS: TIdMultiPartFormDataStream;
  528. sResult, sURL: string;
  529. J: TCslJson;
  530. vArr: array of string;
  531. begin
  532. Result := False;
  533. if not FileExists(AFile) then
  534. begin
  535. // Application.MessageBox(PChar(AFile + ' 文件不存在!'), '错误', MB_OK + MB_ICONERROR);
  536. Exit;
  537. end;
  538. vMPFDS := TIdMultiPartFormDataStream.Create;
  539. try
  540. sURL := Format('%stender/attachment/%d/%d/upload', [FMeasureURL, AWebID, AUperID]);
  541. vMPFDS.AddFile('upitem', AFile, 'text/plain');
  542. vMPFDS.AddFormField('itemid', IntToStr(ABillID));
  543. vMPFDS.AddFormField('Category', ACategory);
  544. vMPFDS.AddFormField('Memo', ReplaceCharsForJson(AMemo));
  545. vMPFDS.AddFormField('PhaseName', APhaseName);
  546. vMPFDS.Position := 0;
  547. try
  548. sResult := IdHTTP.Post(sURL, vMPFDS);
  549. except
  550. Abort;
  551. end;
  552. finally
  553. vMPFDS.Free;
  554. end;
  555. J := TCslJson.Create;
  556. try
  557. J.Text := Utf8ToAnsi(sResult);
  558. if SameText(J.Value['status'], 'true') then
  559. begin
  560. Result := True;
  561. vArr := VarArrayOf(['onlinefilename']);
  562. J.ChildValues('iteminfo', vArr);
  563. ANewFileName := vArr[0];
  564. end
  565. else
  566. begin
  567. Result := False;
  568. end;
  569. finally
  570. J.Free;
  571. end;
  572. end;
  573. function TPHPWeb.GetAttachmentFileList(AWebID: Integer; var vArr: TOVArr): Boolean;
  574. var sURL: string;
  575. begin
  576. sURL := Format('%stender/attachment/%d/list', [FMeasureURL, AWebID]);
  577. Result := Search(sURL, [], [], vArr) = 1;
  578. end;
  579. function TPHPWeb.ExistInServer(AWebID: Integer): Boolean;
  580. var vArr: array of string;
  581. iResult: Integer;
  582. sSearchURL: string;
  583. begin
  584. sSearchURL := Format('%stender/get/%d/exist', [PHPWeb.MeasureURL, AWebID]);
  585. vArr := VarArrayOf(['id', 'name']);
  586. iResult := PHPWeb.Search(sSearchURL, [''], [''], vArr);
  587. if (iResult = 1) and (High(vArr) >= 0) then
  588. Result := True
  589. else
  590. Result := False;
  591. end;
  592. // 格式为 Measure:chen
  593. function TPHPWeb.GetNameFromURLProtocol(AURLProtocol: string): string;
  594. begin
  595. if Trim(AURLProtocol) = '' then
  596. Result := ''
  597. else
  598. Result := Copy(AURLProtocol, 9, Length(AURLProtocol) - 8);
  599. end;
  600. initialization
  601. finalization
  602. if g_PHPWeb <> nil then
  603. g_PHPWeb.Free;
  604. end.