The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Time:2021-1-26

GitHub:https://github.com/iccb1013/Sheng.WeixinConstruction

Due to limited personal energy and time, we will not update and maintain the existing code. However, the wechat interface is relatively stable. After testing, it has not changed, and all functions are still available. On this basis, you can redevelop and complete your business functions, or extract the code from this platform for reuse. In your project, please follow MIT Open source agreement to retain my copyright notice and website links.

GitHub:https://github.com/iccb1013/Sheng.WeixinConstruction.WeixinContract

There is also a separate project for the wechat protocol packaging project. The version of this project is slightly new, and I will update and maintain it. For example, several interfaces needed for small program development have been added recently. But note that because the code structure has been optimized and adjusted, it needs to modify the reference and name of some classes when it is directly referenced to shengxunwei wechat platform.

Shengxunwei wechat marketing system development practice series
Shengxunwei wechat marketing system development practice: (1) function summary and architecture design
Development practice of shengxunwei wechat marketing system: (2) detailed design of central control server
Shengxunwei wechat marketing system development practice: (3) function introduction and some experience of this project promotion process
Shengxunwei wechat marketing system development practice: (4) source code structure description
Development practice of shengxunwei wechat marketing system: (5) installation and deployment instructions


In this paper, I will be divided into two parts. The first part introduces the main structure of the code level in detail, and the second part introduces the method of installation and deployment in detail.

1) Code structure

After getting the code from GitHub, the structure of the source code directory is as follows:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Solution Description:

  • Sheng.WeixinConstruction.Admin.sln

Super management background used by super administrator and system operation and maintenance personnel.
This part of the function development has not been completed, only the basic framework and micro home page template simple management function.

  • Sheng.WeixinConstruction.Container.sln

Authentication central control service.
Responsible for managing and maintaining the AccessToken of all official account of the third party platform and AccessToken.

  • Sheng.WeixinConstruction.FileService.sln

File server.
It provides independent file storage service and provides some functions of managing material with WeChat official account.
Multiple different file server instances can be deployed, and CDN content distribution can be deployed.

  • Sheng.WeixinConstruction.sln

Manage the background and wechat.
The management backstage is used to provide services to the general users of the WeChat management system. The registered accounts are used to connect the official account numbers and use the series of functions of the WeChat marketing system.
The WeChat end is used to provide services to the public official account, and visit the website on WeChat official account.

  • Sheng.WeixinConstruction.WindowsService.sln

Windows services
It is mainly used to perform some timing services.

The following is the solution Sheng.WeixinConstruction.sln And Sheng.WeixinConstruction.Container SLN, and other solutions are relatively simple and will not be described in detail.
Open Sheng.WeixinConstruction.sln After that, the structure is as follows:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

  • Sheng.WeixinConstruction.Management.Shell

Background site

  • Sheng.WeixinConstruction.Client.Shell

Wechat site

1)Sheng.WeixinConstruction.Management.Shell

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

An important function of the management backstage is to connect the official account with WeChat after the user has registered the account.

Open the SettingsController.cs , and expand the docking region.

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Docking and DockingDemo correspond to the official account numbers of users. Dockingdemo is the experience URL for initiating the authorization page: it is used by Tencent auditors to experience the authorization page and confirm the usability of the process. This is Tencent’s request.

AuthorizerEntity represents the authorization of the official account, which contains detailed notes in the code.

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

When users connect to official account, a preauthorization code is required to open the Areas /Api/ Controllers directory. SettingsController.cs , find the getpreauthcode method, which is used to obtain the preauthorization Code:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

In Docking.cshtml This screen calls this interface, and when the preauthorization code is successfully obtained, the page is transferred to the authorized page of Tencent, and a callback page for authorization is given. When the official account owner authorizes the authorization, Tencent will transfer the page to the callback page and transmit the return code.

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Open the SettingsController.cs Find the AuthorizationCallback callback method, and complete the official account authorization.

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Here is a detail:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

In the system, create the official account authorization information and start to maintain its AccessToken, not in the background, but call the interface of the authentication central control server, notify the authentication central control server to achieve it.

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

_ Createauthorizerurl is the address of the authentication central control server:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

2) Sheng.WeixinConstruction.Client.Shell

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Apart from providing services to the public numbers, WeChat is also responsible for the news communication with WeChat’s official backstage. The official account of WeChat is sent to the public. Sheng.WeixinConstruction.Client . shell, not Sheng.WeixinConstruction.Management .Shell。

So the most important thing is to first handle the messages pushed by wechat and open the thirdpart in the controllers directory yWeixinApiController.cs :

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

The Handler interface is used to receive official account messages and event push.

When an ordinary wechat user sends a message to a public account, the wechat server sends the XML packet of the post message to this interface.

In the process of interaction between WeChat users and official account, some actions of users will enable WeChat server to notify the server in the form of event push, and the server address can be set up at the developer center, so that developers can get the information. Among them, some event push allows developers to reply to users after it happens, while some do not. When operating as a third-party platform, appid will be brought as part of the URL, such as:

http://wxc.shengxunwei.com/Th…$APPID$

$APPID$will be replaced by appid in the authorized official account.

When a message is pushed, the URL format is as follows:

http://wxc.shengxunwei.com/We…
?signature=84001ea92e2f369642e861d557b9f4c6781db1ca
&timestamp=1446393828
&nonce=1291578710
&encrypt_type=aes
&msg_signature=3ed4a96dbc50d491664ec3f425eb7fc1f088ac9b

After receiving the message from POST, the first step is to decrypt first, then find which WeChat official account should be assigned to, and find the corresponding official account context, and then send the decrypted message to it.

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

ClientDomainContext is the official account context used by WeChat. For more details about the official account, please refer to:

Development practice of shengxunwei wechat marketing system: (2) design of central control server
http://blog.shengxunwei.com/H…

In clientdomaincontext, a factory class of xmlmessage receiverfactory is referenced to process the XML messages pushed by wechat

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

In this factory class_ The receiverlist stores the specific implementations needed to process different messages. The implementations of these message receivers are defined in the Sheng.WeixinConstruction.Client .Core:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

When receiving the message from WeChat, it is necessary to find the corresponding context object of the official account, call its Handle method, and find the corresponding processor class according to the type of message, and give it to handle it, that is, the process of message processing.

At WeChat, there is another important work that is to get the identity of the official account, OpenId and user information, such as:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

When the official account (fans) visit WeChat page, they need to get personal identities, OpenId and other information of the person concerned, which is related to our own background information.

Open the clientbasilcontroller in the controllers directory, and the wechat view controller classes inherit this base class:

When the official account visits the WeChat page, the controller base class will get the membership information context and the corresponding official account context.

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

In onactionexecuting method, web page authorization of followers is completed according to wechat protocol.

First, we need to know the domain name details of the official account, assuming that we will configure the domain name of WeChat. wxc.shengxunwei.com Official account is official account. Official account is used directly by URL, followed by ID on the public address. But there is an important flaw. If the public address of the platform is illegal, WeChat will be sealed off. The whole platform will be sealed. WeChat will directly take the official account. wxc.shengxunwei.com This domain name is blocked. So we must use the way of extensive analysis to make the official account used. appid.wxc.shengxunwei . com to avoid this. How to configure the pan resolution domain name when installing and deploying shengxunwei wechat marketing system, please refer to the detailed instructions related to installation and deployment.

The process of web page authorization is explained in detail in onactionexecuting method

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Note that the webpage authorization for followers’ identities is performed by jumping to the official authorization page of wechat. After the authorization is completed, it will jump back to the specified callback page. In the third party weixinapi / oauthcallback in the areas / API / controllers directory, there are detailed comments in the code. Please refer to:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

At this point, the web page authorization for the identity of followers is completed.

There is also a front-end page JS authorization method, which will be involved in wechat payment. You can find the getjsapiconfig method under the clientdomaincontext to check:

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

In addition, the wechat terminal also realizes the wechat payment function, which is used for members to recharge or purchase goods with points

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

In short, wechat payment process needs to call the unified ordering interface provided by wechat to realize an ordering operation at the wechat payment end. This ordering and our system’s own ordering are two concepts. After the unified ordering is completed, relevant information is generated to the front-end page, and the front-end page calls JS SDK provided by wechat After the payment is completed, the wechat server will send a notification of the payment result to the callback URL address specified by us. For detailed process, please refer to the official development document of wechat.

I’m at areas / API / controllers/ PayController.cs The related functions are realized in the system

The most complete wechat open platform docking tutorial in history (with complete source code, GitHub open source)

Recommended Today

How to Build a Cybersecurity Career

Original text:How to Build a Cybersecurity Career How to build the cause of network security Normative guidelines for building a successful career in the field of information security fromDaniel miesslerstayinformation safetyCreated / updated: December 17, 2019 I’ve been doing itinformation safety(now many people call it network security) it’s been about 20 years, and I’ve spent […]