import { loginAuth, checkPhone, bindPhone } from "@/manager/LoginManager";

// 跳转登录
export const goLogin = () => uni.reLaunch({ url: "/pages/login/index" });

// 登录逻辑
export const onLogin = (e) => {
  const token = uni.getStorageSync("token");

  if (token) return;

  const { code } = e.detail;
  console.info("onLogin code: ", code);

  loginAuth().then(async () => {
    // 检测是否绑定手机号
    const res = await checkPhone();

    if (res.data) return;

    const params = { wechatPhoneCode: code, clientId: "2" };

    // 绑定手机号
    bindPhone(params);

    // 通知刷新
    uni.$emit("TOKEN_CHANGE");
  });
};

// 检测token
export const checkToken = async () => {
  return new Promise((resolve) => {
    const token = uni.getStorageSync("token");

    if (!token) return;

    resolve(token);
  });
};
