useWeRun
获取微信运动数据
何时使用
当需要使用用户微信运动数据时
提示
获取用户微信数据时, 需要先调用 wx.login
接口。步数信息会在用户主动进入小程序时更新. 此外, 分享微信运动需在小程序管理后台,「开发」-「接口设置」中自助开通该组件权限。 只针对「体育-在线健身」类目的小程序开放。
API
const { get, share } = useWeRun();
参数说明
无
返回值说明
参数 | 类型 | 说明 |
---|---|---|
get | PromiseWithoutOptionAction<Taro.getWeRunData.SuccessCallbackResult> | 获取用户过去三十天微信运动步数 |
share | PromiseAction<Taro.shareToWeRun.record[]> | 分享数据到微信运动 |
代码演示
- React
- Vue
wechat/useWeRun/index
import React from 'react';
import { useWeRun, useModal, useLogin } from 'taro-hooks';
import DemoContent from '@src/components/DemoContent';
import { Button } from '@taroify/core';
export default () => {
const { login } = useLogin();
const { get, share } = useWeRun();
const show = useModal({ mask: true, title: '设置结果', showCancel: false });
const handleGet = async () => {
let content = '';
try {
await login(true);
const result = await get();
content = JSON.stringify(result);
} catch (e) {
content = e.errMsg || e.message;
}
show({ content });
};
const handleShare = async () => {
let content = '';
try {
await login(true);
const recordList = [
{ typeId: 1000, time: 200, calorie: 100 },
{ typeId: 3000, time: 300, calorie: 1000, distance: 1000 },
{ typeId: 4000, calorie: 1000, number: 100 },
];
const result = await share(recordList);
content = JSON.stringify(result);
} catch (e) {
content = e.errMsg || e.message;
}
show({ content });
};
return (
<DemoContent>
<Button
block
color="primary"
className="gap"
onClick={handleGet}
shape="square"
>
获取微信运动步数
</Button>
<Button
block
color="primary"
className="gap"
onClick={handleShare}
shape="square"
>
分享数据至微信运动
</Button>
</DemoContent>
);
};
wechat/useWeRun/index
<template>
<demo-content>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="handleGet()"
>获取微信运动步数</nut-button
>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="handleShare()"
>分享数据至微信运动</nut-button
>
</demo-content>
</template>
<script setup lang="ts">
import { useWeRun, useModal, useLogin } from 'taro-hooks';
const { login } = useLogin();
const { get, share } = useWeRun();
const show = useModal({ mask: true, title: '设置结果', showCancel: false });
const handleGet = async () => {
let content = '';
try {
await login(true);
const result = await get();
content = JSON.stringify(result);
} catch (e) {
content = e.errMsg || e.message;
}
show({ content });
};
const handleShare = async () => {
let content = '';
try {
await login(true);
const recordList = [
{ typeId: 1000, time: 200, calorie: 100 },
{ typeId: 3000, time: 300, calorie: 1000, distance: 1000 },
{ typeId: 4000, calorie: 1000, number: 100 },
];
const result = await share(recordList);
content = JSON.stringify(result);
} catch (e) {
content = e.errMsg || e.message;
}
show({ content });
};
</script>
Hook 支持度
微信小程序 | H5 | ReactNative |
---|---|---|
✔️ |