123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { GetLogo } from '@/api/app';
- import { setConfig } from './auth';
- /**
- * 格式化 Date
- * @param {Date} date
- * @returns {String} yyyy-mm-dd
- */
- export function formatDate(date) {
- if (arguments.length <= 0 || !date) {
- return null;
- }
- return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
- }
- /**
- * @param {Object} time
- * @param {string} cFormat
- * @returns {string | null}
- */
- export function parseTime(time, cFormat) {
- if (arguments.length === 0 || !time) {
- return null;
- }
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
- const formatObj = {
- y: time.getFullYear(),
- m: time.getMonth() + 1,
- d: time.getDate(),
- h: time.getHours(),
- i: time.getMinutes(),
- s: time.getSeconds(),
- a: time.getDay()
- };
- const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
- const value = formatObj[key];
- // Note: getDay() returns 0 on Sunday
- if (key === 'a') {
- return ['日', '一', '二', '三', '四', '五', '六'][value];
- }
- return value.toString().padStart(2, '0');
- });
- return time_str;
- }
- export function getConfigInformation() {
- return new Promise((resolve, reject) => {
- GetLogo()
- .then((res) => {
- setConfig(res);
- resolve(res);
- })
- .catch((err) => {
- reject(err);
- });
- });
- }
- // 对小于 10 的补零
- export function zeroFill(val) {
- if (val < 10) return `0${val}`;
- return val;
- }
- /**
- * 将秒转为时:分:秒格式
- * @param {Number|String} val 秒
- * @param {'normal'|'chinese'} type 格式类型
- * @returns {String} hh:MM:ss 小于1小时返回 MM:ss
- */
- export function secondFormatConversion(val, type = 'normal') {
- const seconds = parseInt(val); // 输入的秒数
- const hours = Math.floor(seconds / 3600); // 小时部分
- const minutes = Math.floor((seconds % 3600) / 60); // 分钟部分
- const remainingSeconds = seconds % 60; // 剩余的秒数
- // 使用零填充函数来格式化小时、分钟和秒
- const formattedHours = zeroFill(hours);
- const formattedMinutes = zeroFill(minutes);
- const formattedSeconds = zeroFill(remainingSeconds);
- // 根据时间范围返回不同的格式
- if (hours > 0) {
- if (type === 'chinese') {
- return `${hours}时${minutes}分${remainingSeconds}秒`;
- }
- return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
- }
- if (type === 'chinese') {
- return `${minutes}分${remainingSeconds}秒`;
- }
- return `${formattedMinutes}:${formattedSeconds}`;
- }
|