>  기사  >  위챗 애플릿  >  WeChat 서버 인터페이스 개발을 위한 예제 튜토리얼

WeChat 서버 인터페이스 개발을 위한 예제 튜토리얼

Y2J
Y2J원래의
2017-05-10 13:56:291834검색

ERP 시스템은 데이터를 위챗 공식 계정과 연동해야 하기 때문에 현재 중간 서버 구축을 준비 중입니다.
개발 환경: 추적 결과 서버가 WeChat으로부터 GET 요청을 수신할 수 없는 것으로 나타났습니다. 코드는 다음과 같습니다:


델파이/파스칼 코드?

unit Unit1;

interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
  IdComponent, IdCustomTCPServer, IdCustomHTTPServer, IdHTTPServer, IdContext,
  IdHashSHA, IdGlobal;
 
type
  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  private
    { Private declarations }
  public
    { Public declarations }
    function SHA1(Input: String): String;
    function CheckSignature(ARequestInfo: TIdHTTPRequestInfo): boolean;
  end;
 
var
  Form1: TForm1;
Const
  Token = 'weixin';
 
implementation
 
{$R *.dfm}
 
function TForm1.SHA1(Input: String): String;
begin
  with TIdHashSHA1.Create do
  try
    Result := LowerCase(HashBytesAsHex(TidBytes(Bytesof(Input))));
  finally
    Free;
  end;
end;
 
 
function TForm1.CheckSignature(ARequestInfo: TIdHTTPRequestInfo): boolean;
var
  signature, timestamp, nonce, echostr: String;
  tmpstr: TStringList;
  temp: String;
begin
  tmpstr := TStringList.Create;
  try
    signature := ARequestInfo.Params.Values['signature'];
    timestamp := ARequestInfo.Params.Values['timestamp'];
    nonce := ARequestInfo.Params.Values['nonce'];
 
    echostr := ARequestInfo.Params.Values['echostr'];
    tmpstr.Add(Token);
    tmpstr.Add(timestamp);
    tmpstr.Add(nonce);
    tmpstr.Sort;
    temp := StringReplace(tmpstr.text, #13#10, '', [rfReplaceAll]);
    Result := SHA1(temp) = signature;
  finally
    tmpstr.Free;
  end;
end;
 
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  IdHTTPServer1.Active := True;
end;
 
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  Memo1.Lines.Add('123');
  if CheckSignature(ARequestInfo) then
  if ARequestInfo.Params.Values[&#39;echostr&#39;] <> &#39;&#39; then
  begin
    Memo1.Lines.Add(ARequestInfo.Params.Values[&#39;echostr&#39;]);
    AResponseInfo.ContentType := &#39;text/html; charset=UTF-8&#39;;
    AResponseInfo.ContentText := ARequestInfo.Params.Values[&#39;echostr&#39;];
  end;
end;
 
end.

위 내용은 WeChat 서버 인터페이스 개발을 위한 예제 튜토리얼의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.