import dayjs from "dayjs";
import { serverUrl } from "./wxrequest";
import { ACCESS_TOKEN,OPENID,USER_NAME,USER_INFO,REG_INFO } from "./constants"
export const setToken = (token) => window.localStorage.setItem(ACCESS_TOKEN, token);

export const getToken = () => window.localStorage.getItem(ACCESS_TOKEN);

export const removeToken = () => window.localStorage.removeItem(ACCESS_TOKEN);

export const setOpenid = (openid) => window.localStorage.setItem(OPENID, openid);

export const getOpenid = () => window.localStorage.getItem(OPENID);

export const setUsername = (username) => window.localStorage.setItem(USER_NAME, username);

export const getUsername = () => window.localStorage.getItem(USER_NAME);

export const setUserinfo = (userinfo) => window.localStorage.setItem(USER_INFO, JSON.stringify(userinfo));

export const getUserinfo = () => JSON.parse(window.localStorage.getItem(USER_INFO));

export const setReginfo = (reginfo) => window.localStorage.setItem(REG_INFO, JSON.stringify(reginfo));

export const getReginfo = () => JSON.parse(window.localStorage.getItem(REG_INFO));

export const delImg = (url) => {
  if (url) {
    if (url.startsWith("http")) return url;
    return serverUrl + url;
  }
  return "https://img2.baidu.com/it/u=3251476880,269103712&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500";
};

export const getToday = () => dayjs().format("YYYY-MM-DD"); //获取当前的日期

export const isCheckedToday = () => {
  //判断当天是否签到过
  const today = getToday();
  if (localStorage.getItem("check-today")) {
    if (localStorage.getItem("check-today") == today) {
      return true;
    }
    localStorage.setItem("check-today", today);
    return false;
  }
  localStorage.setItem("check-today", today);
  return false;
};