This article mainly introduces the general idea of implementing delphi development:
1. the user sends a message to the service number (this can be a menu item or a keyword, such as registering a member .)
2. kbmmw web server receives a message, generates a text message, and completes its own url in the text message. in the url, you can write the openid brought by the first message to the parameter table of the url.
3. the user clicks the text message
4. the web server generates a webpage based on the url of the text message received by the web server. The key here is to write the openid to the webpage.
5. the user fills in this webpage and submits it (the openid is also submitted at the same time)
6. OK. Now we get the content I want, the most required openid, that is, the content submitted by the user, and it is submitted back.
7. implement the business logic based on the content returned by 6.
Openid: as a user, the unique value generated when you pay attention to a service number. from the service number perspective, this is the creator of your service number. If you want to send messages to your users, you can only use this openid.
Go to development:
First, you can apply for a test account for development. The process is very simple. apply with your mobile phone number, send a verification code to your mobile phone, and activate it!
Then, a static IP address and port 80 are required. Theoretically, dynamic domain names can also be used. Bind to your development account.
Next, you need to familiarize yourself with the API and understand some basic concepts.
Now, you can use delphi for development!
First, you need to learn the access materials. here, because the principles are all there, you must carefully read the information. Then, use Delphi to implement a corresponding function and then apply for an account for public platform interface testing.
Function CheckSignature (const signature, timestamp, nonce, token: string): boolean;
Var
Strs: TStringList;
TmpStr: string;
Begin
Strs: = TStringList. Create;
Try
Strs. Add (token );
Strs. Add (timestamp );
Strs. Add (nonce );
Strs. Sort;
TmpStr: = strs [0] + strs [1] + strs [2];
TmpStr: = SHA1 (tmpStr );
If tmpStr = signature then
Result: = True
Else
Result: = False;
Finally
FreeAndNil (strs );
End;
End;
If the result returned by the function is true, the access is successful! Thank you for referring to the example uploaded by a csdn friend. In this example, the SHA1 function unit is provided, so you need to download it back.
After preparing this function, how can I use this function?
Because the server sends a Get request to the Accessed web server, we need to call this CheckSignature in our own implementation of the kbmMW web server's PerformGet method.
Function tdj_frmphonehttpservice.w.mget (ClientIdent: TkbmMWClientIdentity; const AURL: string;
Const Args: array of Variant): Variant;
Var
FuncName: string;
Qv: TkbmMWHttpQueryValues; // process the parameters passed in the Get request.
Begin
If Length (Args) <1 then
KbmMWRaiseException (KBMMW_ERR_SERVICE_HTTP_URLMISSING, 'Missing URL .')
Else
Begin
FuncName: = UpperCase (copy (Args [0], 2, Length (Args [0])-1 ));
If FuncName. Equals ('WeChat. HTML') then
Begin
// Transfer interface
Qv: = TkbmMWHttpQueryValues. Create;
Qv. AsString: = Args [2]; // Obtain the parameters passed by the get request.
Try
// Verify access
If qv. ValueByName ['echostr'] <> ''Then// If echostr is not empty, it indicates a verification request.
Begin
Result: = '';
If dmwx.CheckSignature(Qv. ValueByName ['id'], qv. ValueByName ['signature'],
Qv. ValueByName ['timestamp'], qv. ValueByName ['nonce ']) then
Result: = qv. ValueByName ['chostr'];// If the verification succeeds, echostr is returned to inform the server that the verification is successful.
End;
SetResponseMimeType ('text/HTML ');
SetResponsecharset ('utf-8 ');
Finally
FreeAndnil (qv );
End;
Exit; // end the response to Get.
End;
... Skip the implementation below
In actual projects, I implement the interface code in a wechatImpl unit, and then implement a DataModule to further encapsulate the wechatImpl method, to perform database operations.
Take verification as an example to see how DataModule is implemented?
Function tdmwx. CheckSignature (id, signature, timestamp, nonce: string): Boolean;
Begin
Result: = False;
If not qWXFWH. Active then
QWXFWH. Open;
If qWXFWH. Locate ('fid', VarArrayOf ([id]), []) then // query the service number table and check whether the corresponding id record exists
Begin
If wechatImpl. CheckSignature (signature, timestamp, nonce, qWXFWH. FieldByName ('ftoken'). AsString) // exists, the token value defined in the table is used to call the verification function.
Then
Result: = true;
End;
End;
QWXFWH is an kbmMWuniDACQuery object used to save a table with service numbers. the table structure is as follows:
Create table WX_FWH (
Fid integer, // The id of a service number to manage multiple service numbers.
FTOKEN VarChar (32), // The token value of each service number
FAPPID VarChar (16 ),
FAPPSECRET varchar (16 ),
Faccesstoken varchar (512 ),
Fexpiresin integer,
Fgetdate datetime );
When registering a server, you must first add registration information in this table, for example, id = 1, token = kbmmwtoken1
The corresponding registration content is:
Url = www. test. cc/wechat.html? Id = 1
Token = kbmmwtoken1
In this way, when the server sends a verification request, the id is passed as a parameter. the CheckSignature method of datamodule obtains the token value defined in the data table based on the id value, then, call the interface verification method.
To achieve access, I have divided the implementation code into three layers:
1. call the database-layer verification function in the get method of kbmmw web server
2. implement the verification function at the database layer to obtain pre-defined number information from the table, such as id and token, and then call the specific interface.
3. implement specific interfaces for the database layer to call.
After all the implementation of interfaces such as scheduler is completed, it will take some time to share it with friends. Let's share the idea of implementation first.
Verifying the url address used to access the web server is the first step of the service. Therefore, you must understand the above content before proceeding.
This is actually the case:
Users who follow your service number send a message to your service number. the platform will forward the message to the verified url, which is equivalent to the user of your service number, send messages to your Web server. The purpose of the Web server you developed is to respond to these messages and interact with your service number owner!
For more information about delphi implementation and development, see The PHP Chinese website!