国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
WeChat public platform development tutorial (3) Basic framework construction
1. Receive HTTP requests
2. Distribute requests
3. Message processor specifically processes messages
四、HTTP響應(yīng)
Home WeChat Applet WeChat Development WeChat public platform development tutorial (3) Basic framework construction

WeChat public platform development tutorial (3) Basic framework construction

Feb 16, 2017 pm 04:15 PM
Micro-channel public platform

WeChat public platform development tutorial (3) Basic framework construction

In the previous chapter, we have initially explained the basic principles of WeChat public account development. Today we will explore the design implementation.

First of all, we designed a module hierarchy diagram. Of course, the diagram only shows an implementation method and is not limited to this. See the figure below for details.

The main functions are introduced as follows:

1) Request interface layer. Processing HTTP requests and responses

2) Distribution layer. The request is passed in from the interface layer, and then the request type is analyzed specifically and distributed to different processors

3) Business logic layer. Here is our specific business logic. According to the request, the specific business logic is implemented.

4) Data layer. When we implement an application, we may need to access data, which can be a database or a file. If it is a simple application, this layer may not be available.

In fact, specific applications can be expanded on this structure, and the message object layer, business object layer, data access layer, functional management layer, etc. can be expanded. This is just to provide an idea and is not limited to this.

微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建

Design a flow chart based on the hierarchical diagram and describe each implementation process in detail. to understand the entire process. As shown in the figure below:

微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建

According to the flow chart, we can clearly understand the entire process and the specific implementation steps of message processing.

Below we implement the code for each process.

1. Receive HTTP requests

We need an HttpHandler or a web page to handle WeChat server HTTP requests.

Here we use HttpHandler. Because of its high flexibility and good performance.

The specific implementation is as follows.

????public?class?WeiXinHttpHandler:IHttpHandler
????{????????///?<summary>
????????///?
????????///?</summary>
????????public?bool?IsReusable
????????{????????????get?{?return?true;?}
????????}????????///?<summary>
????????///?處理請(qǐng)求????????///?</summary>
????????///?<param name="context"></param>
????????public?void?ProcessRequest(HttpContext?context)
????????{????????????//由微信服務(wù)接收請(qǐng)求,具體處理請(qǐng)求
????????????WeiXinService?wxService?=?new?WeiXinService(context.Request);????????????string?responseMsg?=?wxService.Response();
????????????context.Response.Clear();
????????????context.Response.Charset?=?"UTF-8";
????????????context.Response.Write(responseMsg);
????????????context.Response.End();
????????}
????}

If it is HTTPHandler, you need to configure the specific application in the configuration file. We will not explain the specific node configuration. Give an example directly and configure the HttpHandler node as follows

<httpHandlers>
???<add verb="*" path="WXService.ashx" type="namespace.WeiXinHttpHandler,WXWeb" validate="true"/></httpHandlers>

2. Distribute requests

In order to encapsulate the function, we also encapsulated this in the processing component. In fact, it can be placed in HttpHandler.

1) Verify signature

If this is the first request, the signature needs to be verified. It is equivalent to an HTTP handshake. In the previous chapter, the server URL and token value were set. This function is to check whether the connection is successful.

This request is a GET request. The following specific instructions (official):

Business logic:

Encryption/verification process:

<1> Put the three parameters token, timestamp, and nonce into dictionary order Sorting

<2> Splice the three parameter strings into one string for SHA1 encryption

<3> The developer can compare the encrypted string with the signature to identify the The request came from WeChat

and the official only provided PHP code examples. Many things are not directly translated in C#. So there are also some specific treatments here. Let’s look at the official code first:

????private?function?checkSignature()
????{????????$signature?=?$_GET["signature"];????????$timestamp?=?$_GET["timestamp"];????????$nonce?=?$_GET["nonce"];????
????????????????
????????$token?=?TOKEN;????????$tmpArr?=?array($token,?$timestamp,?$nonce);????????sort($tmpArr);????????$tmpStr?=?implode(?$tmpArr?);????????$tmpStr?=?sha1(?$tmpStr?);????????
????????if(?$tmpStr?==?$signature?){????????????return?true;
????????}else{????????????return?false;
????????}
????}

We translate it into the C# version:

????????///?<summary>
????????///?檢查簽名????????///?</summary>
????????///?<param name="request"></param>
????????///?<returns></returns>
????????private?bool?CheckSignature()
????????{????????????string?signature?=?Request.QueryString[SIGNATURE];????????????string?timestamp?=?Request.QueryString[TIMESTAMP];????????????string?nonce?=?Request.QueryString[NONCE];

????????????List<string>?list?=?new?List<string>();
????????????list.Add(TOKEN);
????????????list.Add(timestamp);
????????????list.Add(nonce);????????????//排序????????????list.Sort();????????????//拼串
????????????string?input?=?string.Empty;????????????foreach?(var?item?in?list)
????????????{
????????????????input?+=?item;
????????????}????????????//加密
????????????string?new_signature?=?SecurityUtility.SHA1Encrypt(input);????????????//驗(yàn)證
????????????if?(new_signature?==?signature)
????????????{????????????????return?true;
????????????}????????????else
????????????{????????????????return?false;
????????????}
????????}

SHA1 encryption is required here. The specific algorithm is as follows:

????????///?<summary>
????????///?SHA1加密????????///?</summary>
????????///?<param name="intput">輸入字符串</param>
????????///?<returns>加密后的字符串</returns>
????????public?static?string?SHA1Encrypt(string?intput)
????????{????????????byte[]?StrRes?=?Encoding.Default.GetBytes(intput);
????????????HashAlgorithm?mySHA?=?new?SHA1CryptoServiceProvider();
????????????StrRes?=?mySHA.ComputeHash(StrRes);
????????????StringBuilder?EnText?=?new?StringBuilder();????????????foreach?(byte?Byte?in?StrRes)
????????????{
????????????????EnText.AppendFormat("{0:x2}",?Byte);
????????????}????????????return?EnText.ToString();
????????}

2) Distribution request

The next step is the specific message request, here are all POST requests.

Because there are multiple message types, we encapsulate them through factory classes, and then each message has a dedicated processor for processing. Specific implementation logic:

????????///?<summary>
????????///?處理請(qǐng)求????????///?</summary>
????????///?<returns></returns>
????????private?string?ResponseMsg()
????????{????????????string?requestXml?=?Common.ReadRequest(this.Request);
????????????IHandler?handler?=?HandlerFactory.CreateHandler(requestXml);????????????if?(handler?!=?null)
????????????{????????????????return?handler.HandleRequest();
????????????}????????????return?string.Empty;
????????}

The external method for processing requests (this is the method called by HttpHandler), that is:

????????///?<summary>
????????///?處理請(qǐng)求,產(chǎn)生響應(yīng)????????///?</summary>
????????///?<returns></returns>
????????public?string?Response()
????????{????????????string?method?=?Request.HttpMethod.ToUpper();????????????//驗(yàn)證簽名
????????????if?(method?==?"GET")
????????????{????????????????if?(CheckSignature())
????????????????{????????????????????return?Request.QueryString[ECHOSTR];
????????????????}????????????????else
????????????????{????????????????????return?"error";
????????????????}
????????????}????????????//處理消息
????????????if?(method?==?"POST")
????????????{????????????????return?ResponseMsg();
????????????}????????????else
????????????{????????????????return?"無(wú)法處理";
????????????}
????????}

3. Message processor specifically processes messages

1) Message type

First let’s take a look , the specific message type, in fact, the message interface has been clearly given in the previous picture.

Let’s take a closer look here to see what types of messages are requested, what types of messages are replied, etc.

Be sure to note that the requested message is of text type, and the reply message is not necessarily text. It can be any replyable message such as graphics, text, music, etc. See the table below for details.

微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建

2) Design the message class according to the specific message interface.

這里給出類圖,供參考。

微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建

?

3)針對(duì)不同的消息,會(huì)有不同的處理器,來(lái)看下具體的類圖。

?微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建 ?

4)具體業(yè)務(wù)處理?

每個(gè)handler里面就是可以處理具體請(qǐng)求。輸入的什么消息,訪問(wèn)那些數(shù)據(jù),調(diào)用服務(wù)等,都在這里處理。

還是建議大家對(duì)具體的業(yè)務(wù)進(jìn)行單獨(dú)封裝,在Handler中,只提供調(diào)用的接口。

因?yàn)殡S著業(yè)務(wù)的增加,一個(gè)Handler可能要處理很多業(yè)務(wù),如果所有的操作邏輯都寫在這里,勢(shì)必影響閱讀,也不易于維護(hù)與擴(kuò)展。?

5)產(chǎn)生回復(fù)消息

在處理完請(qǐng)求后,需要生成回復(fù)消息,響應(yīng)到終端。消息格式,就是我們介紹那些消息類型,但必須是可用于回復(fù)的,當(dāng)前支持的有:文本、圖文、音樂(lè)等。

一定要明確:回復(fù)的消息類型不一定要與請(qǐng)求的消息類型一樣,比如,請(qǐng)求是文本,回復(fù)的可以是圖文、音樂(lè)。

產(chǎn)生回復(fù)消息的過(guò)程,其實(shí),就是特定的消息對(duì)象格式化為對(duì)應(yīng)的XML的過(guò)程,然后將XML響應(yīng)至微信服務(wù)器。

6)實(shí)例

這里以微信用戶關(guān)注公眾賬號(hào),然后服務(wù)端處理處理事件請(qǐng)求,登記用戶,并提示歡迎信息。

????class?EventHandler?:?IHandler
????{????????///?<summary>
????????///?請(qǐng)求的xml????????///?</summary>
????????private?string?RequestXml?{?get;?set;?}????????///?<summary>
????????///?構(gòu)造函數(shù)????????///?</summary>
????????///?<param name="requestXml"></param>
????????public?EventHandler(string?requestXml)
????????{????????????this.RequestXml?=?requestXml;
????????}????????///?<summary>
????????///?處理請(qǐng)求????????///?</summary>
????????///?<returns></returns>
????????public?string?HandleRequest()
????????{????????????string?response?=?string.Empty;
????????????EventMessage?em?=?EventMessage.LoadFromXml(RequestXml);????????????if?(em.Event?==?EventType.Subscribe)
????????????{????????????????//注冊(cè)用戶
????????????????User?user?=?new?User();
????????????????user.OpenID?=?em.FromUserName;
????????????????UserManager.Regester(user);????????????????//回復(fù)歡迎消息
????????????????TextMessage?tm?=?new?TextMessage();
????????????????tm.ToUserName?=?em.FromUserName;
????????????????tm.FromUserName?=?em.ToUserName;
????????????????tm.CreateTime?=?Common.GetNowTime();
????????????????tm.Content?=?"歡迎您關(guān)注xxx,我是小微。有什么我能幫助您的嗎?";
????????????????response?=?tm.GenerateContent();
????????????}????????????return?response;
????????}
????}

?

四、HTTP響應(yīng)

?最后將處理結(jié)果返回至最初HttpHandler,響應(yīng)給微信服務(wù)器,直接Response處理。這也是在最開(kāi)始設(shè)計(jì)的HttpHandler中實(shí)現(xiàn)的。

下面是代碼片段,具體可見(jiàn)一、Http請(qǐng)求?

????????????context.Response.Clear();
????????????context.Response.Charset?=?"UTF-8";
????????????context.Response.Write(responseMsg);
????????????context.Response.End();


?更多微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建?相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)