Article directory
I. Overview
WeChat mini-programs are applications based on the WeChat platform. Users can use these mini-programs directly in WeChat without downloading and installing them. They are light, fast, and practical.
-
WeChat Mini Programs can usually provide various services and functions, such as shopping, booking, social networking, games and so on. Developers can use WeChat mini program development tools to develop and upload to the WeChat platform for release. Users can search or scan the QR code to enter the Mini Program through WeChat's built-in Mini Program, or they can also enter through the sharing link.
-
WeChat applets can be used in WeChat without opening other applications, downloading and installing them, so they have the advantages of convenience, speed, and saving mobile phone storage space. In addition, WeChat Mini Programs also support various application scenarios, such as public transportation, tourist attractions, catering services, and so on.
In conclusion, WeChat Mini Program is a very useful tool, which provides various convenient services and functions, and also provides a convenient development platform for developers.
There are several prerequisites for the development of WeChat applets:
- Register a WeChat applet account. If you find it too troublesome, you can go to a certain treasure to find someone to help you register one.
- Install development tools
Official address: https://mp.weixin.qq.com/
Official document address: https://developers.weixin.qq.com/miniprogram/dev/framework/
2. The overall development structure
It is mainly divided into two parts: one part is the WeChat applet end, which is the front end , but it also provides cloud development; the other part is the back end, which will be connected to various external resources. In fact, it is the same as other non-small program development processes.
3. Register an account and install development tools
1) Register an account
The first step in developing a Mini Program is that you need to have a Mini Program account, through which you can manage your Mini Program.
1. Registration method
Click the " Register Now " button in the upper right corner on the official homepage of the WeChat public platform ( https://mp.weixin.qq.com/ ).
2. Select the registered account type
Select " Mini Programs " and click " View Type Differences " to view the differences and advantages of different types of accounts.
3. Fill in the email address and password
Please fill in the email address that has not been registered on the public platform, open platform, enterprise account, or unbound personal account.
[Reminder] Before filling in the mini program registration information, you need to register an email address, and each email address can only apply for one mini program . In addition, the mailboxes that have been bound to other Official Accounts, Mini Programs, and Personal Accounts cannot re-register for new Mini Programs.
4. Activate email
After clicking "Register", jump to the email activation page, activate the account, and continue the registration process.
5. Fill in the subject information
After clicking the activation link, continue to the next step of the registration process. Please select the subject type to complete the subject information and administrator information.
Select the subject type, here choose individual, of course you can also choose enterprise:
The principal types are described as follows:
1 ID number and 1 mobile phone number can only register 5 Mini Programs. Note that the subject information cannot be changed after submission.
If you choose an enterprise type account , you can choose two subject verification methods for this type of account (if you choose a personal account type, the following content can be ignored).
- Method 1: Payment Verification
It is necessary to use the company's public account to send money to Tencent to verify the identity of the subject, and the payment information can be viewed after submitting the subject information.
Please remit the specified amount to the specified receiving account according to the prompt on the page.
Reminder: Please
10
complete the remittance within 2 days, otherwise the registration will fail.
- Method 2: WeChat authentication
To verify the identity of the subject through WeChat authentication, a verification fee of 300 yuan is required. Before the certification is passed, some functions of the Mini Program are temporarily unavailable.
Fill in the administrator information
and confirm that the subject information cannot be changed
6. Log in background supplementary information
In the applet management background, supplement the basic information of the applet, such as name, icon, description, etc.
[Reminder] If your official account is a service account and has already been certified, you can use the "quick registration and certification applet process" without resubmitting the main materials, making a payment to the public, or paying the meta-certification fee
300
.
2) Install development tools
Download address: https://developers.weixin.qq.com/miniprogram/dev/devtools/devtools.html
3) Quick start
Before we start, let me talk about a few knowledge points.
1. The difference between cloud development and traditional development
- Cloud development is a set of cloud capabilities that solve the front-end and back-end development of small programs
- It provides a complete set of cloud services and a simple and easy-to-use API and management interface to reduce the cost of back-end development as much as possible, allowing developers to focus on the development of core business logic and complete back-end operations and management as easily as possible
- Cloud development includes applet front-end and applet backend.
Traditional development:
Cloud development:
-
Perhaps the developers who are just getting started with making small programs are a little confused about such an official introduction. To put it bluntly, the so-called cloud capabilities are actually server-side (back-end) . Developers themselves do not need to locally configure basic capabilities such as servers, databases, storage, and cloud functions, and can directly operate on the official server side provided by WeChat.
-
For ordinary development, you need to prepare your own server and the basic capabilities mentioned above. Therefore, compared with ordinary development, the biggest advantage of small program cloud development is that it is more convenient, more efficient, and saves development costs.
Pros and cons of using traditional development:
- Advantages: We don't need to worry about the background logic, we can get the desired data by sending the request directly
- Disadvantages: The development cost is too large, cannot be developed by individuals, requires a domain name, server and learning a new programming language
Pros and cons of using cloud development:
- Advantages: You can develop front-end and back-end by yourself, go online directly, do not need to rely on the back-end, and more importantly, it is easy to learn
- Disadvantages: We write the front and back ends by ourselves, so we have to learn APIs such as cloud development
Other differences between cloud development and traditional development:
Comparison item | cloud development | traditional server |
---|---|---|
Degree of difficulty | Simple | complex |
Ease of deployment | Basically no need to deploy | Deployment is time-consuming and laborious |
Do you need a domain name | unnecessary | need |
Do you need to file | unnecessary | need |
Whether to support https | unnecessary | need |
suitable for the company | Small and medium companies, individuals | big company |
Easy to learn | easy skill | more difficult to learn |
cost | The free version is basically enough | 200-2000/year |
2. Cloud development API classification
1) Cloud function
- A cloud function is a piece of code that runs on the cloud. It does not need to manage the server. It can be written in the development tool, uploaded and deployed with one click to run the back-end code.
- The applet provides a dedicated cloud function call
API
. Developers can use the methodwx-server- sdk
providedgetWXContext
to obtain the context of each call (appid
,openid
etc.), without having to maintain a complex authentication mechanism, they can obtain a natural and trusted user login status (openid
)
2) Cloud database
- A
MongoDB
database - A database can have multiple collections (equivalent to tables in relational data), a collection can be regarded as an
JSON
array , each object in the array is a record, and the format of the record isJSON
object
3) Cloud storage
- Cloud development provides a storage space, the ability to directly upload/download cloud files on the front end of the applet, and visual management on the cloud development console
- On the applet side, you can call
wx.cloud.uploadFile
andwx.cloud.downloadFile
to complete uploading and downloading cloud file operations
3. Get AppID
After registering an account, you can directly log in to the background to obtain the AppID.
4. Quick start
Create a small program, and the AppID is obtained above.
Traditional development:
4. Mini Program Login Process
1) Timing flow chart of the login process
Let’s take a look at the WeChat Mini Program login flow chart first, the official address : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.htmlDescription
- Call
wx.login()
to get the temporary login credentialscode
and send them back to the developer server. - Call
auth.code2Session
the interface exchange for the user's unique IDOpenID
, the user's unique ID under the WeChat open platform accountUnionID
(if the current Mini Program has been bound to the WeChat open platform account) and the session keysession_key
.
Afterwards, the developer server can generate a custom login status according to the user ID, which is used to identify the user's identity during the front-end and back-end interactions in the subsequent business logic.
【Precautions】
- The session key
session_key
is the key that cryptographically signs user data. In order to protect the application's own data, the developer server should not deliver the session key to the applet, nor should it provide this key to the outside world. - Temporary login credentials
code
can only be used once
2) Know openid, unionid and code
- openid: openid is a string used to uniquely identify the user. In the WeChat applet, each user's openid is unique. Through openid, the applet can obtain the basic information of the user, such as avatar, nickname, etc.
[Note] The same user has different openids in different applets. Therefore, when developing applets, openid cannot be used to judge the uniqueness of users.
unionid
:unionid
It is a string used to uniquely identify the user when the user binds multiple applications under the same WeChat open platform account. If the user uses the same WeChat account for login authorization in multiple mini-programs, then all of these mini-programsunionid
are the same.
[Note] The user's unionid will only be generated when the user binds multiple applications to the same WeChat open platform account. Therefore, if the user has not bound multiple applications, the applet will not be able to obtain the user's unionid.
code
:code
It is the user login credential, which is issued to the applet by the WeChat server. After the user authorizes the login, the Mini Program can obtain the user's ID by calling the WeChat login interfacecode
. Then, bycode
requesting the user'sopenid
andsession_key
.
[Note] Each code can only be used once and is valid for
5
minutes. Therefore, when using code to log in, it needs to be convertedopenid
intosession_key
information such as and of the user in time to avoid code expiration.
openid
, unionid
and code
are three very important parameters in WeChat Mini Program login authorization. Understanding the functions and usage of these parameters will help developers better design and develop the Mini Program login authorization function.
-
Get the code through wx.login().
-
Send this code to the backend, and the backend will return a token, which will be used as your unique identity.
-
Save the token in local storage through wx.setStorageSync().
-
When the user enters the page next time, the wx.getStorageSync() method will be used to determine whether the token has a value. If there is a value, other data can be requested. If there is no value, the login operation will be performed.
The introduction of the WeChat applet development process is here first. If you have any questions, please leave me a message. I will continue to update the relevant tutorials. Please wait patiently, and you can also follow my public account [Big Data and Cloud Native Technology Sharing] Add Group communication or private message communication technology~