useLogin
获取登录凭证, 检查登录状态
何时使用
当需要获取登录凭证, 检查登录状态
API
const { check, login } = useLogin();
返回值说明
返回值 | 说明 | 类型 |
---|---|---|
login | 获取登录凭证(若needCheck 为true 则自动检测当前登录状态来进行登录操作, plugin 为true 则为插件登录) | (needCheck?: boolean, plugin?: boolean, timeout?: number) => Promise<Taro.login.SuccessCallbackResult> |
check | 检查登录状态 | () => Promise<General.CallbackResult> |
代码演示
- 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 |
---|---|---|
✔️ |