tiefen_space_h5/utils/auth.js

75 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-07-02 15:09:48 +08:00
import { setCookie, deleteCookie, getCookie } from "cookies-next";
2024-07-22 16:07:41 +08:00
import requireAPI from "./requireAPI";
2024-12-27 12:51:47 +08:00
import { get, save } from "./storeInfo";
2024-07-02 15:09:48 +08:00
export async function checkAuth() {
2024-07-06 11:05:19 +08:00
try {
2024-07-22 16:07:41 +08:00
const data = await requireAPI("POST", `/api/login/validate`);
2024-09-09 15:34:31 +08:00
if (data.ret === 1) {
2024-08-06 16:21:34 +08:00
return true;
2024-09-09 15:34:31 +08:00
} else {
2024-08-06 16:21:34 +08:00
return false;
}
2024-07-06 11:05:19 +08:00
} catch (e) {
2024-09-09 15:34:31 +08:00
// console.log(e);
2024-07-02 15:09:48 +08:00
}
}
export function signIn(data) {
setCookie("token", data.data.token, { maxAge: 60 * 60 * 24 * 365 * 10 });
setCookie("mid", data.data.account.mid, { maxAge: 60 * 60 * 24 * 365 * 10 });
2024-07-02 15:09:48 +08:00
}
export function signOut() {
deleteCookie("token");
2024-07-06 11:05:19 +08:00
deleteCookie("mid");
2024-07-02 15:09:48 +08:00
}
2024-12-27 12:51:47 +08:00
export async function getUserInfo(mid) {
if (!mid) return;
try {
const data = await requireAPI("POST", `/api/account/list_by_mid`, {
body: { mid },
});
if (data.ret === 1) {
return data.data.account;
}
} catch (e) {
// console.log(e);
}
}
export async function getVipPrice() {
try {
const data = await requireAPI(
"POST",
`/api/vas/get_membership_product_list`
);
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
} else {
return data.data.product.real_price / 100;
}
} catch (e) {
// console.log(e);
}
}
export async function checkRole() {
2024-08-05 21:10:36 +08:00
const account = get("account");
2024-12-27 12:51:47 +08:00
const userInfo = await getUserInfo(account.mid);
if (userInfo) {
if (userInfo.is_a_member !== account) {
save("account", userInfo);
}
}
const role = userInfo?.role;
const isVip = userInfo?.is_a_member;
return role !== 0 || isVip === 1;
2024-08-05 21:10:36 +08:00
}