首頁 >微信小程式 >微信開發 >delphi 實作微信開發接取微信公眾號平台

delphi 實作微信開發接取微信公眾號平台

高洛峰
高洛峰原創
2017-03-03 09:42:224007瀏覽

先學習存取的資料,在http://mp.weixin.qq.com/wiki/home/index.html,因為原理都在,所以一定要認真閱讀,然後,利用Delphi實作一個對應函數,然後申請微信公眾平台介面測試帳號。 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

function CheckSignature(const signature, timestamp, nonce, token: string ): boolean;
var
 

strs: TStringList;
  tmpStr: string;
begin

  strs := TStringList.Create;
  try
    strs.Add(token);
    str  Add(timestamp);
    strs.Add(nonce);

#    strs.Sort;
    tmpStr := strs[0] + strs[1] + strs[2]; ## cp := SHA1(tmpStr);

    if tmpStr = signature then
      Result := True
    else
    # Result :#    else
   ##  end;
end;
函數的回傳結果為真時,表示存取成功!函數,參考csdn朋友上傳的範例,表示感謝。在這個範例中,提供了SHA1函數的單元,所以,你要下載回來。

準備完這個函數,接下來,要如何使用這個函數呢?
因為微信伺服器是向接入的web伺服器發送Get請求,所以,我們要在自己實作的kbmMW web server的PerformGet方法中來呼叫這個CheckSignature。

function TDJ_FrmPhoneHTTPService.PerformGet(ClientIdent: TkbmMWClientIdentity; const AURL: string;
  const Args: array of Variant): Variant;##MuncName: str. ;//處理Get請求傳遞來的參數.
begin

  if Length(Args) < 1 then
    kbmMWRaiseException(KBMMW_ERR_SERVICE_HTTP_URLMISSING, 'Missing URL.')##  begin
    FuncName := UpperCase(copy(Args[0], 2, Length(Args[0]) - 1));
    if FuncName.Equals('WECHAT.HTML') then> ##      // 轉微信介面
      qv := TkbmMWHttpQueryValues.Create;
      qv.AsString :=驗證接入
        if qv.ValueByName['echostr'] <> '' then//如果echostr不為空,表明是驗證請求
          if dmwx.CheckSignature(qv.ValueByName['id'], qv.ValueByName['signature'],
            qv.ValueByName['timestamp'], qv.ValueByName['nonce']) then
            Result := qv.ValueByName['echostr'];//如果驗證成功,回傳echostr,告知微信伺服器驗證成功.
        end;
        SetResponseMimeType('text/HTML') 18/U ');
      finally
        FreeAndnil(qv);
      end;
     的實作

在實際的項目,我是這樣實現的,將微信的介面程式碼,實作在一個單元wechatImpl中,再實作一個DataModule,對wechatImpl的方法進一步封裝,封裝的時候,來實現對資料庫的操作。

還是以驗證為例,來看看DataModule中是如何實現的?

function tdmwx.CheckSignature(id, signature, timestamp, nonce: string): Boolean;
begin
  result:=False;
  if not qWXFWH.Active then##  . Open;
  if qWXFWH.Locate('fid', VarArrayOf([id]), []) then//查詢服務號碼表,是否有對應的id記錄
  begin
    if wechatImpl.CheckSignature( signature, timestamp, nonce, qWXFWH.FieldByName('FToken').AsString)//存在,則取在表中定義的token值,去呼叫驗證函數.
    then
    #  end;
end;
其中,qWXFWH是kbmMWuniDACQuery對象,用以保存微信服務號碼的一張表,表結構如下:
CREATE TABLE WX_FWH (
    FID INTEGER,//一個服務號碼的id,達到管理多個服務號碼的目的。
    FACCESSTOKEN VARCHAR (512),
    FACCESSTOKEN VARCHAR (512),
    FEXPIRESIN INTEGER,
    FGETDATE DATETIME);
在登錄伺服器時,請先在此表中增加註冊資訊,例如:id=1,token1=mmwto ##The corresponding registration content is:
url=www.test.cc/wechat.html?id=1
token=kbmmwtoken1
In this way, when the WeChat server sends a verification request, the id will be used as a parameter Passed over together, the CheckSignature method of datamodule will obtain the token value defined in the data table based on the value of id, and then call the verification method of the WeChat interface.

To summarize the above content, in order to achieve WeChat access, I divided the implemented code into three layers:
1. Call the verification function of the database layer in the get method of kbmmw web server
2. Implement the verification function in the database layer, obtain the predefined WeChat related information, such as id and token, from the table, and then call the specific WeChat interface
3. Implement the specific WeChat interface for the database layer to call.

The plan is to share it with friends after the implementation of the WeChat interface is completed. Unfortunately, time is tight recently and there are many chores, so it will take some time. Let me first share the ideas for implementation.

Verification of the URL address connected to the web server is the first step in providing WeChat services. Therefore, the above related content must be understood before proceeding.
It’s actually like this:
WeChat users who follow your service account send messages to your service account, and the WeChat platform will forward them to the URL you verified, which is equivalent to the WeChat users of your WeChat service account , send a message to your Web server. The purpose of the Web server you develop is to respond to these messages and interact with followers of your WeChat service account!

For more articles related to Delphi's realization of WeChat development and access to the WeChat public account platform, please pay attention to the PHP Chinese website!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn