UNPKG

taro-hooks

Version:
36 lines 1.21 kB
import { getUserInfo as taroGetUserInfo, getUserProfile as taroGetUserProfile } from '@tarojs/taro'; import { useState } from '@taro-hooks/core'; import usePromise from '../usePromise'; import useAuthorize from '../useAuthorize'; import { generateGeneralFail } from '../utils/tool'; function useUserInfo() { var _useState = useState(), userInfo = _useState[0], setUserInfo = _useState[1]; var _useAuthorize = useAuthorize(), get = _useAuthorize.get; var getInfo = usePromise(taroGetUserInfo); var getProfile = usePromise(taroGetUserProfile); var getUserInfo = function getUserInfo(options) { return get().then(function (res) { if (res.authSetting['scope.userInfo']) { return getInfo(options).then(function (res) { setUserInfo(res); return res; }); } return generateGeneralFail('getUserInfo', 'please auth scope.userInfo first'); }); }; var getUserProfile = function getUserProfile(option) { return getProfile(option).then(function (res) { setUserInfo(res); return res; }); }; return [userInfo, { getUserInfo: getUserInfo, getUserProfile: getUserProfile }]; } export default useUserInfo;