常用js函数

//封装request
export function request(config = {}) {
	const BASE_URL = 'http://runtuchigua.cn/api';
	const authToken = uni.getStorageSync('authToken');
	
    let {
        url,
        method = "GET",
        header = {
			Accept: 'application/json'
		},
        data,
		isShow = true
    } = config;

    url = BASE_URL + url;

	if(authToken){
		header.Authorization = `Bearer ${authToken}`;
	}

    // 显示加载提示
	if(isShow){
		uni.showLoading({
		    title: '加载中...',
		    mask: true
		});
	}
    
    return new Promise((resolve, reject) => {
        uni.request({
            url,
            method,
            header,
            data,
            success: (res) => {
				if(res.statusCode === 200){
					if (res.data.code === 200) {
					    resolve(res.data);
					} else if (res.data.code === 400){
						if(isShow){
							uni.showModal({
							    title: "错误提醒",
							    content: res.data.msg,
							    showCancel: false
							});
						}
					    reject(res.data);
					} else{
						if(isShow){
							uni.showToast({
								title: `错误代码:${res.statusCode}`,
								icon:"none"
							})
						}
						reject(res.data);
					}
				}
            },
            fail: (err) => {
				if (isShow){
					uni.showToast({
					    title: '请求失败,请稍后再试',
					    icon: "none"
					});
				}
                
                reject(err);
            },
            complete: () => {
                // 隐藏加载提示
				if(isShow){
					uni.hideLoading();
				}
            }
        });
    });
}
//将utc时间转换为北京时间
export function utcTocst(time, justTime = true){
	const date = new Date(time);
	const year = date.getUTCFullYear();
	let month = date.getUTCMonth() + 1; // 注意月份是从0开始的
	let day = date.getUTCDate();
	let hour = date.getUTCHours() + 8; // 加上8小时调整到CST
	if (hour >= 24) { // 处理跨天的情况
	    hour -= 24;
	    day += 1;
	}
	let minute = date.getUTCMinutes();
	let second = date.getUTCSeconds();
	
	// 确保两位数显示
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	hour = hour > 9 ? hour : '0' + hour;
	minute = minute > 9 ? minute : '0' + minute;
	second = second > 9 ? second : '0' + second;
	
	if(justTime){
		return `${hour}:${minute}:${second}`;
	}else{
		return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
	}
	  
}
//获取时间差
export function getTimeDifference(dateTime){
	const nowTime = new Date().getTime();
	const beforeTime = new Date(dateTime).getTime();
	const diffTime = parseInt((nowTime - beforeTime) / 1000);
	
	if (diffTime < (60 * 60 * 24)){
		return "不到1天";
	}else if(diffTime < (60 * 60 * 24 * 30)){
		return Math.floor(diffTime / (60 * 60 * 24)) + "天";
	}else if(diffTime < (60 * 60 * 24 * 30 * 12)){
		return Math.floor(diffTime / (60 * 60 * 24 * 30)) + "个月";
	}else{
		return Math.floor(diffTime / (60 * 60 * 24 * 30 * 12)) + "年";
	}
}
//获取指定时间毫秒级时间戳
export function getTime(time) {
    const res = new Date(time);
    if (isNaN(res.getTime())) {
        throw new Error(`Invalid time value: ${time}`);
    }
    return res.getTime();
}
//获取当前毫秒级时间戳
export function nowTime(){
	let res = new Date()
	return res.getTime()
}
// 获取该月有多少天
export function getDayTheMonth(){
	const today = new Date();
	const year = today.getFullYear();
	const month = today.getMonth();
	const days = new Date(year, month + 1, 0).getDate();
	return days;
}
// 获取该月有多少天
export function getDayTheMonth(){
	const today = new Date();
	const year = today.getFullYear();
	const month = today.getMonth();
	const days = new Date(year, month + 1, 0).getDate();
	return days;
}
// 获取这个月的第一天是星期几
export function getWeekTheMonth(justNum = false){
	const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
	const today = new Date();
	const year = today.getFullYear();
	const month = today.getMonth();
	const week = new Date(year, month, 1).getDay();
	if(justNum){
		return week;
	}else{
		return weekDays[week];
	}
	
}
// 获取今天是几号
export function getTodayIsDay(){
	const today = new Date();
	const theDay = today.getDate();
	return theDay;
}
// 获取当前年月日
export function nowDate(){
	const date = new Date();
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0'); // 注意:月份从0开始
	const day = String(date.getDate()).padStart(2, '0');
	const formatDate = `${year}-${month}-${day}`;
	return formatDate
}
// 获取当前时分秒
export function nowHourMinuteSecond(){
	const date = new Date();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	const formatDate = `${hour}:${minute}:${second}`;
	return formatDate
}
//将数字转换为中国货币形式
export function numberToLocaleString(number){
	if (number === '') number = '0.00';
	return parseFloat(number).toLocaleString("zh-CN", {style:"currency", currency:"CNY"})
}
//数字前面补零,保证结果是两位数
export function addZero(i){
	return i.toString().padStart(2, '0');
}