Note: this article is more about the implementation ideas and principles. (PS: It’s more like a log, mostly written for myself in case you forget)

demand:

Recently, there is a requirement in the project that the wechat public account should be used to inform the user of the message, and it should be associated with the mini program, that is, after the user receives the message on wechat and clicks the message, he can jump to the designated page of the corresponding mini program, as shown in the figure below

Introduction to Basic Knowledge

OpenID: In order to identify users, each user will generate a secure openID for each public account or small program and other applications. The public account or application can store this ID for users to identify their identity when they log in next time, or bind it with the user’s original account in third-party applications

General idea:

First, the user logs in to the mini program, then the system obtains the openID of the user in the mini program, and finally invokes the unified message service interface of wechat to send message notification to the corresponding user in the public account according to the OpenID of the mini program. The steps are as follows

  1. First, on the login page of the small program, the front end calls the wechat interface to obtain the current user’s code
  2. When the user logs in, the back-end appends logic: first obtains the access_token of the small program, then invokes the interface to obtain the OpenID according to the code, and finally obtains the OpenID and stores it in the database

Note: The access_token must be the appID and appsecret of the small program. The appID and appSecret of the public account cannot be used, otherwise the unauthorized appID error will be reported when the wechat notification message interface is invoked later

Question: The above step is to obtain the user’s openID in the small program, but we need to use the public account to notify the user, we need the user’s openID in the public account, how to deal with?

A: You can use the unified messaging service provided by wechat

The last note

  • Remember the public number with small procedures bound together
  • Remember to open the public certification
  • Note that the URL must have a value, even if you want to jump to a small program, but you still have to give the URL a random value