Files
YGChatCS/src/hooks/useGoLogin.js
2025-10-12 13:55:35 +08:00

60 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { loginAuth, bindPhone, checkPhone } from "@/manager/LoginManager";
import { clientId } from "@/constant/base";
import { useAppStore } from "@/store";
// 跳转登录
export const goLogin = () => uni.navigateTo({ url: "/pages/login/index" });
// 登录成功后,返回上一页
export const goBack = () => uni.navigateBack({ delta: 1 });
// 登录逻辑
export const onLogin = async (e) => {
console.info("onLogin code: ", e);
return new Promise(async (resolve, reject) => {
// 判断用户拒绝
if (e !== undefined && e && e.detail && e.detail.errno === 104) {
reject();
return;
}
await loginAuth(e).then(async () => {
// 检查手机号是否绑定
const checkRes = await checkPhone();
if (checkRes.data) {
resolve();
return;
}
if (e === undefined) {
resolve();
return;
}
const { code } = e.detail;
// 绑定手机号
const params = { wechatPhoneCode: code, clientId: clientId };
const res = await bindPhone(params);
if (res.data) {
resolve();
}
});
});
};
// 检测token
export const checkToken = () => {
return new Promise((resolve) => {
const appStore = useAppStore();
console.log("appStore.hasToken: ", appStore.hasToken);
if (!appStore.hasToken) {
console.log("没有token跳转到登录页");
goLogin();
return;
}
resolve();
});
};