Skip to content

Open Interface

Login

login

TIP

The API usage is as follows: wx.login(Object object)

  • Function description: Call the interface to obtain login credentials, and then exchange the credentials for user login status information.

TIP

  • The v2 version (only supported by public clouds) follows the Luffa standard, and you can refer to the practical tutorial instructions. It can be called directly in the IDE (minimum supported version: 2.1.111).
  • The v1 version needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content. In the IDE, you can use the mock panel to mock the return value.
  • For version control rules, please refer to apiVersion Configuration.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • object.success callback function parameters: Object res
PropertiesTypeDescription
codestringUser login credentials (valid for five minutes). Developers need to call the call in the server background jscode2session , and use code to exchange for openid, session_key and other information
  • object.fail callback function parameter: Object err
PropertiesTypeDescription
errMsgstringError message
errnoNumbererrno error code, for detailed description of the error code, refer to server error code
  • Sample code:
js
wx.login({
  success(res) {
    if (res.code) {
      // Initiate a network request
      wx.request({
        url: "https://example.com/onLogin",
        data: {
          code: res.code,
        },
      });
    } else {
      console.log("Login failed!" + res.errMsg);
    }
  },
});

checkSession

TIP

The API usage is as follows: wx.checkSession(Object object)

  • Function description: Check whether the login state is expired.

TIP

  1. The v2 version (only supported by public clouds) follows the Luffa standard, and you can refer to the practical tutorial instructions. It can be called directly in the IDE (minimum supported version: 2.1.111).
  • session_key is unique. When using the applet, the same user has only one valid session_key at the same time.
  • The user login state obtained through the wx.login interface has a certain timeliness. Developers can call the wx.checkSession interface to check whether the current user login state is valid.
  • The verification object of wx.checkSession is the login state session_key corresponding to the last code operation. A successful call indicates that the session_key has not expired, and a failed call indicates that the session_key has expired.
  • After the login state expires, the developer can call wx.login again to obtain a new user login state.
  1. The v1 version needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content. You can use the mock panel in the IDE to mock the return value.
  2. For version control rules, please refer to apiVersion Configuration.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • Sample code:
js
wx.checkSession({
  success() {
    // The session_key has not expired and remains valid for this lifecycle.
  },
  fail() {
    // The session_key has expired, and the login process needs to be executed again.
    wx.login(); // Log in again.
  },
});

Account information

TIP

Account information related API Mini Program supports, but not supported by mini games

getAccountInfoSync

TIP

The API usage is as follows:Object wx.getAccountInfoSync()

  • Functional description: Get the current account information. The online mini program version number can only be obtained in the official version of the mini program, and cannot be obtained in the development version and trial version.
  • Return value: Object,Account information
PropertiesTypeDescription
miniProgramObjectMini program account information
  • miniProgram structure attribute
Structure propertiesTypeDescription
appIdstringMini program appId
envVersionstringMini program version, legal value is:
develop:Development version
trial:Experience version
release:Official version
versionstringOnline mini program version number

User information

TIP

Account information related API Mini Program supports, but not supported by mini games

getUserProfile

TIP

The API usage is as follows:wx.getUserProfile(Object object)

  • Function description: IDE does not currently support it. This API needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content
    You can use the mock panel in the IDE to mock the return value

  • Get user information. It can only be called after the page generates a click event (for example, in the callback of bindtap on the button). The authorization window will pop up for each request, and userInfo will be returned after the user agrees. This interface is used to replace wx.getUserInfo, see User Information Interface Adjustment Instructions

  • **Return value:**Object object。

PropertiesTypeDefault valueRequiredDescription
langstringenNoThe language for displaying user information, legal value is:
en:English
zh_CN: Traditional Chinese
descstring-YesState the purpose of obtaining user personal information, no more than 30 characters
successFunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)

getUserInfo

TIP

The API usage is as follows: wx.getUserInfo(Object object)

  • Functional description: IDE currently does not support it. This API needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content.
    In the IDE, you can use the mock panel to mock the return value.

  • Obtain user information, user authorization scope.userInfo is required during use.

  • Parameters and descriptions: Object object。

PropertiesTypeDefault valueRequiredDescription
langstringenNoThe language for displaying user information, legal value is:
en:English
zh_CN: Traditional Chinese
successFunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • object.success callback function parameters: Object res
PropertiesTypeDescription
userInfoUserInfoUser information object
  • Example
  • Mini program user information component example code (user authorization)
js
<!-- If only displaying user avatar and nickname, the <open-data /> component can be used -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>
<!-- The button must be used for login authorization -->
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">Login authorization</button>
<view wx:else>Please upgrade the host client version.</view>
js
Page({
  data: {
    canIUse: wx.canIUse("button.open-type.getUserInfo"),
  },
  onLoad: function () {
    // Check if the user has already authorized.
    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.userInfo"]) {
          // Already authorized, can directly call getUserInfo to get user profile photo and nickname.
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo);
            },
          });
        }
      },
    });
  },
  bindGetUserInfo(e) {
    console.log(e.detail.userInfo);
  },
});
  • Sample code:
js
// Must be called when the user has already authorized.
wx.getUserInfo({
  success: function (res) {
    var userInfo = res.userInfo;
    var nickName = userInfo.nickName;
    var avatarUrl = userInfo.avatarUrl;
  },
});

userInfo

  • Functional description: User information
  • Parameters and descriptions:
PropertiesTypeDescription
nickNamestringUser nickname
avatarUrlstringThe URL of the user's avatar image. The last value of the URL represents the square avatar size (there are 0, 46, 64, 96, 132 values
gendernumberUser gender. No longer returned, legal values
0: Unknown
1: Male
2: Female
countrystringUser's country. No more returns
provincestringUser's region. No more returns
citystringUser's city. No more returns
languagestringDisplay the language used by country, province, and city. Forced to return 'zh_TW', legal values
en:English
zh_TW: Traditional Chinese

Settings

AuthSetting

  • Functional description: User authorization setting information.
  • Parameters and descriptions:
PropertiesDescription
boolean scope.userLocationWhether to authorize geographic location, corresponding interface wx.getLocation, Whether to authorize geographic location, corresponding interface wx.chooseLocation
boolean scope.writePhotosAlbumWhether to authorize saving to album wx.saveImageToPhotosAlbum
boolean scope.cameraWhether to authorize camera, corresponding to <camera /> component
boolean scope.addFriendAllow to be added as a friend, actively call wx.authorize Interface for authorization

getSetting

TIP

The API usage is as follows: wx.getSetting(Object object)

  • Functional description: Get the user's current settings.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • object.success callback function parameters: Object res。
PropertiesTypeDescription
authSettingAuthSettingUser authorization result
  • Sample code:
js
wx.getSetting({
  success(res) {
    console.log(res.authSetting);
    // res.authSetting = {
    //   "scope.userInfo": true,
    //   "scope.userLocation": true
    // }
  },
});

openSetting

TIP

The API usage is as follows: wx.openSetting(Object object)

  • Functional description: Call up the client applet settings interface, return the operation result set by the user, after the user clicks, you can jump to open the settings page and manage the authorization information.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • object.success callback function parameters: Object res。
PropertiesTypeDescription
authSettingAuthSettingUser authorization result
js
wx.openSetting({
  success(res) {
    console.log(res.authSetting);
    // res.authSetting = {
    //   "scope.userInfo": true,
    //   "scope.userLocation": true
    // }
  },
});
js
wx.openSetting({
  success(res) {
    console.log(res.authSetting);
    // res.authSetting = {
    //   "scope.userInfo": true,
    //   "scope.userLocation": true
    // }
  },
});

Biometric authentication

TIP

Biometric authentication related API Mini Program supports, but not supported by mini games

checkIsSoterEnrolledInDevice

TIP

The API usage is as follows: wx.checkIsSoterEnrolledInDevice(Object object)

  • Functional description: Verify whether the biometric information is entered in the device.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • object.success callback function parameters: Object res。
PropertiesTypeDescription
isEnrolledbooleanWhether the information has been entered
errMsgstringError message
  • Sample code:
js
wx.checkIsSoterEnrolledInDevice({
  success(res) {
    console.log(res.isEnrolled);
  },
});

checkIsSupportSoterAuthentication

TIP

The API usage is as follows: wx.checkIsSupportSoterAuthentication(Object object)

  • Functional description: Check whether the machine supports biometric authentication, callback success if supported, callback fail if not supported.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • Sample code:
js
wx.checkIsSupportSoterAuthentication({
  success() {
    // Support biometric authentication
  },
});

startSoterAuthentication

TIP

The API usage is as follows: wx.startSoterAuthentication(Object object)

  • Functional description: Start biometric authentication.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
authContentstring''NoVerification description, that is, the dialog prompt content displayed on the interface during the identification process
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • Sample code:
js
wx.startSoterAuthentication({
  authContent: "Unlock biometric authentication",
  success() {
    // Authentication successful
  },
});

Authorization

TIP

The API usage is as follows: wx.authorize(Object object)

  • Functional description: Initiate an authorization request to the user in advance. After the call, a pop-up window will immediately ask the user whether to agree to authorize the applet to use a certain function or obtain certain user data, but the corresponding interface will not be actually called. If the user has agreed to the authorization before, no pop-up window will appear and the result will be returned directly as success.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
scopestring-YesScopes that need to obtain permissions See scope for details
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • Sample code:
js
// You can query whether the user has authorized the scope of "scope.record" using wx.getSetting
wx.getSetting({
  success(res) {
    if (!res.authSetting["scope.record"]) {
      wx.authorize({
        scope: "scope.record",
        success() {
          // Once the user has granted permission for the mini program to use the recording feature, subsequent calls to the wx.startRecord API will not prompt the user for permission again.
          wx.startRecord();
        },
      });
    }
  },
});

Subscription Message

requestSubscribeMessage

TIP

The API usage is as follows: wx.requestSubscribeMessage(Object object)

  • Functional description: Call up the client mini program subscription message interface and return the operation result of the user's subscription message. The user's subscription status for the relevant template message can be obtained through the wx.getSetting Interface

TIP

  • Only supported by public clouds.
  • One-time template id and permanent template id cannot be used at the same time.
  • In one authorization call, the template title corresponding to each tmplId cannot be the same. If the same one appears, only one is retained.
  • Parameters and descriptions: Object object。
PropertiesTypeDefault valueRequiredDescription
tmplIdsArray-YesA collection of message template ids that need to be subscribed. A maximum of 3 messages can be subscribed in one call. The message template id is configured in the console [Mini Program Management-Subscribe Message]. The template title corresponding to each tmplId needs to be different, otherwise it will be filtered.
successfunction-NoCallback function for successful interface call
failFunction-NoCallback function for failed interface call
completeFunction-NoCallback function for interface call end (will be executed for both successful and failed calls)
  • Sample code:
PropertiesTypeDescription
errMsgStringWhen the interface call is successful, the errMsg value is 'requestSubscribeMessage:ok'
[TEMPLATE_ID: string]String[TEMPLATE_ID] is a dynamic key, that is, the template id, and the values ​​include 'accept', 'reject', 'ban', and 'filter'
'accept':Indicates that the user agrees to subscribe to the template message corresponding to the id
'reject':Indicates that the user refuses to subscribe to the template message corresponding to the id
'ban':Indicates that it has been banned by the background
'filter':Indicates that the template has been filtered by the background because the template title has the same name
For example, { errMsg: "requestSubscribeMessage:ok", zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: "accept"} means the user agrees to subscribe to the message zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE
  • object.fail callback function parameter: Object err
PropertiesTypeDescription
errMsgStringInterface call failure error message
errnoNumberInterface call failure error code
  • Error code
errCodeerrMsgDescription
10001TmplIds can't be emptyParameters are empty
10002Request list failNetwork problem, message list request failed
10003Request subscribe failNetwork problem, subscription request failed to send
20001No template data return, verify the template id existNo template data, usually template ID The message does not exist or does not correspond to the template type.
20002Templates type must be sameThe template message type has both one-time and permanent.
20003Templates count out of max boundsThe number of template messages exceeds the upper limit.
20004The main switch is switched offThe user has turned off the main switch and cannot subscribe.
20005This mini program was banned from subscribing messagesThe mini program is banned.
20013Reject DeviceMsg TemplateSubscribing to device messages through this interface is not allowed.
  • Sample code:
js
wx.requestSubscribeMessage({
  tmplIds: [''],
  success: (res) => {
    console.log('requestSubscribeMessage===success', res)
  }
})