cross socket接口封装

cross socket接口封装

cross socket是DELPHI跨操作系统的SOCKET通讯库,支持WINDOWS,LINUX,MACOS操作系统。

经过封装的接口,调用异常简单。

procedure TForm1.Button2Click(Sender: TObject);
//REST查询
begin
  var url, sql1, sql2: string;
  sql1 := TNetEncoding.URL.Encode('select * from tgoods');
  sql2 := TNetEncoding.URL.Encode('select * from tunit');
  url := FUrl + 'rest?accountno=1&tablenum=2&plugid=9999&funcid=' + IntToStr(cmd_query) + '&sql1=' + sql1 + '&sql2=' + sql2;
  var s: string := IdHTTP1.Get(url);
  Memo1.Lines.Add(s);
  var js: TynJsonCross := TynJsonCross.Create;
  js.Text := s;
  js.ReadCDS(0, ClientDataSet1);
  js.ReadCDS(1, ClientDataSet2);
  js.Free;
end;

  

procedure TForm1.Button3Click(Sender: TObject);
//bin调用存储过程
begin
  var pack: TMsgPack := TMsgPack.Create;
  pack.Force('cmd').AsInteger := cmd_plugbin;
  pack.Force('plugid').AsInteger := 9999;
  pack.Force('funcid').AsInteger := cmd_sp;
  pack.Force('spname').AsString := 'sp_9';
  pack.Force('accountno').AsString := '1';
  pack.Force('params').AsString := '@goodsid=100036';   //多个参数以分号作分隔符
  execRemoteFunc(pack, FUrl + 'bin');
  if pack.Force('return').AsBoolean then
    ClientDataSet1.Data := pack.Force('dataset1').AsVariant;
  pack.Free;
end;

  

procedure TForm1.Button5Click(Sender: TObject);
//REST TOKEN
begin
  const secret: string = 'ynMiddleWare(cross)';    //公钥
  var LToken: TJWT := TJWT.Create;
  LToken.Claims.Subject := secret;      //主题
  LToken.Claims.IssuedAt := Now;        //签发时间
  LToken.Claims.Expiration := Now + 1;  //超时
  LToken.Claims.Issuer := secret;       //签发人
  var LAlg: TJOSEAlgorithmId := TJOSEAlgorithmId.HS256;    //hs256
  var s: string := TJOSE.SerializeCompact(secret, LAlg, LToken);
  var url: string := FUrl + 'rest?accountno=1&tablenum=1&&token=' + s + '&plugid=9999&funcid=' + IntToStr(cmd_query_token) + '&sql1=' + TNetEncoding.URL.Encode('select * from tunit');
  s := IdHTTP1.Get(url);
  Memo1.Lines.Add(s);
  LToken.Free;
  var js: TynJsonCross := TynJsonCross.Create;
  js.Text := s;
  js.ReadCDS(0, ClientDataSet1);
  js.Free;
end;

  

procedure TForm1.Button6Click(Sender: TObject);
//bin雪花ID
begin
  var pack: TMsgPack := TMsgPack.Create;
  pack.Force('cmd').AsInteger := cmd_plugbin;
  pack.Force('accountno').AsString := '1';
  pack.Force('orgid').AsInteger := 1;
  pack.Force('machineid').AsInteger := 1;
  pack.Force('plugid').AsInteger := 1000;
  pack.Force('funcid').AsInteger := cmd_snowid;
  execRemoteFunc(pack, FUrl + 'bin');
  Memo1.Lines.Add(IntToStr(pack.Force('snowid').AsInteger));
  pack.Free;
end;

procedure TForm1.Button7Click(Sender: TObject);
//rest雪花ID
begin
  var url: string;
  url := FUrl + 'rest?orgid=6&machineid=9&plugid=1000&funcid=' + IntToStr(cmd_snowid);
  var s: string := IdHTTP1.Get(url);
  Memo1.Lines.Add(s);
end;

procedure TForm1.Button8Click(Sender: TObject);
//BIN提交
begin
  if ClientDataSet1.State in [dsInsert, dsEdit] then
    ClientDataSet1.Post;
  if ClientDataSet2.State in [dsInsert, dsEdit] then
    ClientDataSet2.Post;
  if ClientDataSet1.ChangeCount = 0 then
    exit;
  if ClientDataSet2.ChangeCount = 0 then
    exit;
  var pack: TMsgPack := TMsgPack.Create;
  pack.Force('cmd').AsInteger := cmd_plugbin;
  pack.Force('plugid').AsInteger := 9999;
  pack.Force('funcid').AsInteger := cmd_save;
  pack.Force('tablenum').AsInteger := 2;
  pack.Force('tablename1').asstring := 'tgoods';
  pack.Force('tablename2').asstring := 'tunit';
  pack.Force('delta1').AsVariant := ClientDataSet1.Delta;
  pack.Force('delta2').AsVariant := ClientDataSet2.Delta;
  pack.Force('accountno').AsString := '1';
  execRemoteFunc(pack, FUrl + 'bin');
  if pack.Force('return').AsBoolean then
  begin
    ClientDataSet1.MergeChangeLog;
    ClientDataSet2.MergeChangeLog;
  end;
end;

procedure TForm1.Button9Click(Sender: TObject);
//rest上传文件
begin
  if OpenDialog1.Execute then
  begin
    var ms: TIdMultiPartFormDataStream := TIdMultiPartFormDataStream.Create;
    ms.AddFormField('test', TNetEncoding.URL.Encode('中国'));
    ms.AddFile('file1', OpenDialog1.FileName);
    IdHTTP1.Post(FUrl + 'rest?plugid=8888&funcid=6&filenum=1&filename1=test.txt', ms);
    ms.Free;
  end;
end;

procedure TForm1.execRemoteFunc(pack: TMsgPack; url: string);
//bin执行远程方法
begin
  var send: TMemoryStream := TMemoryStream.Create;
  var recv: TMemoryStream := TMemoryStream.Create;
  pack.EncodeToStream(send);    //msgpack-->stream
  send.Position := 0;
  IdHTTP1.Post(url, send, recv);  //send
  send.Free;
  pack.Clear;
  recv.Position := 0;
  var ms2: TStream := TMemoryStream.Create;
  TZip.UnZipStream(recv, ms2);        //解压
  ms2.Position := 0;
  pack.DecodeFromStream(ms2);          //stream-->msgpack
  recv.Free;
  ms2.Free;
end;

  

procedure TForm1.Button10Click(Sender: TObject);
//rest下载文件
//{"files":[
//	{"fielname1":"文件名1","file1":"BASE64文件流1"},
//	{"fielname2":"文件名1","file2":"BASE64文件流2"}
//	]
//}
begin
  var s: string := IdHTTP1.get(FUrl + 'rest?plugid=8888&funcid=5&filenum=1&filename1=1.exe');
  var jo: JsonDataObjects.TJsonObject := JsonDataObjects.TJsonObject.Parse(s) as JsonDataObjects.TJsonObject;
  var i: Integer;
  var filename, file2: string;
  var dd: TIdDecoderMIME := TIdDecoderMIME.Create(nil); //base64解码
  var ms: TMemoryStream := TMemoryStream.Create;
  try
    for i := 0 to jo.A['files'].Count - 1 do
    begin
      filename := jo.A['files'].O[i].s['filename' + (i + 1).ToString];  //文件名
      file2 := jo.A['files'].O[i].s['file' + (i + 1).ToString];  //文件
      dd.DecodeStream(file2, ms);   //base64解码
      ms.SaveToFile(filename);   //保存下载的文件
      ms.Clear;
    end;
  finally
    dd.Free;
    ms.Free;
  end;
end;

procedure TForm1.Button11Click(Sender: TObject);
//rest保存
begin
  var delta: TynJsonDelta := TynJsonDelta.Create('tunit');
  var delta2: TynJsonDelta := TynJsonDelta.Create('tgoods');
  var deltas: TynJsonDeltas := TynJsonDeltas.Create;
  try
    delta.Insert('{"unitid":"13","unitname":"人"}');
    delta.Delete('{"unitid":"66","unitname":"国"}');
    delta.Update('{"unitid":"11","unitname":"中"}', '{"unitid":"11","unitname":"个"}');

    delta2.Insert('{"goodsid":"123456","goodsname":"咏南中间件"}');

    deltas.Add(delta.Text);
    deltas.Add(delta2.Text);
    Memo1.Text := deltas.Text;
  finally
    delta.Free;
    delta2.Free;
    deltas.Free;
  end;
end;

procedure TForm1.Button12Click(Sender: TObject);
//bin上传文件
begin
  if OpenDialog1.Execute then
  begin
    var pack: TMsgPack := TMsgPack.Create;           //准备参数
    pack.Force('cmd').AsInteger := cmd_plugbin;
    pack.Force('filenum').AsInteger := 1;
    pack.Force('plugid').AsInteger := 8888;
    pack.Force('funcid').AsInteger := cmd_up;
    pack.Force('filename1').AsString := 'test.txt';
    pack.Force('file1').LoadBinaryFromFile(OpenDialog1.FileName);
    execRemoteFunc(pack, FUrl + 'bin');          //执行远程方法
    if pack.Force('return').AsBoolean then
    begin
      Memo1.Lines.Add('上传成功');
    end;
    pack.Free;
  end;
end;

procedure TForm1.Button13Click(Sender: TObject);
//bin下载文件
begin
  var pack: TMsgPack := TMsgPack.Create;           //准备参数
  pack.Force('cmd').AsInteger := cmd_plugbin;
  pack.Force('filenum').AsInteger := 1;
  pack.Force('plugid').AsInteger := 8888;
  pack.Force('funcid').AsInteger := cmd_down;
  pack.Force('filename1').AsString := 'test.txt';
  execRemoteFunc(pack, FUrl + 'bin');          //执行远程方法
  if pack.Force('return').AsBoolean then
  begin
    pack.Force('file1').SaveBinaryToFile('d:\test.txt');
    Memo1.Lines.Add('下载成功');
  end;
  pack.Free;
end;

procedure TForm1.Button14Click(Sender: TObject);
//rest存储过程
begin
  var url, spname, params: string;
  url := FUrl + 'rest?accountno=1&spname=' + spname + '&params=' + params + '&plugid=9999&funcid=' + IntToStr(cmd_sp);
  var s: string := IdHTTP1.Get(url);
  Memo1.Lines.Add(s);
  var js: TynJsonCross := TynJsonCross.Create;
  js.Text := s;
  js.ReadCDS(0, ClientDataSet1);
  js.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
//bin查询
begin
  var pack: TMsgPack := TMsgPack.Create;           //准备参数
  pack.Force('cmd').AsInteger := cmd_plugbin;
  pack.Force('plugid').AsInteger := 9999;
  pack.Force('funcid').AsInteger := cmd_query;
  pack.Force('tablenum').AsInteger := 2;
  pack.Force('sql1').AsString := 'select * from tgoods';
  pack.Force('sql2').AsString := 'select * from tunit';
  pack.Force('accountno').AsString := '1';
  execRemoteFunc(pack, FUrl + 'bin');      //执行远程方法
  if pack.Force('return').AsBoolean then
  begin
    ClientDataSet1.Data := pack.Force('dataset1').AsVariant;
    ClientDataSet2.Data := pack.Force('dataset2').AsVariant;
  end;
  pack.Free;
end;

  

posted @ 2020-02-12 09:56  delphi中间件  阅读(1947)  评论(0编辑  收藏  举报