跳到主要内容

useLogin

获取登录凭证, 检查登录状态

何时使用

当需要获取登录凭证, 检查登录状态

API

const { check, login } = useLogin();

返回值说明

返回值说明类型
login获取登录凭证(若needChecktrue则自动检测当前登录状态来进行登录操作, plugintrue则为插件登录)(needCheck?: boolean, plugin?: boolean, timeout?: number) => Promise<Taro.login.SuccessCallbackResult>
check检查登录状态() => Promise<General.CallbackResult>

代码演示

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>
);
};

Hook 支持度

微信小程序H5ReactNative
✔️