1.util.js代码
import { config } from "../config.js";
const { imgsDomain } = config;
//常用功能封装 目前收录方法有:像素处理,日期处理,数组处理
//像素处理
// 遇到必须使用px的地方,保证适配,封装rpx转对应px方法
export const {
pixelRatio: dpr,
windowWidth,
windowHeight,
system,
} = wx.getSystemInfoSync();
const rate = windowWidth / 750;
export function rpx2px(rpx) {
return rate * rpx;
}
//日期处理
// 封装日期格式为年-月-日
const formatTime = (date) => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
// return `${[year, month, day].map(formatNumber).join("/")} ${[hour, minute, second].map(formatNumber).join(":")}`;
return `${[year, month, day].map(formatNumber).join(".")} ${[hour, minute, second].map(formatNumber).join(":")}`;
};
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
// 年月日过滤
const formatDate = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return `${[year, month].map(formatNumber).join('年') + '月'}${[day].map(formatNumber).join('') + '日'} `
}
// 时分过滤
const formatHour = date => {
const hour = date.getHours()
const minute = date.getMinutes()
return `${[hour, minute].map(formatNumber).join(':')}`
}
// 周过滤
const formatDay = date => {
const day = date.getDay()
let days = ''
switch (day){
case 1:
days='周一';
break;
case 2:
days='周二';
break;
case 3:
days ='周三';
break;
case 4:
days ='周四';
break;
case 5:
days ='周五';
break;
case 6:
days ='周六';
break;
case 0:
days ='周日';
break;
}
return `${[days].map(formatNumber).join('')}`
}
const formatNumber = (n) => {
n = n.toString();
return n[1] ? n : `0${n}`;
};
// 数组处理
/**
* 给数组数据的audio属性添加domain
* @param {*} arr [{name,audio}]
* @returns [] 包含完整路径的数据
*/
const fillAudioDomain = function (arr = []) {
return arr.map((item) => {
if (!item.audio.includes(imgsDomain)) {
item.audio = `${imgsDomain}${item.audio}`;
}
return item;
});
};
// 数组处理
/**
* 给imgs数组数据的path属性添加domain
* @param {*} arr [{name,path}]
* @returns [] 包含完整路径的图片数组数据
*/
const fillImgDomain = function (arr = []) {
return arr.map((item) => {
if (!item.path.includes(imgsDomain)) {
item.path = `${imgsDomain}${item.path}`;
}
return item;
});
};
// 数组处理
/**
* 将图片数组转换为[{name:path}]格式
* @param {*} arr [{name,path}]
* @returns {}
*/
const getImgNameByVal = function (arr = []) {
const result = {};
for (let i in arr) {
result[arr[i].name] = arr[i].path;
}
return result;
};
// 数组处理
/**
* 获取图片数组数据中的path部分
* @param {*} arr [{name,path}]
* @returns []
*/
const getImgPath = function (arr = []) {
return arr.map((item) => item.path);
};
// 数字转换成欧洲格式 如"123,456,789"
const formatNum = (num) => {
if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)) {
alert("wrong!");
return num;
}
var a = RegExp.$1,
b = RegExp.$2,
c = RegExp.$3;
var re = new RegExp().compile("(\\d)(\\d{3})(,|$)");
while (re.test(b)) b = b.replace(re, "$1,$2$3");
return a + "" + b + "" + c;
}
module.exports = {
formatTime,
fillAudioDomain,
fillImgDomain,
getImgNameByVal,
getImgPath,
formatNum,
formatDay,
formatHour,
formatDate
};
2.
3.引入
import {
formatNum
} from '../../utils/functiontools/util';
onLoad: function (options) {
console.log(formatNum(525441155))
},