unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DateUtils, IdHTTP, IdHashMessageDigest, Pos_Log,
superobject;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
edt_Url: TEdit;
edt_BarCode: TEdit;
edt_Type: TEdit;
btn2: TButton;
procedure btn2Click(Sender: TObject);
private
function Test(sVaule, sBarCode: string; var sMsg: string): Boolean;
function My_MD5(const sVaule: string): string;
function GetJavaTime(d: TDateTime): Int64;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetJavaTime(d: TDateTime): Int64;
var
dJavaStart: TDateTime;
begin
//java里的时间是从1970年1月1日0点到当前的间隔
dJavaStart := EncodeDateTime(1970, 1, 1, 8, 0, 0, 0); //从08:00:00开始计算
Result := MilliSecondsBetween(d, dJavaStart); //精确到毫秒
// Result := SecondsBetween(d, dJavaStart); //精确到秒
end;
function TForm1.My_MD5(const sVaule: string): string;
var
mymd5: TIdHashMessageDigest5;
begin
try
mymd5 := TIdHashMessageDigest5.Create;
Result := (mymd5.AsHex(mymd5.HashValue(sVaule)));
finally
mymd5.Free;
end;
end;
function TForm1.Test(sVaule, sBarCode: string; var sMsg: string): Boolean;
var
S, sUrl, sAccountId, sServiceName, sRequestTime, sDataInfo, sSign, sSignKey, sUpdata, sJson: string;
IdHttp: TIdHTTP;
JO: ISuperObject;
ja: TSuperArray;
begin
//对接优惠券接口
LogInfo('开始提交接口*******Begin******************************');
sUrl := Trim(edt_Url.Text);
sSignKey := '6e591e1e95b74d608fa016187ae3ed21';
sAccountId := 'shenda';
sServiceName := Trim(sVaule);
sRequestTime := IntToStr(GetJavaTime(Now));
if UpperCase(sServiceName) = UpperCase('findCoupon') then //查询接口
sDataInfo := '{"couponCode":"' + sBarCode + '"}'
else if UpperCase(sServiceName) = UpperCase('activeCoupon') then //激活优惠券
sDataInfo := '{"couponCode":"' + sBarCode + '"}'
else if UpperCase(sServiceName) = UpperCase('refundCoupon') then //退优惠券
sDataInfo := '{"couponCode":"' + sBarCode + '","refundFlag":1}' //0-已使用、已过期可以退成功 1-已使用、已过期不可退成功
else
begin
sMsg := '错误:非法接口类型!';
LogInfo(sMsg);
LogInfo('结束提交接口*******End***【Error】***************************');
Exit;
end;
sSign := LowerCase(My_MD5(sAccountId + sServiceName + sRequestTime + sDataInfo + sSignKey)); //获取32位签名
sUpdata := sUrl + '?'
+ 'accountId=' + sAccountId
+ '&serviceName=' + sServiceName
+ '&requestTime=' + sRequestTime
+ '&signKey=' + sSignKey
+ '&sign=' + sSign
+ '&data=' + sDataInfo;
sUpdata := AnsiToUtf8(sUpdata);
LogInfo('提交地址:' + sUrl);
LogInfo('提交数据:' + sUpdata);
Memo1.Lines.Text := sUpdata;
IdHttp := TIdHTTP.Create(nil);
try
try
IdHttp.HTTPOptions := IdHttp.HTTPOptions + [hoKeepOrigProtocol];
IdHttp.ProtocolVersion := pv1_1;
IdHttp.Request.Accept := 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*';
IdHttp.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)';
IdHttp.Request.ContentEncoding := 'utf-8';
IdHttp.Request.ContentType := 'text/xml;Charset=UTF-8';
//正式提交接口
S := IdHttp.Get(sUpdata);
sJson := UTF8Decode(S);
sJson := StringReplace(sJson, '\', '', [rfReplaceAll]);
sJson := StringReplace(sJson, '"{"', '[{"', [rfReplaceAll]);
sJson := StringReplace(sJson, '"}"', '"}]', [rfReplaceAll]);
if sJson = '' then
begin
sMsg := '错误:返回数据为空!';
LogInfo(sMsg);
LogInfo('结束提交宏理接口*******End***【Error】***************************');
Exit;
end;
LogInfo('返回数据:' + sJson);
Memo2.Lines.Text := sJson;
except
on e: Exception do
begin
sMsg := '错误[Exception]:提交接口错误->' + E.Message;
LogInfo(sMsg);
LogInfo('结束提交接口*******End***【Error】***************************');
Exit;
end;
end;
finally
FreeAndNil(IdHttp);
end;
//解析Json
JO := SO(sJson);
if JO.O['result'].AsString = 'fail' then
begin
sMsg := '返回状态Error:【' + JO.O['result'].AsString + '】';
LogInfo(sMsg);
LogInfo('结束提交接口*******End***【Error】***************************');
Exit;
end;
//成功
if UpperCase(sServiceName) = UpperCase('findCoupon') then
begin
ja := JO['data'].AsArray; //解析data部分---->只有查询接口有此主体data
if ja <> nil then
sMsg := '查询状态:【' + JO.O['result'].AsString + '】;' + #10#13
+ '优惠券编号:【' + ja[0]['couponCode'].AsString + '】;' + #10#13
+ '优惠券状态:【' + ja[0]['couponState'].AsString + '】;' + #10#13
+ '优惠金额:【' + ja[0]['couponMny'].AsString + '】;' + #10#13
+ '满减金额:【' + ja[0]['condMny'].AsString + '】;' + #10#13
+ '有效结束日期:【' + ja[0]['validEndDt'].AsString + '】;' + #10#13
+ '剩余核销数量:【' + ja[0]['unUsedQty'].AsString + '】;'
else
sMsg := '查询状态:【' + JO.O['result'].AsString + '】';
end
else
begin
sMsg := '返回状态:【' + JO.O['result'].AsString + '】';
end;
LogInfo(sMsg);
LogInfo('结束提理接口*******End***【Success】***************************');
end;
procedure TForm1.btn2Click(Sender: TObject);
var
S1, S2, sMsg: string;
begin
//activeCoupon激活优惠券 findCoupon查询优惠券 refundCoupon退优惠券
S1 := Trim(edt_Type.Text);
S2 := Trim(edt_BarCode.Text);
if not test(S1, S2, sMsg) then // 180913332011247 180913790829524
begin
ShowMessage(sMsg);
end
else
begin
ShowMessage(sMsg);
end;
end;
end.
————————————————
原文链接:https://blog.csdn.net/qq_39951605/article/details/82702860