import router from "@/router";
import { getStaffOnDutyStatus } from "@/service/StaffService";
import { eRole } from "@/types/roleType";
import { getUserMsg } from "./userMsg";

export function authCheck(cb: () => void) {
  if (getUserMsg()) cb();
  else {
    router.push({
      name: "Login",
    });
  }
}

/**
 *
 * @param cb 有权限时回调
 * @param cb2 没有权限时回调 什么时候没有权限？本地无token，用户在其他地方登录，不在岗或被禁用了
 */
export async function chatAuthCheck(
  cb: () => void,
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  cb2?: (arg: any) => void
) {
  const msg = getUserMsg();
  if (msg?.role2 === eRole.user) {
    cb();
  } else {
    const ret = await getStaffOnDutyStatus();
    if (ret.code === 200 && ret.data) {
      cb();
    } else {
      cb2 && cb2(ret.data);
    }
  }
}
