WeChat applet - get the user's exercise steps

Procedure to obtain user information

Custom function getUserRun

insert image description here
In order to get the user's WeChat sports data page load, call this function and perform the following operations in the function

1. Get the user's permission settings

insert image description here

  • wx.getSetting(object, object) gets the settings of the current user, and the return value is the permissions (including allowed and not allowed) that the applet has requested from the client
  • The success in the function is that the authSetting in the output content is an empty object after successful execution, indicating that the current user does not have any permissions to modify the program
2. Determine whether the user has authorized the number of WeChat exercise steps

insert image description here

  • When the user authorizes the WeChat exercise steps, the successful return value in the setting authSetting should include scope.werun
  • It can be judged according to whether the return value authSetting object contains scope.werun

insert image description here

2.1 If the user has not been authorized
  • By calling wx.authorize(object, object), a pop-up window immediately asks whether to authorize the applet to use a certain function or obtain some data of the user. If the user has authorized it, it will not pop up.
    insert image description here
    insert image description here
  • The parameter value of scope is the permission to be applied for
  • Execute the sucess function after the user allows
    insert image description here
    • Call a custom method to get the user's WeChat movement data
  • If the user does not allow it, execute the fail functioninsert image description here
    • In the function, through wx.showModal pop-up prompt box title is the title and content is the content. After the user confirms, the prompt box disappears
2.2 If the user has authorized
  • Call the custom method (this method is to get the data of the user's WeChat movement)
    insert image description here
3. Custom function to read the user's WeChat movement data
  //定义函数读取用户微信运动数据
  getWeRunData() {
    
    
    wx.getWeRunData({
    
    
      success(res) {
    
    
        console.log(res)
      }
    })
  },

insert image description here

  • Obtain the user's WeChat exercise steps in the past 30 days by calling the wx.getWeRunData(Object object) interface. You need to call the wx.login interface first. The step count information will be updated when the user actively enters the applet.

  • But the returned data is encrypted for security and decrypted as follows
    insert image description here

  • Recompile custom function code

     //定义函数读取用户微信运动数据
      getWeRunData() {
          
          
        wx.getWeRunData({
          
          
          success(res) {
          
          
            //由于数据是进行加密的所以我们通过条用云函数的方式进行解密
            wx.cloud.callFunction({
          
          
              name: 'deswerundata',
              data: {
          
          
                weRunData: wx.cloud.CloudID(res.cloudID) // 这个 CloudID 值到云函数端会被替换
              }
            }).then(res=>{
          
          
              console.log(res)
            })
          }
        })
      },
    
    • The value of name is the name of the cloud function we want to call
    • data is the data we want to pass
    • .then is to get the returned data after requesting the cloud function
  • Create cloud function deswerundata
    insert image description here
    insert image description here

  • Edit the code in index.js in this cloud function to be

    // 云函数入口文件
    const cloud = require('wx-server-sdk')
    
    cloud.init()
    
    // 云函数入口函数
    exports.main = async (event, context) => {
          
          
      let weRunData = event.weRunData
    
      return {
          
          
        weRunData
      }
    }
    
  • Just upload and recompile the cloud function
    insert image description here

Guess you like

Origin blog.csdn.net/JHXL_/article/details/106398571