diff --git a/hooks/useGetUserInfo/index.d.ts b/hooks/useGetUserInfo/index.d.ts new file mode 100644 index 0000000..57f3642 --- /dev/null +++ b/hooks/useGetUserInfo/index.d.ts @@ -0,0 +1,7 @@ +export type getUserInfoFunction = () => Promise>; + +/** + * 获取用户信息 + */ +export default function useGetUserInfo(returnType: "array"): [boolean, getUserInfoFunction]; +export default function useGetUserInfo(returnType?: "object"): { loading: boolean; getDictionary: getUserInfoFunction }; diff --git a/hooks/useGetUserInfo/index.js b/hooks/useGetUserInfo/index.js new file mode 100644 index 0000000..3ac40a5 --- /dev/null +++ b/hooks/useGetUserInfo/index.js @@ -0,0 +1,39 @@ +import { request } from "@cqsjjb/jjb-common-lib/http"; +import { useState } from "react"; + +/** + * 获取用户信息 + */ +function useGetUserInfo(returnType = "object") { + // loading状态 + const [loading, setLoading] = useState(false); + + // 获取用户信息 + const getUserInfo = () => { + setLoading(true); + + return new Promise((resolve, reject) => { + // 发送请求 + request( + "/basic-info/user/getInfo", + "get", + {}, + ) + .then((res) => { + resolve(res.data); + }) + .catch((err) => { + reject(err); + }) + .finally(() => { + setLoading(false); + }); + }); + }; + + if (returnType === "array") + return [loading, getUserInfo]; + return { loading, getUserInfo }; +} + +export default useGetUserInfo;