Objective: Through a few lines of code, obtain the number of micro channel small program micro steps.

  1. Create the cloud function getOpenData and upload and deploy it as follows.
// The cloud function index.js
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
  return event;
}
Copy the code
  1. Page WXML code
<! -- index.wxml -->
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">getPhoneNumber</button>
<button bindtap="getWeRunData">getWeRunData</button>
Copy the code
  1. Page JS code
// index.js
Page({
  async getPhoneNumber(e) {
    const { cloudID } = e.detail;
    if (cloudID) {
      let { result } = await this.getOpenData(cloudID);
      console.log(result); }},async getWeRunData() {
    let { cloudID } = await wx.getWeRunData().catch((error) = > error);
    if (cloudID) {
      let { result } = await this.getOpenData(cloudID);
      console.log(result); }},async getOpenData(cloudID) {
    return wx.cloud.callFunction({
      name"getOpenData".data: {
        openData: wx.cloud.CloudID(cloudID), }, }); }});Copy the code
  1. Note: you need to initialize the cloud function wx.cloud.init({}) and check to compile JS to ES5.
  2. The corresponding number or number of wechat steps can be obtained from result.
  3. To obtain applets open data through cloud functions, sessionKey failure caused by frequent calls to wx.login() can be avoided.