/* * @Author: cclu 1106109051@qq.com * @Date: 2024-12-04 16:02:53 * @LastEditors: cclu 1106109051@qq.com * @LastEditTime: 2025-01-20 14:23:01 * @FilePath: \wanmeiyizhan\utils\index.js * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ function formatNumber(n) { const str = n.toString() return str[1] ? str : `0${str}` } export function 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() const t1 = [year, month, day].map(formatNumber).join('/') const t2 = [hour, minute, second].map(formatNumber).join(':') return `${t1} ${t2}` } export function getLocalTime(now, type) { type = type || 'YYYY-MM-DD hh:mm:ss' now = new Date(now) var year = now.getFullYear() var month = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1) // 获取当前月份的日期,不足10补0 var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate() // 获取当前几号,不足10补0 var hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours() var minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes() var second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds() return type.replace('YYYY', year).replace('MM', month).replace('DD', date).replace('hh', hour).replace('mm', minute).replace('ss', second) // return year + '-' + month + '-' + date + ' ' + hour + ':' + minute // + ":" + second; } // 转义日期格式 function cutDate(dd, type, daynum) { dd = new Date(dd) || new Date() type = type || 'YYYY/MM/DD' // hh:mm:ss daynum = daynum || 0 dd.setDate(dd.getDate() + daynum) // 获取AddDayCount天后的日期 var y = dd.getFullYear() var m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1) // 获取当前月份的日期,不足10补0 var d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0 var h = dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours() var mi = dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes() var s = dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds() return type.replace('YYYY', y).replace('MM', m).replace('DD', d).replace('hh', h).replace('mm', mi).replace('ss', s) } // 生成二维码和条形码 let barcode = require('../common/JsBarcode.all.js') let qrcode = require('../common/qarcode.js') export function barc(id, code, width, height, rolate) { barcode.code128(uni.createCanvasContext(id), code, width, height, rolate) } function qrc(id, code, width, height, ecc, src) { ecc = ecc || null src = src || null qrcode.qrApi.draw(code, id, width, height, ecc, src) } // 一维数组按照c成二维数组 /* array 需要分割的数组 subGroupLen 按照多少长度分割 */ const ArrayGroup = (array, subGroupLen) => { let index = 0 let newArray = [] while (index < array.length) { newArray.push(array.slice(index, index += subGroupLen)) } return newArray } export function wrapTreeNode(data) { const wrapData = data.map((item) => { const node = { ...item.node }; if (item.children && item.children.length > 0) { node.children = wrapTreeNode(item.children); } return node }); return wrapData; } export function formatDate(dateStr) { const date = new Date(dateStr); // 获取月份和日期,确保是两位数 const month = (date.getMonth() + 1).toString().padStart(2, '0'); // getMonth() 返回的是 0 基的月份 const day = date.getDate().toString().padStart(2, '0'); // getDate() 返回的是日 // 返回格式化后的结果 return `${month}.${day}`; } // 格式化数字 export function handleFormatNumber(num) { // 将数字转为字符串,避免浮点数精度问题 let str = num.toString(); // 如果有小数点 if (str.indexOf('.') !== -1) { // 截取小数点后两位,不做四舍五入 let decimalPart = str.split('.')[1]; if (decimalPart.length > 2) { return str.split('.')[0] + '.' + decimalPart.slice(0, 2); } return str; } else { // 没有小数点,补齐后两位 return str + '.00'; } } export default { formatNumber, formatTime, getLocalTime, ArrayGroup, barc, qrc, cutDate, wrapTreeNode, formatDate, handleFormatNumber }