支付寶小程序 快速示例·運動步數(shù)

2020-09-16 15:00 更新

借助運動數(shù)據(jù)功能,小程序在獲得用戶許可的情況下,可以獲取用戶最近 30 天內(nèi)的運動步數(shù),步數(shù)信息會在用戶進入小程序時更新。

注意:為了保證用戶良好的授權(quán)體驗,請開發(fā)者和商家在業(yè)務(wù)真實需要時發(fā)起授權(quán)請求,不要在小程序的首屏就喚起授權(quán)。

前提條件

獲取模板代碼

下載 獲取運動步數(shù) demo.Zip 文件,并解壓至本地。

使用步驟

添加功能

  1. 登錄 小程序開發(fā)中心,在 我的小程序 中,選擇相應(yīng)的小程序,進入該小程序詳情頁。

  1. 運動數(shù)據(jù)功能 需要簽約才能生效,在小程序上線后,點擊功能列表右側(cè) 立即簽約;簽約完成后,需要 1 個工作日左右的審批時間(審批結(jié)果會以短信和郵件形式告知),審批成功后,功能狀態(tài)會變?yōu)?已生效(如下圖所示)。

image

新建/打開項目

在 IDE 啟動界面新建 開放能力 > 獲取運動步數(shù) 模板項目,或者打開 獲取模板代碼 中的 zip 文件內(nèi)容。

快速體驗

終端進入 client 文件夾,輸入如下代碼安裝前端項目依賴

npm install
  1. 在小程序開發(fā)者工具打開下載的工程,然后關(guān)聯(lián)對應(yīng)的小程序。
  2. 將 client/pages/app.js 中對應(yīng)的如下小程序配置項改為用戶自己的參數(shù)。
    appId: '2021*********', // 小程序應(yīng)用標(biāo)識
    spaceId: 'ca8eb10f-26c1-4bee-**********', // 服務(wù)空間標(biāo)識
    clientSecret: 'Xckz2************', // 服務(wù)空間 secret key
    endpoint: 'https://api.************' // 服務(wù)空間地址,從小程序Serverless控制臺處獲得 

  1. 修改 server/functions/stepdecryption/index.js 文件下的 aesSecret

  1. 選中 server/functions/stepdecryption 目錄的代碼作為云函數(shù)進行上傳和部署,右鍵點擊喚起菜單,選擇 部署云函數(shù)。部署成功相當(dāng)于完成云函數(shù)的代碼發(fā)布,部署前注意區(qū)分測試環(huán)境和生產(chǎn)環(huán)境。

image

  1. 保存文件后,打開 IDE 的模擬器,頁面會自動刷新。至此,我們已經(jīng)完成了一個簡單的小程序的搭建,并將最核心的能力使用小程序 Serverless 開發(fā)完成,可使用支付寶開發(fā)者工具體驗。

前端初始化 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'})

獲取步數(shù)的加密數(shù)據(jù)

await my.getRunData({
  countDate: `${date.getFullYear()}-${month}-${day}`,
  success: (res) => {
    console.log(res.response)


  },
  fail: (res) => {
  },
  complete: (res) => {
  },});

調(diào)用 Serverless 云函數(shù)傳入相關(guān)參數(shù)進行解密

mpserverless.function.invoke('stepdecryption', {
  "step": res.response,})

nodejs 云函數(shù)解密代碼

'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
      }
    }
  }}
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號