在src下创建一个utils的文件夹:
index.js:(import {baseURL} from '../config/index.js'相关的可先删除)
let baseURL = import.meta.env.VITE_APP_API_BASEURL
// 验证手机号是否正确
export const validatePhoneNumber = function (phoneNumber) {
const regex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
return regex.test(phoneNumber);
}
// 验证密码是否为强密码
export const validateStrongPassword = function (password) {
const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/;
return regex.test(password);
}
// 将json数据转换为url参数
export const transPrams = function (data) {
var str="";
for(var k in data){
str+=k+"="+data[k]+"&"
}
return str.slice(0,-1);
}
// 判断一个json对象是否为一个空对象
export const isEmptyObject= function(obj) {
if (obj === undefined || obj === null) {
return true;
}
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
// 判断是否为url
export const isUrl= function(url) {
const regex = '[a-zA-z]+://[^\s]*';
return regex.test(url);
}
// 判断一个字符串是否为null,''
export const isEmptyStr= function(str) {
return str === null || str === '' || str === undefined
}
// 获取一个文件的后缀名
export const getFileFormat= function(url) {
const match = url.match(/.+\.([^?]+)$/);
return match ? match[1] : null;
}
// 深拷贝
export const deepClone = (obj)=> {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj);
}
if (obj instanceof RegExp) {
return new RegExp(obj);
}
if (obj instanceof Array) {
const arrCopy = [];
for (let i = 0; i < obj.length; i++) {
arrCopy[i] = deepClone(obj[i]);
}
return arrCopy;
}
const objCopy = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
objCopy[key] = deepClone(obj[key]);
}
}
return objCopy;
}
// 获取拼接图片的连接
export const getImgUrl = (url) => {
if (isEmptyStr(baseURL)) {
return url;
}
var reg = /^https?/i;
// 如果不是https开头
if(!reg.test(url)){
// 地址加baseURl
url=baseURL+'/'+url;
}
var reg = /^http?/i;
// 如果不是http开头
if(!reg.test(url)){
// 地址加baseURl
url=baseURL+'/'+url;
}
return url
}
// 返回输入的月份有多少天
export const getDaysInMonth = (year, month) =>{
const totalDays = new Date(year, month, 0).getDate(); // 获取当月总天数
return Array.from({ length: totalDays }, (_, index) => index + 1);
}
// 获取当前的年份
export const getCurrentYear = () =>{
let date = new Date();
let year = date.getFullYear();
return year;
}
// 获取当前的月份
export const getCurrentMonth = () =>{
let date = new Date();
let month = date.getMonth() + 1; // getMonth()返回的月份是从0开始的,所以需要加1
return month;
}
// 获取当前的日期
export const getCurrentDate = () =>{
let date = new Date();
let currentDate = date.getDate();
return currentDate;
}
// 将时间戳转化为时间(时分)的格式
export const formatDateTime = (timestamp) =>{
// 创建一个Date对象,并设置时间戳
const date = new Date(timestamp);
// 获取小时和分钟
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
// 格式化小时和分钟为两位数,不足时补0
const formattedHours = hours < 10 ? '0' + hours : hours;
const formattedMinutes = minutes < 10 ? '0' + minutes : minutes;
const formattedSeconds = seconds < 10 ? '0' + seconds : seconds;
// 拼接成时间格式,例如:12:34:56
const time = formattedHours + ':' + formattedMinutes + ':' + formattedSeconds;
return time
}
// 判断一个字符串是不是json格式
export function isJSON(str) {
try {
const json = JSON.parse(str);
return typeof json === 'object' && json !== null;
} catch (error) {
return false;
}
}
// 判断日期字符串是不是未来的时间
export function isFutureDate(dateString) {
const inputDate = new Date(dateString);
const currentDate = new Date();
// 检查输入日期是否有效
if (isNaN(inputDate.getTime())) {
console.error('无效的日期字符串:', dateString);
return false;
}
// 比较输入日期和当前日期
if (inputDate > currentDate) {
return true; // 是未来的时间
} else {
return false; // 不是未来的时间
}
}
// 当前请求是否成功
export const isCodeTrue = (code) =>{
return code === 0 || code === '0';
}
// 当前时间是否在两个时间之前
export const isCurrentTimeBetween = (startTime,endTime) =>{
// 获取当前时间
const now = new Date();
// 判断当前时间是否在startTime和endTime之间
if (now >= startTime && now <= endTime) {
// 当前时间在startTime和endTime之间,返回0
return 0;
} else if (now < startTime) {
// 当前时间在startTime之前,返回-1
return -1;
} else {
// 当前时间在endTime之后,返回1
return 1;
}
}
// 对象数组去重
export const uniqueObjects = (arr) =>{
const uniqueArr = [];
for (let i = 0; i < arr.length; i++) {
if (!uniqueArr.find(item => JSON.stringify(item) === JSON.stringify(arr[i]))) {
uniqueArr.push(arr[i]);
}
}
return uniqueArr;
}
// 根据对象数组中的某一个key来检索某一个对象
export const findIndexByKeyValue = (array, key, value) => {
return array.findIndex(item => item[key] === value);
}