Procedure to obtain user information
- Click to refer to the authorization in the WeChat documentation
- First, the program first asks the user what permissions to access
- Return to the program after the user makes a selection
- Program portability to access the server
- Return information if user allows
- Do not return if user is allowed
Custom function getUserRun
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
- 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
- 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
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.
- The parameter value of scope is the permission to be applied for
- Execute the sucess function after the user allows
- Call a custom method to get the user's WeChat movement data
- If the user does not allow it, execute the fail function
- 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)
3. Custom function to read the user's WeChat movement data
//定义函数读取用户微信运动数据
getWeRunData() {
wx.getWeRunData({
success(res) {
console.log(res)
}
})
},
-
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
-
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
-
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