W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
借助運動數(shù)據(jù)功能,小程序在獲得用戶許可的情況下,可以獲取用戶最近 30 天內(nèi)的運動步數(shù),步數(shù)信息會在用戶進入小程序時更新。
注意:為了保證用戶良好的授權(quán)體驗,請開發(fā)者和商家在業(yè)務(wù)真實需要時發(fā)起授權(quán)請求,不要在小程序的首屏就喚起授權(quán)。
前提條件
下載 獲取運動步數(shù) demo.Zip 文件,并解壓至本地。
在 IDE 啟動界面新建 開放能力 > 獲取運動步數(shù) 模板項目,或者打開 獲取模板代碼 中的 zip 文件內(nèi)容。
npm install
appId: '2021*********', // 小程序應(yīng)用標(biāo)識
spaceId: 'ca8eb10f-26c1-4bee-**********', // 服務(wù)空間標(biāo)識
clientSecret: 'Xckz2************', // 服務(wù)空間 secret key
endpoint: 'https://api.************' // 服務(wù)空間地址,從小程序Serverless控制臺處獲得
import MPServerless from '@alicloud/mpserverless-sdk';
const mpserverless = new MPServerless({
uploadFile: my.uploadFile,
request: my.request,
getAuthCode: my.getAuthCode,}, {
appId: ' ', // 小程序應(yīng)用標(biāo)識
spaceId: ' ', // 服務(wù)空間標(biāo)識
clientSecret: ' ', // 服務(wù)空間 secret key
endpoint: ' ' // 服務(wù)空間地址,從小程序Serverless控制臺處獲得});const res = await mpserverless.user.authorize({
authProvider: 'alipay_openapi',
// authType: 'anonymous'})
await my.getRunData({
countDate: `${date.getFullYear()}-${month}-${day}`,
success: (res) => {
console.log(res.response)
},
fail: (res) => {
},
complete: (res) => {
},});
mpserverless.function.invoke('stepdecryption', {
"step": res.response,})
'use strict';
const crypto = require('crypto');const aesSecret = ''; // AES密鑰
module.exports = async (ctx) => {
const step = ctx.args.step;
if (!step) {
return {
success: false,
error: {
code: 'InvalidParameter',
message: '待解密部署不能為空'
}
}
}
try {
ctx.logger.info('[args]', ctx.args);
const crypted = Buffer.from(step, 'base64').toString('binary');
const key = Buffer.from(aesSecret, 'base64');
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
let decoded = decipher.update(crypted, 'binary', 'utf8');
decoded += decipher.final('utf8');
return {
success: true,
data: decoded
}
} catch (e) {
ctx.logger.error(e);
return {
success: false,
error: {
code: 'DecipheStepFail',
message: e.message
}
}
}}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: