import { navigateTo, getStorageSync, getSystemInfoSync } from "@tarojs/taro";
const verifyPhone = (phone: string) => {
  return /^1[3-9]\d{9}$/.test(phone);
};

const isLogin = () => {
  const Authorization = getStorageSync("Authorization");
  if (!Boolean(Authorization)) {
    navigateTo({ url: "/pages/login/index" });
  }
  return Boolean(Authorization);
};

const strSlice = (str: string, length: number) => {
  return str.length > length ? str.slice(0, length) + "..." : str;
};

//
// 传入 2025-09-08 16:42:26， 输出：转化时间，多少小时前，多少天前，多少星期前，多少月前，多少年前
const timeFormat = (time: string) => {
  const now = new Date();
  const timeDate = new Date(time);
  const diff = now.getTime() - timeDate.getTime();
  const diffHours = diff / (1000 * 60 * 60);
  const diffDays = diff / (1000 * 60 * 60 * 24);
  const diffWeeks = diff / (1000 * 60 * 60 * 24 * 7);
  const diffMonths = diff / (1000 * 60 * 60 * 24 * 30);
  const diffYears = diff / (1000 * 60 * 60 * 24 * 365);
  if (diffYears > 1) {
    return diffYears.toFixed(0) + "年前";
  } else if (diffMonths > 1) {
    return diffMonths.toFixed(0) + "月前";
  } else if (diffWeeks > 1) {
    return diffWeeks.toFixed(0) + "周前";
  } else if (diffDays > 1) {
    return diffDays.toFixed(0) + "天前";
  } else if (diffHours > 1) {
    return diffHours.toFixed(0) + "小时前";
  } else {
    return "刚刚";
  }
};

// 获取安卓/ios的系统
const getSystemInfo = () => {
  const systemInfo: any = getSystemInfoSync();
  return systemInfo.system === "AndroidOS" ? "android" : "ios";
};

export { verifyPhone, isLogin, strSlice, timeFormat, getSystemInfo };
