220 lines
6.5 KiB
JavaScript
220 lines
6.5 KiB
JavaScript
"use client";
|
||
import { Toast } from "antd-mobile";
|
||
//格式化时间戳
|
||
export function formatDeadline(timestamp) {
|
||
const date = new Date(timestamp * 1000); // 时间戳以秒为单位,需要乘以1000转换成毫秒
|
||
const year = date.getFullYear();
|
||
const month = ("0" + (date.getMonth() + 1)).slice(-2);
|
||
const day = ("0" + date.getDate()).slice(-2);
|
||
const hours = ("0" + date.getHours()).slice(-2);
|
||
const minutes = ("0" + date.getMinutes()).slice(-2);
|
||
const seconds = ("0" + date.getSeconds()).slice(-2);
|
||
|
||
return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
|
||
}
|
||
|
||
// 格式化时间戳-完整
|
||
export function formatTimestamp(timestamp) {
|
||
const now = new Date();
|
||
const date = new Date(timestamp * 1000);
|
||
|
||
// 检查是否是今天
|
||
if (now.toDateString() === date.toDateString()) {
|
||
return date.toLocaleTimeString(undefined, {
|
||
hour: "numeric",
|
||
minute: "numeric",
|
||
});
|
||
}
|
||
|
||
// 获取昨天的日期
|
||
const yesterday = new Date(now);
|
||
yesterday.setDate(now.getDate() - 1);
|
||
|
||
// 检查是否是昨天
|
||
if (yesterday.toDateString() === date.toDateString()) {
|
||
return `昨天 ${date.getHours()}:${String(date.getMinutes()).padStart(
|
||
2,
|
||
"0"
|
||
)}`;
|
||
}
|
||
|
||
// 检查是否是今年
|
||
if (now.getFullYear() === date.getFullYear()) {
|
||
return `${date.getMonth() + 1}-${date.getDate()} ${String(
|
||
date.getHours()
|
||
).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
|
||
}
|
||
|
||
// 不是今年,返回完整日期和时间
|
||
return `${date.getFullYear()}-${
|
||
date.getMonth() + 1
|
||
}-${date.getDate()} ${String(date.getHours()).padStart(2, "0")}:${String(
|
||
date.getMinutes()
|
||
).padStart(2, "0")}`;
|
||
}
|
||
|
||
// 格式化时间戳-简洁
|
||
export function formatDate(timestamp) {
|
||
const nowTime = new Date();
|
||
const date = new Date(timestamp * 1000);
|
||
const year = date.getFullYear();
|
||
const month = date.getMonth() + 1; // 月份从0开始,所以需要加1
|
||
const day = date.getDate();
|
||
const hours = date.getHours();
|
||
const minutes = date.getMinutes();
|
||
if (nowTime.getTime() - 24 * 60 * 60 * 1000 > date.getTime()) {
|
||
return `${month}/${day}`;
|
||
} else {
|
||
return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(
|
||
2,
|
||
"0"
|
||
)}`;
|
||
}
|
||
}
|
||
|
||
// 防抖函数
|
||
export function debounce(fn, delay) {
|
||
let timer = null;
|
||
return (fnn) => {
|
||
//清除上一次的延时器
|
||
if (timer) {
|
||
clearTimeout(timer);
|
||
// return;
|
||
// console.log(timer);
|
||
}
|
||
//重新设置新的延时器
|
||
timer = setTimeout(() => {
|
||
//修改this指向问题
|
||
// fn.apply(this,value)
|
||
fn(fnn);
|
||
}, delay);
|
||
};
|
||
}
|
||
|
||
export function utf8Length(str) {
|
||
let length = 0;
|
||
for (let i = 0; i < str.length; i++) {
|
||
const code = str.charCodeAt(i);
|
||
if (code < 0x80) {
|
||
// 0xxxxxxx
|
||
length += 0.5;
|
||
} else if (code < 0x800) {
|
||
// 110xxxxx 10xxxxxx
|
||
length += 2;
|
||
} else if (code < 0x10000) {
|
||
// 1110xxxx 10xxxxxx 10xxxxxx
|
||
length += 1;
|
||
} else if (code <= 0x10ffff) {
|
||
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||
length += 4;
|
||
}
|
||
}
|
||
return length;
|
||
}
|
||
|
||
export function getVideoBase64(url) {
|
||
return new Promise((resolve, reject) => {
|
||
// 创建 video 元素
|
||
const video = document.createElement("video");
|
||
// 设置跨域属性
|
||
video.crossOrigin = "anonymous";
|
||
// 设置视频 URL
|
||
video.src = url;
|
||
// 设置视频画面宽度
|
||
video.width = "90";
|
||
// 设置视频画面高度
|
||
video.height = "90";
|
||
// 设置视频播放位置为第一帧
|
||
video.currentTime = 1;
|
||
// 设置预加载方式为自动加载
|
||
video.preload = "auto";
|
||
|
||
// 创建 canvas 元素
|
||
const canvas = document.createElement("canvas");
|
||
// 将 canvas 的宽度和高度设置为视频画面的宽度和高度
|
||
canvas.width = video.width;
|
||
canvas.height = video.height;
|
||
|
||
// 判断是否为 iOS 系统
|
||
if (/(iPad|iPhone|iPod)/gi.test(window && window.navigator?.userAgent)) {
|
||
// 在 iOS 系统中,需要手动触发视频加载,然后设置自动播放和静音属性
|
||
video.load();
|
||
// video.autoplay = true;
|
||
|
||
// 在 iOS 系统中,监听 loadedmetadata 事件可以保证视频数据已经全部加载完毕
|
||
}
|
||
video.muted = true;
|
||
video.addEventListener("loadeddata", function () {
|
||
setTimeout(function () {
|
||
// console.log("监听到了listen:", "loadeddata");
|
||
canvas
|
||
.getContext("2d")
|
||
.drawImage(video, 0, 0, canvas.width, canvas.height); //绘制canvas
|
||
const dataURL = canvas.toDataURL("image/jpeg"); //转换为base64
|
||
video.setAttribute("poster", dataURL);
|
||
// console.log(dataURL, "dataURL");
|
||
resolve(dataURL);
|
||
|
||
// 清除内存
|
||
canvas.remove();
|
||
video.remove();
|
||
}, 100);
|
||
});
|
||
|
||
// 监听视频事件,在发生错误时 Promise 会 reject
|
||
video.addEventListener("error", function () {
|
||
// reject(new Error("视频加载错误"));
|
||
Toast.show({
|
||
icon: "fail",
|
||
content: "请更换浏览器,IOS用户建议使用Safari。",
|
||
position: "top",
|
||
});
|
||
// 清除内存
|
||
canvas.remove();
|
||
video.remove();
|
||
});
|
||
});
|
||
}
|
||
|
||
export function getcountLines(str) {
|
||
const newStr = str.startsWith("\n") ? str.slice(1) : str;
|
||
const lines = newStr.split("\n").filter((line) => line.trim() !== "");
|
||
return lines.length;
|
||
}
|
||
|
||
// 跳转页面
|
||
export function goToPage(link) {
|
||
let linkArr = link.split("?");
|
||
const params = linkArr[1]?.split("&");
|
||
|
||
if (!params) return linkArr[0];
|
||
// if (linkArr[0].includes("/")) {
|
||
// const path = linkArr[0].replace(/^\/+|\/+$/g, "");
|
||
// const pathNames = path.split("/").filter((it) => it != "");
|
||
// return [pathNames[0], { screen: pathNames[1] }];
|
||
// }
|
||
const query = params
|
||
.map((it) => ({ [it.split("=")[0]]: it.split("=")[1] }))
|
||
.reduce((acc, cur) => ({ ...acc, ...cur }), {});
|
||
return [linkArr[0], query];
|
||
}
|
||
|
||
// 尝试使用浏览器打开URL
|
||
export function openUrlWithBrowser(url) {
|
||
try {
|
||
const userAgent = window && window.navigator?.userAgent;
|
||
//区分设备类型
|
||
if (/Android/i.test(userAgent)) {
|
||
var intentUrl =
|
||
"intent://app.tiefen.fun/#Intent;scheme=https;package=com.android.chrome;end";
|
||
window.location.href = intentUrl;
|
||
} else if (/iPhone|iPad|iPod/i.test(userAgent)) {
|
||
window.location.href = `x-safari-https://app.tiefen.fun${url}`;
|
||
} else {
|
||
// setDeviceType("pc");
|
||
}
|
||
} catch (e) {
|
||
// 如果谷歌浏览器未安装,使用系统默认浏览器打开URL
|
||
}
|
||
}
|