95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
export const scrollToTop = (scrollTop = 0) => {
|
||
uni.pageScrollTo({
|
||
scrollTop,
|
||
duration: 0,
|
||
});
|
||
};
|
||
|
||
// 查看新闻距离当前多少时间
|
||
export const formatTime = (timestamp: any) => {
|
||
const now = new Date();
|
||
const targetTime = new Date(timestamp);
|
||
const diff = (now - targetTime) / 1000; // 时间差,单位为秒
|
||
|
||
if (diff < 60) {
|
||
return "刚刚";
|
||
} else if (diff < 600) {
|
||
const minutes = Math.floor(diff / 60);
|
||
return `${minutes}分钟前`;
|
||
} else {
|
||
const hours = targetTime.getHours();
|
||
const minutes = targetTime.getMinutes();
|
||
return `${hours}:${minutes.toString().padStart(2, "0")}`;
|
||
}
|
||
};
|
||
|
||
// 将【实时快讯】接口返回的数据,根据时间进行分组
|
||
export const formatData = (list: any) => {
|
||
let result = [];
|
||
// 根据 day 对数据进行分组
|
||
const groupedByDay = list.reduce((acc: any, item: any) => {
|
||
const { day } = item;
|
||
if (!acc[day]) {
|
||
acc[day] = [];
|
||
}
|
||
acc[day].push(item);
|
||
return acc;
|
||
}, {});
|
||
|
||
for (let key in groupedByDay) {
|
||
result.push({
|
||
day: key,
|
||
id: key,
|
||
children: groupedByDay[key],
|
||
});
|
||
}
|
||
return result;
|
||
};
|
||
|
||
// 对搜索的文本进行处理
|
||
export const extractText = (text: string, keyword: string) => {
|
||
const index = text.indexOf(keyword);
|
||
if (index === -1) {
|
||
return text; // 如果没有找到关键字,返回全部文本
|
||
} else {
|
||
const start = Math.max(0, index - 15); // 确保开始位置不小于0
|
||
const end = Math.min(text.length, index + keyword.length + 15); // 确保结束位置不超过文本长度
|
||
return text.substring(start); // 截取并返回结果
|
||
// return text.substring(start, end); // 截取并返回结果
|
||
}
|
||
};
|
||
|
||
export const formatTimestamp = (timestamp) => {
|
||
var date = new Date(timestamp);
|
||
var year = date.getFullYear();
|
||
var month = date.getMonth() + 1;
|
||
var day = date.getDate();
|
||
month = month < 10 ? "0" + month : month;
|
||
day = day < 10 ? "0" + day : day;
|
||
// 拼接成最终的日期字符串
|
||
return year + "-" + month + "-" + day;
|
||
};
|
||
|
||
// 登录手机号格式验证
|
||
export const validatePhoneNumber = (phoneNumber) => {
|
||
const phoneRegex = /^1[3-9]\d{9}$/;
|
||
return phoneRegex.test(phoneNumber);
|
||
};
|
||
|
||
// 跳转外链
|
||
export const jumpUrl = (url) => {
|
||
window.open(url, "_blank");
|
||
};
|
||
|
||
|
||
export function getNavHeight() {
|
||
const systemInfo = uni.getSystemInfoSync();
|
||
console.log(systemInfo);
|
||
const statusBarHeight = systemInfo.statusBarHeight; // 状态栏高度
|
||
const navigationBarHeight = systemInfo.platform === 'android' ? statusBarHeight + 48 : statusBarHeight + 44; // 通常安卓底部有48px的导航栏,iOS是44px
|
||
console.log('状态栏高度:', statusBarHeight);
|
||
console.log('导航栏高度:', navigationBarHeight);
|
||
|
||
return statusBarHeight
|
||
|
||
} |