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.
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:
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.
Authentication central control service.
Responsible for managing and maintaining the AccessToken of all official account of the third party platform and AccessToken.
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.
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.
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:
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.
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.
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:
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.
Open the SettingsController.cs Find the AuthorizationCallback callback method, and complete the official account authorization.
Here is a detail:
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.
_ Createauthorizerurl is the address of the authentication central control server:
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 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:
$APPID$will be replaced by appid in the authorized official account.
When a message is pushed, the URL format is as follows:
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.
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
In clientdomaincontext, a factory class of xmlmessage receiverfactory is referenced to process the XML messages pushed by wechat
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：
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:
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.
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
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:
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:
In addition, the wechat terminal also realizes the wechat payment function, which is used for members to recharge or purchase goods with points
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