|
@@ -0,0 +1,576 @@
|
|
|
+const util = {}
|
|
|
+util.isValue = (val) => {
|
|
|
+ if (val === null || val === undefined || (typeof val === 'string' && val.trim() === '') || (typeof val === 'object' && val?.length === 0)) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+util.structureParams = (array, attribute) => {
|
|
|
+ const endArray = []
|
|
|
+ array.forEach(v => {
|
|
|
+ endArray.push(v[attribute])
|
|
|
+ })
|
|
|
+ return endArray
|
|
|
+}
|
|
|
+
|
|
|
+util.replaceParams = (array, reArray, attribute, reAttribute) => {
|
|
|
+ const endArray = []
|
|
|
+ const endAllArray = []
|
|
|
+ array.forEach(v => {
|
|
|
+ reArray.forEach(rv => {
|
|
|
+ if (v === rv[attribute]) {
|
|
|
+ endArray.push(rv[reAttribute])
|
|
|
+ endAllArray.push(rv)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ return {
|
|
|
+ replace: endArray,
|
|
|
+ all: endAllArray
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+util.copyObject = (ob) => {
|
|
|
+ return JSON.parse(JSON.stringify(ob))
|
|
|
+}
|
|
|
+
|
|
|
+util.arrayToMap = (array, key) => {
|
|
|
+ const map = new Map()
|
|
|
+ array.forEach((v) => {
|
|
|
+ map.set(v[key], v)
|
|
|
+ })
|
|
|
+ return map
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通过某个字段在一个多级数组中查找数据
|
|
|
+ * @param data 目标数组,不能包含函数
|
|
|
+ * @param current 目标数据
|
|
|
+ * @param key 查找的字段
|
|
|
+ * @param children 子集集合字段
|
|
|
+ */
|
|
|
+util.findInListData = (data, current, key = "id", children = 'children') => {
|
|
|
+
|
|
|
+ for(let item of data){
|
|
|
+
|
|
|
+ if(item[key] && JSON.parse(JSON.stringify(item[key])) == JSON.parse(JSON.stringify(current))) return item
|
|
|
+
|
|
|
+ if(!!item[children] && Array.isArray(item[children]) && item[children].length > 0){
|
|
|
+
|
|
|
+ const findChildData = findInListData(item[children], current, key, children)
|
|
|
+
|
|
|
+ if(findChildData) return findChildData
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null
|
|
|
+}
|
|
|
+
|
|
|
+util.formatGetParam = (params) => {
|
|
|
+ let paramUrl = ''
|
|
|
+ Object.keys(params).forEach((v, i) => {
|
|
|
+ paramUrl += i === 0 ? `${v}=${encodeURIComponent(params[v])}` : `&${v}=${encodeURIComponent(params[v])}`
|
|
|
+ })
|
|
|
+ return paramUrl
|
|
|
+}
|
|
|
+
|
|
|
+util.formatTableHeadFilters = (arr, text = 'dictLabel', value = 'dictValue') => {
|
|
|
+ return arr.map(v => {
|
|
|
+ v.value = v[value]
|
|
|
+ v.text = v[text]
|
|
|
+ return v
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+util.YMDHms = (date) => {
|
|
|
+ const _date = new Date(date)
|
|
|
+ const Y = `${_date.getFullYear()}`;
|
|
|
+ const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
|
|
|
+ const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
|
|
|
+ const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
|
|
|
+ const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
|
|
|
+ const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
|
|
|
+ return `${Y}-${M}-${D} ${H}:${m}:${s}`;
|
|
|
+}
|
|
|
+
|
|
|
+util.YM = (date, format = false) => {
|
|
|
+ const _date = new Date(date)
|
|
|
+ const Y = `${_date.getFullYear()}`;
|
|
|
+ const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
|
|
|
+ return format ? `${Y}年${M}月` : `${Y}-${M}`;
|
|
|
+}
|
|
|
+util.M = (date, format = false) => {
|
|
|
+ const _date = new Date(date)
|
|
|
+ const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
|
|
|
+ return format ? `${M}月` : `${M}`;
|
|
|
+}
|
|
|
+
|
|
|
+util.YMD = (date, format = false) => {
|
|
|
+ const _date = new Date(date)
|
|
|
+ const Y = `${_date.getFullYear()}`;
|
|
|
+ const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
|
|
|
+ const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
|
|
|
+ return format ? `${Y}年${M}月${D}日` : `${Y}-${M}-${D}`;
|
|
|
+}
|
|
|
+
|
|
|
+util.Hms = (date, format = false) => {
|
|
|
+ const _date = new Date(date)
|
|
|
+ const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
|
|
|
+ const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
|
|
|
+ const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
|
|
|
+ return format ? `${H}时${m}分${s}秒` : `${H}:${m}:${s}`;
|
|
|
+}
|
|
|
+
|
|
|
+//防抖
|
|
|
+util.debounce = function (cb, ms = 0) {
|
|
|
+ let timer = null
|
|
|
+ return function () {
|
|
|
+ if (timer) clearTimeout(timer)
|
|
|
+ timer = setTimeout(() => {
|
|
|
+ // @ts-ignore
|
|
|
+ cb.apply(this, arguments)
|
|
|
+ timer = null
|
|
|
+ }, ms)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+util.comTime = (time, format = false) => {
|
|
|
+ const sAll = time
|
|
|
+ const d = Math.floor(sAll / (1000 * 60 * 60 * 24))
|
|
|
+ const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
|
|
|
+ const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60))
|
|
|
+ const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000)
|
|
|
+ let result = {d, h, m ,s}
|
|
|
+ if (format) {
|
|
|
+ result = ''
|
|
|
+ if (d > 0) {
|
|
|
+ result += d + '天'
|
|
|
+ }
|
|
|
+ if (d > 0 || h > 0) {
|
|
|
+ result += h + '时'
|
|
|
+ }
|
|
|
+ if (d > 0 || h > 0 || m > 0) {
|
|
|
+ result += m + '分'
|
|
|
+ }
|
|
|
+ result += s + '秒'
|
|
|
+ }
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+util.comTimeByArea = (start, end, format = false, _D = '天', _H = '时', _M = '分', _S = '秒') => {
|
|
|
+ const sAll = new Date(end).getTime() - new Date(start).getTime()
|
|
|
+ const d = Math.floor(sAll / (1000 * 60 * 60 * 24))
|
|
|
+ const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
|
|
|
+ const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60))
|
|
|
+ const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000)
|
|
|
+ let result = {d, h, m ,s}
|
|
|
+ if (format) {
|
|
|
+ result = ''
|
|
|
+ if (d > 0) {
|
|
|
+ result += d + _D
|
|
|
+ }
|
|
|
+ if (d > 0 || h > 0) {
|
|
|
+ result += h + _H
|
|
|
+ }
|
|
|
+ if (d > 0 || h > 0 || m > 0) {
|
|
|
+ result += m + _M
|
|
|
+ }
|
|
|
+ result += s + _S
|
|
|
+ }
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+util.deepAssign = (...obj) => {
|
|
|
+ const result = Object.assign({}, ...obj)
|
|
|
+ for (let item of obj) {
|
|
|
+ for (let [idx, val] of Object.entries(item)) {
|
|
|
+ if (val instanceof Array) {
|
|
|
+ result[idx] = val
|
|
|
+ } else if (val instanceof Object) {
|
|
|
+ result[idx] = deepAssign(result[idx], val)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+util.copy = (value) => {
|
|
|
+ const str = document.createElement('input')
|
|
|
+ str.setAttribute('value', value)
|
|
|
+ document.body.appendChild(str)
|
|
|
+ str.select()
|
|
|
+ document.execCommand('copy')
|
|
|
+ document.body.removeChild(str)
|
|
|
+ console.log(value)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param precision 精度 1、0.1 、0.01……
|
|
|
+ * @param colorArr
|
|
|
+ * [
|
|
|
+ * [20.1, '#111111'],
|
|
|
+ * [20.3, '#dddddd'],
|
|
|
+ * [20.7, '#eeeeee'],
|
|
|
+ * ]
|
|
|
+ * @return colorMap
|
|
|
+ * new Map([
|
|
|
+ * [20.1, '#111111']
|
|
|
+ * ……
|
|
|
+ * [20.3, '#dddddd']
|
|
|
+ * ……
|
|
|
+ * [20.7, '#eeeeee']
|
|
|
+ * ])
|
|
|
+ */
|
|
|
+util.getGradientColorArray = (precision, colorArr) => {
|
|
|
+ // 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
|
|
|
+ const colorRgb = (sColor) => {
|
|
|
+ const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
|
|
|
+ let _sColor = sColor.toLowerCase();
|
|
|
+ if (_sColor && reg.test(_sColor)) {
|
|
|
+ if (_sColor.length === 4) {
|
|
|
+ let sColorNew = "#";
|
|
|
+ for (let i = 1; i < 4; i += 1) {
|
|
|
+ sColorNew += _sColor.slice(i, i + 1).concat(_sColor.slice(i, i + 1));
|
|
|
+ }
|
|
|
+ _sColor = sColorNew;
|
|
|
+ }
|
|
|
+ //处理六位的颜色值
|
|
|
+ const sColorChange = [];
|
|
|
+ for (let i = 1; i < 7; i += 2) {
|
|
|
+ // @ts-ignore
|
|
|
+ sColorChange.push(parseInt("0x" + _sColor.slice(i, i + 2)));
|
|
|
+ }
|
|
|
+ return sColorChange;
|
|
|
+ } else {
|
|
|
+ return _sColor;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // 将rgb表示方式转换为hex表示方式
|
|
|
+ const colorHex = (rgb) => {
|
|
|
+ const _this = rgb;
|
|
|
+ const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
|
|
|
+ if (/^(rgb|RGB)/.test(_this)) {
|
|
|
+ const aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, "").split(",");
|
|
|
+ let strHex = "#";
|
|
|
+ for (let i = 0; i < aColor.length; i++) {
|
|
|
+ let hex = Number(aColor[i]).toString(16);
|
|
|
+ hex = Number(hex) < 10 ? 0 + '' + hex : hex;// 保证每个rgb的值为2位
|
|
|
+ if (hex === "0") {
|
|
|
+ hex += hex;
|
|
|
+ }
|
|
|
+ strHex += hex;
|
|
|
+ }
|
|
|
+ if (strHex.length !== 7) {
|
|
|
+ strHex = _this;
|
|
|
+ }
|
|
|
+ return strHex;
|
|
|
+ } else if (reg.test(_this)) {
|
|
|
+ const aNum = _this.replace(/#/, "").split("");
|
|
|
+ if (aNum.length === 6) {
|
|
|
+ return _this;
|
|
|
+ } else if (aNum.length === 3) {
|
|
|
+ let numHex = "#";
|
|
|
+ for (let i = 0; i < aNum.length; i += 1) {
|
|
|
+ numHex += (aNum[i] + aNum[i]);
|
|
|
+ }
|
|
|
+ return numHex;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return _this;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const rgb2hex = (sRGB) => {
|
|
|
+ const reg = /^(RGB|rgb)\((\d+),\s*(\d+),\s*(\d+)\)$/
|
|
|
+ if (!reg.test(sRGB)) return sRGB
|
|
|
+ const rgbArr = sRGB.match(/\d+/g)
|
|
|
+ const resultRgbArr = rgbArr.map((v) => {
|
|
|
+ if (+v > 16) return (+v).toString(16)
|
|
|
+ return '0' + (+v).toString(16)
|
|
|
+ })
|
|
|
+ return '#' + resultRgbArr.join('')
|
|
|
+ }
|
|
|
+ const gradientColor = (startColor, endColor, step) => {
|
|
|
+ const startRGB = colorRgb(startColor);//转换为rgb数组模式
|
|
|
+ const startR = startRGB[0];
|
|
|
+ const startG = startRGB[1];
|
|
|
+ const startB = startRGB[2];
|
|
|
+ const endRGB = colorRgb(endColor);
|
|
|
+ const endR = endRGB[0];
|
|
|
+ const endG = endRGB[1];
|
|
|
+ const endB = endRGB[2];
|
|
|
+ const sR = (endR - startR) / step;//总差值
|
|
|
+ const sG = (endG - startG) / step;
|
|
|
+ const sB = (endB - startB) / step;
|
|
|
+ const colorArr = [];
|
|
|
+ for (let i = 0; i <= step; i++) {
|
|
|
+ //计算每一步的hex值
|
|
|
+ const hex = colorHex('rgb(' + parseInt((sR * i + startR)) + ',' + parseInt((sG * i + startG)) + ',' + parseInt((sB * i + startB)) + ')');
|
|
|
+ // @ts-ignore
|
|
|
+ colorArr.push(rgb2hex(hex));
|
|
|
+ }
|
|
|
+ return colorArr;
|
|
|
+ }
|
|
|
+ const colorMap = new Map()
|
|
|
+ colorArr.forEach((v, i) => {
|
|
|
+ if (i < colorArr.length - 1) {
|
|
|
+ const _arr = gradientColor(v[1], colorArr[i + 1][1], (Number(colorArr[i + 1][0]) - Number(v[0])) / precision)
|
|
|
+ _arr.forEach((cV, cI) => {
|
|
|
+ colorMap.set((Number(v[0]) + cI * precision).toFixed(String(precision).split('').filter(p => p === '0').length), cV)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ colorMap.set(Number(v[0]).toFixed(String(precision).split('').filter(p => p === '0').length), v[1])
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return colorMap
|
|
|
+}
|
|
|
+// 根据部门名称 寻找上级所有父节点名称
|
|
|
+//data:要遍历的数据, target:查找目标, result用于装查找结果的数组
|
|
|
+util.findParent = (data, target, result) => {
|
|
|
+ for (let item of data) {
|
|
|
+ if (item.deptName === target) {
|
|
|
+ //将查找到的目标数据加入结果数组中
|
|
|
+ //可根据需求unshift(item.id)或unshift(item)
|
|
|
+ result.unshift(item.deptName);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (item.children && item.children.length > 0) {
|
|
|
+ let isFind = findParent(item.children, target, result);
|
|
|
+ if (isFind) {
|
|
|
+ result.unshift(item.deptName);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+};
|
|
|
+
|
|
|
+// 涉及到跨域问题,需要配nginx代理的url地址处理
|
|
|
+util.proxyNginxUrl = (url) => {
|
|
|
+ if (url) {
|
|
|
+ // @ts-ignore
|
|
|
+ const apiMapper = window.cusConfig?.nginxApiMapper || new Map()
|
|
|
+ let newUrl = url
|
|
|
+ apiMapper.forEach((v, k) => {
|
|
|
+ if (url.includes(k)) {
|
|
|
+ newUrl = v + url.substring(url.indexOf(k) + k.length, url.length)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return newUrl
|
|
|
+ }
|
|
|
+ return url
|
|
|
+};
|
|
|
+
|
|
|
+// 后端接口拦截 < > 内的文本,正反向格式化为 /《/ /》/ res = true 为接收返回值进行格式化
|
|
|
+util.formatInputHtmlInterceptor = (html, res = true) => {
|
|
|
+ if (html) {
|
|
|
+ const map = new Map()
|
|
|
+ map.set('<', '_《_')
|
|
|
+ map.set('>', '_》_')
|
|
|
+ let nHtml = html.toString()
|
|
|
+ map.forEach((v, k) => {
|
|
|
+ if (res) {
|
|
|
+ nHtml = nHtml.replace(eval(`/${v}/g`), k)
|
|
|
+ } else {
|
|
|
+ nHtml = nHtml.replace(eval(`/${k}/g`), v)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return nHtml
|
|
|
+ }
|
|
|
+ return html
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//生成从minNum到maxNum的随机数
|
|
|
+// export const randomNum = (minNum,maxNum) => {
|
|
|
+// return parseInt(String(Math.random() * (maxNum - minNum + 1) + minNum),10);
|
|
|
+// }
|
|
|
+
|
|
|
+util.randomNum = (min = 0, max = 0, decimal=0) => {
|
|
|
+ // 获取数值的小数部分
|
|
|
+ const getDecimalNum = (data) => {
|
|
|
+ return Number(data.toString().split('.')[1]);
|
|
|
+ }
|
|
|
+ let min_z = Math.trunc(min); // 最小值的整数部分
|
|
|
+ let max_z = Math.trunc(max); // 最大值的整数部分
|
|
|
+ // 判断是否存在小数部分,不存在的话为0
|
|
|
+ let min_x = isNaN(getDecimalNum(min)) ? 0 : getDecimalNum(min); // 最小值的小数部分
|
|
|
+ let max_x = isNaN(getDecimalNum(max)) ? 0 : getDecimalNum(max); // 最大值的小数部分
|
|
|
+
|
|
|
+ // 区分有小数和没小数的情况
|
|
|
+ if (min_x > 0 || max_x > 0 || decimal > 0) {
|
|
|
+ // 整数部分随机数
|
|
|
+ let z = parseInt(String(Math.random() * (max_z - min_z + 1) + min_z), 10);
|
|
|
+ // 小数部分随机数
|
|
|
+ let x = 0;
|
|
|
+ // 小数部分随机数最大位数
|
|
|
+ let max_decimal = min_x.toString().length > max_x.toString().length ? min_x.toString().length : max_x.toString().length;
|
|
|
+ max_decimal = decimal > max_decimal ? decimal : max_decimal;
|
|
|
+ // 判断随机出的整数部分,是否等于最小值或者最大值
|
|
|
+ if(z == min_z || z == max_z){
|
|
|
+ if(z == min_z){
|
|
|
+ // 整数部分随机数等于最小值,那么应该从最小值的小数部分开始,到小数位数的最大值随机就可以
|
|
|
+ x = parseInt(String(Math.random() * (Math.pow(10, max_decimal) - min_x) + min_x), 10);
|
|
|
+ }else{
|
|
|
+ // 整数部分随机数等于最大值,那么应该从0开始,到最大值小数部分
|
|
|
+ x = parseInt(String(Math.random() * (max_x + 1)), 10);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ // 整数部分在最大最小值区间的,就从0到小数位数的最大值随机就可以
|
|
|
+ x = parseInt(String(Math.random() * (Math.pow(10, max_decimal))), 10);
|
|
|
+ }
|
|
|
+ return Number(`${z}.${x}`);
|
|
|
+ } else {
|
|
|
+ return parseInt(String(Math.random() * (max_z - min_z + 1) + min_z), 10);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param val 处理的数值
|
|
|
+ * @param unit 单位,默认空字符
|
|
|
+ * @param digits 小数,默认1
|
|
|
+ */
|
|
|
+util.formatNumberUnit = (val, unit = '', digits = 1) => {
|
|
|
+ let num = ''
|
|
|
+ let str = ''
|
|
|
+ if (String(val).length < 5) {
|
|
|
+ num = val
|
|
|
+ str = unit
|
|
|
+ } else if (String(val).length < 9) {
|
|
|
+ num = Number((Number(val) / 10000).toFixed(digits)) / 1 + ''
|
|
|
+ str = '万' + unit
|
|
|
+ } else if (String(val).length < 13) {
|
|
|
+ num = Number((Number(val) / 10000 / 10000).toFixed(digits)) / 1 + ''
|
|
|
+ str = '亿' + unit
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ num: num, unit: str
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 将一串数字str按指定间隔n填充指定字符char
|
|
|
+ * @param str
|
|
|
+ * @param n
|
|
|
+ * @param char
|
|
|
+ */
|
|
|
+util.numberJoinChar = (str, n = 3, char = ',') => {
|
|
|
+ const _str = String(str)
|
|
|
+ let result = '';
|
|
|
+ for (let i = _str.length - 1; i >= 0; i--) {
|
|
|
+ result = _str[i] + result;
|
|
|
+ if ((_str.length - i) % n === 0 && i !== 0) {
|
|
|
+ result = char + result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
+// 根据起始周几获取本周第一天日期
|
|
|
+util.getFirstDateOfWeek = (fDate, firstDayOfWeek) => {
|
|
|
+ const day = fDate.getDay();
|
|
|
+ const diff = (day < firstDayOfWeek ? 7 : 0) + day - firstDayOfWeek;
|
|
|
+ const firstDate = new Date(fDate.getFullYear(), fDate.getMonth(), fDate.getDate() - diff);
|
|
|
+ return firstDate;
|
|
|
+}
|
|
|
+
|
|
|
+// 根据起始周几获取本周最后一天日期
|
|
|
+util.getLastDateOfWeek = (lDate, lastDayOfWeek) => {
|
|
|
+ const day = lDate.getDay();
|
|
|
+ const diff = (day < lastDayOfWeek ? 7 : 0) + day - lastDayOfWeek;
|
|
|
+ const lastDate = new Date(lDate.getFullYear(), lDate.getMonth(), lDate.getDate() + (6 - diff));
|
|
|
+ return lastDate;
|
|
|
+}
|
|
|
+
|
|
|
+util.oneDayTime = 1000 * 60 * 60 * 24
|
|
|
+
|
|
|
+// 根据当天时间、起始周几、选的月,获取整月日期
|
|
|
+util.getMonthCalendarData = (d, fD, selectMonth = null) => {
|
|
|
+ const getYMDTime = (date) => {
|
|
|
+ const _date = new Date(date)
|
|
|
+ const Y = _date.getFullYear()
|
|
|
+ const M = _date.getMonth() + 1
|
|
|
+ const D = _date.getDate()
|
|
|
+ return new Date(`${Y}-${M}-${D}`).getTime()
|
|
|
+ }
|
|
|
+ const _date = selectMonth ? new Date(selectMonth) : new Date(d)
|
|
|
+ // d的月份有几天
|
|
|
+ const monthDaysTotal = new Date(_date.getFullYear(), _date.getMonth() + 1, 0).getDate()
|
|
|
+ // d的月份的第一天
|
|
|
+ const monthFirstDate = new Date(_date.getFullYear(), _date.getMonth(), 1)
|
|
|
+ // 日历第一天
|
|
|
+ const wholeFirstDay = getFirstDateOfWeek(monthFirstDate, fD)
|
|
|
+ // d的月份的最后一天
|
|
|
+ const monthLastDate = new Date(_date.getFullYear(), _date.getMonth(), monthDaysTotal)
|
|
|
+ // 日历最后一天
|
|
|
+ const wholeLastDay = getLastDateOfWeek(monthLastDate, fD)
|
|
|
+ const arr = []
|
|
|
+ let w = []
|
|
|
+ for (let i = wholeFirstDay.getTime(); i <= wholeLastDay.getTime(); i += oneDayTime) {
|
|
|
+ const t = new Date(i)
|
|
|
+ const obj = {
|
|
|
+ date: t,
|
|
|
+ dateStr: YMD(t),
|
|
|
+ dayStr: t.getDate(),
|
|
|
+ tom: getYMDTime(t) > getYMDTime(d)
|
|
|
+ }
|
|
|
+ if (getYMDTime(t) < monthFirstDate.getTime()) {
|
|
|
+ obj.last = true // 是补全的日历过去时间
|
|
|
+ } else if (getYMDTime(t) > monthLastDate.getTime()) {
|
|
|
+ obj.will = true // 是补全的日历未来时间
|
|
|
+ } else if (getYMDTime(t) === getYMDTime(d)) {
|
|
|
+ obj.today = true // 是传入的当前日期
|
|
|
+ }
|
|
|
+ w.push(obj)
|
|
|
+ if (w.length === 7) {
|
|
|
+ const wObj = {
|
|
|
+ calendar: w
|
|
|
+ }
|
|
|
+ arr.push(wObj)
|
|
|
+ w = []
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return arr
|
|
|
+}
|
|
|
+
|
|
|
+// 根据起始周几获取一周的数组
|
|
|
+util.getWeekCN = (xq) => {
|
|
|
+ const m = new Map([
|
|
|
+ [0, '日'],
|
|
|
+ [1, '一'],
|
|
|
+ [2, '二'],
|
|
|
+ [3, '三'],
|
|
|
+ [4, '四'],
|
|
|
+ [5, '五'],
|
|
|
+ [6, '六'],
|
|
|
+ ])
|
|
|
+ const weekArray = [];
|
|
|
+ for (let i = 0; i < 7; i++) {
|
|
|
+ const index = (xq + i) % 7;
|
|
|
+ weekArray.push(m.get(index));
|
|
|
+ }
|
|
|
+ return weekArray
|
|
|
+}
|
|
|
+
|
|
|
+util.resetRgbaOpacity = (polyColor, opacity) => {
|
|
|
+ // 使用正则表达式匹配rgba颜色中的透明度部分
|
|
|
+ const rgbaRegex = /rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)/;
|
|
|
+ const matches = polyColor.match(rgbaRegex);
|
|
|
+ if (!matches) {
|
|
|
+ throw new Error('Invalid rgba string');
|
|
|
+ }
|
|
|
+ // 提取原来的透明度值,并将其乘以给定因子
|
|
|
+ const oldOpacity = parseFloat(matches[4]);
|
|
|
+ const newOpacity = Math.min(Math.max(oldOpacity * opacity, 0), 1); // 确保透明度值在0到1之间
|
|
|
+ // 重新组合rgba字符串
|
|
|
+ const newRgbaStr = `rgba(${matches[1]}, ${matches[2]}, ${matches[3]}, ${newOpacity})`;
|
|
|
+ return newRgbaStr;
|
|
|
+}
|
|
|
+
|
|
|
+util.randomColor = (opacity) => `rgba(${randomNum(0, 255)}, ${randomNum(0, 255)}, ${randomNum(0, 255)}, ${opacity ? opacity : randomNum(0.5, 1, 1)})`
|
|
|
+
|
|
|
+export default util
|