From 715dbef032d6fadd9bd05f8785f6dc631de17de2 Mon Sep 17 00:00:00 2001 From: jlzhou <12020042@qq.com> Date: Tue, 9 May 2023 17:07:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8E=9F=E5=9E=8B=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E5=92=8C=E5=91=A8=E6=9C=9F=E6=80=A7=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 3 + src/utils/index.js | 8 +++ src/utils/modules/prototypes.js | 122 ++++++++++++++++++++++++++++++++ src/utils/modules/timerEvent.js | 25 +++++++ 4 files changed, 158 insertions(+) create mode 100644 src/utils/modules/prototypes.js create mode 100644 src/utils/modules/timerEvent.js diff --git a/src/main.js b/src/main.js index 065fa4d..65a19e1 100644 --- a/src/main.js +++ b/src/main.js @@ -75,6 +75,9 @@ app.component('svg-icon', SvgIcon) directive(app) +import {util} from '@/utils' +app.use(util) + // 使用element-plus 并且设置全局的大小 app.use(ElementPlus, { locale: locale, diff --git a/src/utils/index.js b/src/utils/index.js index 4e65504..ab7b5f1 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,4 +1,12 @@ import { parseTime } from './ruoyi' +import prototypes from "./modules/prototypes" +import timerEvent from "./modules/timerEvent"; +export const util = { + install(Vue) { + prototypes(); + timerEvent(); + } +} /** * 表格时间格式化 diff --git a/src/utils/modules/prototypes.js b/src/utils/modules/prototypes.js new file mode 100644 index 0000000..2d14d73 --- /dev/null +++ b/src/utils/modules/prototypes.js @@ -0,0 +1,122 @@ +/** + * 原型扩展库 + */ +export default function(){}; + +/** + * 将日期对象格式化成字符串 + * @param {String} format 格式支持:yyyy-MM-dd HH:mm:ss.S 默认:yyyy-MM-dd + */ +Date.prototype.format = function (format) { + format = format == null ? "yyyy-MM-dd" : format; + let o = { + "M+": this.getMonth() + 1, //month + "d+": this.getDate(), //day + "H+": this.getHours(), //hour + "m+": this.getMinutes(), //minute + "s+": this.getSeconds(), //second + "q+": Math.floor((this.getMonth() + 3) / 3), //quarter + "S": this.getMilliseconds() //millisecond + } + + if (/(y+)/.test(format)) + format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); + for (var k in o) + if (new RegExp("(" + k + ")").test(format)) + format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); + return format; +} + +Number.prototype.toDate = function () { + return new Date(this); +} + +Number.prototype.toChina = function () { + let number = this.toString(); + if (number.match(/\D/) || number.length >= 14) return; + let zhArray = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']; // 数字对应中文 + let baseArray = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万']; //进位填充字符,第一位是 个位,可省略 + let string = String(number).split('').reverse().map((item, index) => { // 把数字切割成数组并倒序排列,然后进行遍历转成中文 + // 如果当前位为0,直接输出数字, 否则输出 数字 + 进位填充字符 + item = Number(item) == 0 ? zhArray[Number(item)] : zhArray[Number(item)] + baseArray[index]; + return item; + }).reverse().join(''); // 倒叙回来数组,拼接成字符串 + string = string.replace(/^一十/, '十'); // 如果以 一十 开头,可省略一 + string = string.replace(/零+/, '零'); // 如果有多位相邻的零,只写一个即可 + return string; +} +/** +* 将字符串格式化成日期对象 +* @param {String} format 格式支持:yyyy-MM-dd HH:mm:ss.S 默认:yyyy-MM-dd +*/ +String.prototype.toDate = function (format) { + if (/^\d+$/.test(this)) { + return new Date(Number(this)); + } + format = format == null ? "yyyy-MM-dd" : format; + var d = new Date(); + var o = { + "y+": "d.setYear(val)", //year + "M+": "d.setMonth(val-1)", //month + "d+": "d.setDate(val)", //day + "H+": "d.setHours(val)", //hour + "m+": "d.setMinutes(val)", //minute + "s+": "d.setSeconds(val)", //second + "S+": "d.setMilliSeconds(val)" //millisecond + } + var regx = format + for (var k in o) { + if (new RegExp("(" + k + ")").test(format)) + regx = regx.replace(new RegExp(k), "[0-9]+"); + } + + if (!new RegExp("^" + regx + "$").test(this)) { + throw new Error("日期格式不正确:" + format); + } + + for (var k in o) { + if (new RegExp("(" + k + ")").test(format)) { + var regx = format; + for (var temp in o) { + if (temp == k) { + regx = regx.replace(new RegExp(temp), "([0-9]+)"); + } else { + regx = regx.replace(new RegExp(temp), "[0-9]+"); + } + + } + var val = Number(this.replace(new RegExp("^" + regx + "$"), "$1")); + try { + + if (k == "S+") { + d.setMilliseconds(val); + } else { + eval(o[k].replace("val", val)); + } + } catch (e) { alert(e); } + } else { + try { + var val = k == "M+" ? 1 : 0; + if (k == "S+") { + d.setMilliseconds(val); + } else { + eval(o[k].replace("val", val)); + } + } catch (e) { alert(e); } + } + } + return d; +} + +String.prototype.toTime = function () { + return eval(this.replace(/^(\d{2}):(\d{2}):(\d{2})$/,"$1*3600+$2*60+$3")); +} + +Number.prototype.toTime = function(){ + let ss = this % 60; + let temp = (this - ss)/60; + let mm = temp % 60; + let hh = (temp - mm)/60; + + return `${hh.toString().padStart(2,"0")}:${mm.toString().padStart(2,"0")}:${ss.toString().padStart(2,"0")}` +} \ No newline at end of file diff --git a/src/utils/modules/timerEvent.js b/src/utils/modules/timerEvent.js new file mode 100644 index 0000000..2f084a6 --- /dev/null +++ b/src/utils/modules/timerEvent.js @@ -0,0 +1,25 @@ +export default function(){} +/** + * 触发周期性事件 + */ +let count = 0; +window.dispatchEvent(new CustomEvent("timer", { bubbles: false, detail: { count: count } })); +window.setInterval(() => { + count++; + if (count > 36000000) { + count = 0; + } + window.dispatchEvent(new CustomEvent("timer", { bubbles: false, detail: { count: count } })); + if(count%5==0){ + window.dispatchEvent(new CustomEvent("timer5",{bubbles: false})); + } + if(count%10==0){ + window.dispatchEvent(new CustomEvent("timer10",{bubbles: false})); + } + if(count%30==0){ + window.dispatchEvent(new CustomEvent("timer30",{bubbles: false})); + } + if(count%60==0){ + window.dispatchEvent(new CustomEvent("timer60",{bubbles: false})); + } +}, 1000); \ No newline at end of file